5

I am confused with the output of below code when I execute it.

Code:

int add(int a, int b)
{
    cout<<"inside int add function"<<endl;
    return a+b;
}

float add(float a, float b)
{
    cout<<"inside float add function"<<endl;
    return a+b;
}

int main()
{
    cout<<add(10.0f,20.0f)<<endl<<add(20,50); 
    return 0;
}

output:

inside int add function
inside float add function
30
70

I dont understand the order of cout messages are getting printed in console. But I expected the output of above program like below

inside float add function
30
inside int add function
70

Could someone explain about above behavior.

DNamto
  • 1,284
  • 1
  • 17
  • 42
Govan Gova
  • 53
  • 5
  • It looks like the compiler is just evaluating both functions before performing the `cout`. So you're seeing it run the int add function, then the float add function, *then* the `cout`. – cf stands with Monica Apr 26 '14 at 09:54
  • It's a sort of compiler optimization, it's the same as `return Something(getWhatever(), getWhatever());` the compiler sometimes will swap both calls. –  Apr 26 '14 at 10:00
  • possible duplicate of [cout << order of call to functions it prints?](http://stackoverflow.com/questions/2129230/cout-order-of-call-to-functions-it-prints) – edmz Apr 26 '14 at 10:02
  • 2
    Is there some reason you think one output is *required* over the other? It's certainly reasonable to expect what you claim to expect. But the compiler is free to do something different than we expect unless we insist that it not. Is there some reason you think the result you got is prohibited? Surely if you do `add(1,2)+add(1.0,2.0)`, the calls to `add` can occur in either order, right? – David Schwartz Apr 26 '14 at 10:08

2 Answers2

7

This line in you code:

cout<<add(10.0f,20.0f)<<endl<<add(20,50);

will be translated by the compiler into:

operator<<(operator<<(operator<<(cout,add(10.0f,20.0f)),endl),add(20,50));

As the order of evaluation of function parameters is not mandated by the standard, it just happens that add(20,50) is evaluated before operator<<(operator<<(cout,add(10.0f,20.0f)),endl).

Massimiliano
  • 7,082
  • 2
  • 40
  • 56
1

the line cout<<add(10.0f,20.0f)<<endl<<add(20,50); is expected to print your output:

inside int add function
inside float add function
30
70

Thats because to print to cout firstly calls add(10.0f , 20.0f) and stores the output to a internal variable, then calls add(10, 20) and stores the output to another internal variable, and finally it prints the returned values. Something like this:

float a = add(10.0f, 20.0f);
int b = add(10, 20);
cout << a << endl << b;

In this case, if you want to print as you wish, try to print first one function, and then the other:

cout << add(10.0f, 20.0f) << endl;
cout << add(10, 20);
melchor629
  • 212
  • 3
  • 10
  • Not another internal variable, it can be the same as the other one, compiler is free to choose whether the 2nd call is to be the first or 1st to be 2nd and so on. Also, it's not "internal", it's just how the generated assembly looks like, so if it stores the first one in an "internal" variable and decides that 2nd one is to be printed after the 1st one, then it'll be something like `mov REG, std::ostream; USE OPERATOR<< on REG FOR 1ST, USE OPERATOR<< ON REG FOR 2ND` –  Apr 26 '14 at 10:11
  • I know that "internal variable" is called register, but I called this way to not enter in the complicated world of assembly – melchor629 Apr 26 '14 at 10:13
  • It could've been better if you explained using C++ code, like `std::ostream& out; out << a; out << b;` –  Apr 26 '14 at 10:14