Ok, this is going to sound a little odd but I don't think the code below should work but it does: the function circleCircumference() is never called in main() though it's return value is used (Line 16) but it compiles and runs as intended.
Some of you may ask "It work's so what's the problem?" but I'm very new to c++ and want to understand why it works... does using the return value act as calling the function?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
float circleCircumference()
{
double circleCircumferenceValue = 0; //initialise the variable
constfloat piValue = 3.14159265359; //define Pi
cout << "This program calculates the circumference of a circle of a given radius." << endl;
cout << "Enter a number: "; //request input
cin >> circleCircumferenceValue; //capture input to variable
return circleCircumferenceValue * piValue; //generate the value
}
int main()
{
cout << "The circumference is: " << circleCircumference() << endl; //outputs the result
return 0;
}
the function circleCircumference() is never called in main() though it's return value is used (Line 16)
You have a slight misunderstanding here, it seems. The function *is* called in main. You cannot just "use" the return value of the function unless you store it somewhere yourself.
You have a return value because you're calling the function. Line 16 is a perfectly valid function call that returns a float that is then passed to operator<< without being stored in a variable first.
Thanks all, think I understand now, the use of the return value and the call are one and the same, but if I wanted to retain the return value I'd need to assign it a variable defined in main() like: