Functions - return values

I understand that

#include <iostream>

int main ()

{
bla
bla
bla
}

Is a function. It has x amount of statements.

Then when you return values suddenly, cout << Return5(); // prints 5
becomes a function as well.
that's right... you have a method int Return5() {} when you call this method the calling is replaced by the return value. what's the problem?

regards
It's more that I am confused as to the terminology.

cout << Return5();

should produce out-put from

Return5 () {}

if i am correct.

that is a function.

the activity below it asks me to use these various functions in a program.

cout << Return5();
cout << Return5() + 2; // prints 7
cout << ReturnNothing(); // This will not compile.

How are they functions, i thought they were statements. (x amount of statements makes a function)

unless -.-

cout << Return5();

its saying print the value of function Return5 ()???

Thank you

Regards.
Last edited on

its saying print the value of function Return5 ()???


Rather, the return value. A function call is a statement.
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int Return5()
    {
    return 5;
    }

void ReturnNothing()
    {
    return;
    }

int main ()
    {
    cout << Return5(); // this calls the function and prints it's return value
    cout << Return5() + 2; // same as above, but adds 2 to it's return value
    cout << ReturnNothing(); // same as above, but nothing is returned from function (void)
    }


See http://www.cplusplus.com/doc/tutorial/functions/ for more info.
Last edited on
// this calls the function and prints it's return value

its!
cout << ReturnNothing(); // same as above, but nothing is returned from function (void)


Actually I'm pretty sure this is undefined behavior (or at least not "legal").
teddyeddy wrote:
 
cout << ReturnNothing(); // This will not compile. 


He already mentioned that before.
Last edited on
Topic archived. No new replies allowed.