Do Return Values call Functions?

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>

using namespace std;

float circleCircumference()
    {
    double circleCircumferenceValue = 0; //initialise the variable
    const float 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.
Last edited on
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.
In cout << "The circumference is: " << circleCircumference() << endl;
the expression circleCircumference() needs to be evaluated.

(Just as in cout << i+j ;, the expression i+j needs to be evaluated.)

Evaluating circleCircumference() involves calling the function.
And the result of that evaluation is the value returned by the function.
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:

float circleCircReturnValue = circleCircumference()

Topic archived. No new replies allowed.