Home > Enterprise >  Why can I not call a function from a variable's definition?
Why can I not call a function from a variable's definition?

Time:02-05

I have included the code I wrote below. I have created a function that calculates the volume of a cone based on the user's input. This is working as intended.

# include <iostream>
# include <string.h>
# include <string>

using namespace std;

// ConeVolume prototype
float ConeVolume(float radius, float height);


int main()
{
    // Establish variables 
    float radius1;
    float height2;
    float volumeCone = ConeVolume(radius1, height2);

    // User input to define the varibales
    cout << "Radius: ";
    cin >> radius1;
    cout << "Height: ";
    cin >> height2;

    // Return variable using the ConeVolume function
    cout << endl << "Cone Volume: " << volumeCone;

    return 0;
}

// Function that calculates the volume of a Cone
float ConeVolume(float radius, float height)
{
    float pi = 3.14;
    float volume = (pi/3)*(radius * radius) * (height);
    
    return volume;
}


My question... if I were to call the function by outputting the variable "float ConeVolume" as below, why does the program return '0'? Can I not set the value of a variable equal to a function?

    // Return variable using the volumeCone float variable
    cout << endl << "Cone Volume: " << volumeCone;

CodePudding user response:

You've simply made a silly mistake. You've called the 'ConeVolume' function before taking user input. So, only garbage values are being passed to the function.

# include <iostream>
# include <string.h>
# include <string>

using namespace std;

// ConeVolume prototype
float ConeVolume(float radius, float height);


int main()
{
    // Establish variables 
    float radius1;
    float height2;

    //wrong code here
    // you've called the function before taking input of radius1 and height2
    //float volumeCone = ConeVolume(radius1, height2);

    // User input to define the varibales
    cout << "Radius: ";
    cin >> radius1;
    cout << "Height: ";
    cin >> height2;

    // Correct code:
    // Call the function after taking input
    float volumeCone = ConeVolume(radius1, height2);

    // Return variable using the ConeVolume function
    cout << endl << "Cone Volume: " << volumeCone;

    return 0;
}

// Function that calculates the volume of a Cone
float ConeVolume(float radius, float height)
{
    float pi = 3.14;
    float volume = (pi/3)*(radius * radius) * (height);
    
    return volume;
}

Hope this helped.

CodePudding user response:

The program return 0 because the value volumeCone is not being updated after you changed the values of radius1 and height2.

You have to call the function coneVolume() again, or better yet just call it after you define radius1 and height2.

int main()
{
    // Establish variables 
    float radius1;
    float height2;

    // User input to define the varibales
    cout << "Radius: ";
    cin >> radius1;
    cout << "Height: ";
    cin >> height2;

    float volumeCone = ConeVolume(radius1, height2);

    // Return variable using the ConeVolume function
    cout << endl << "Cone Volume: " << volumeCone;

    return 0;
}

CodePudding user response:

Insert the line

volumeCone = ConeVolume(radius1, height2);

after the line

cin >> height2;

And change line

float volumeCone = ConeVolume(radius1, height2);

to

float volumeCone;

.

  • Related