Problem regarding calling a function multiple times in the same line

Hi guys :) I have a question regarding multiple function calls within the same line.

#include<iostream>
using namespace std;

int check();
int a = 0;

int main()
{
cout << check() << " " << check() << " " << check();
return 0;
}

int check()
{
a++;
return a;
}

I thought the output would be:
1 2 3
but when I executed the program, it showed:
3 2 1

In my opinion, the first time check() gets called, the value of a should get increased by 1 and check() should return 1 and so 1 2 and 3 should come in that order. What's going wrong here? Is this something that's function specific?
Last edited on
The C++ standard does not specify the order of the function calls. It's up to the compiler to decide.

If you want guarantee the order you should split it into multiple statements.
1
2
3
4
int v1 = check();
int v2 = check();
int v3 = check();
cout << v1 << " " << v2 << " " << v3;
Last edited on
Peter87, I tried iniitializing v1, v2 and v3 in the same line, like this:

int v1 = check(), v2 = check(), v3 = check();

the output screen showed:
1 2 3

I understand that you said that the compiler decides the order of function call in case of one function being called multiple times in the same line but then why is it different for a "cout" statement and an assignment statement? Is the order random? If so, then why is it the same for all the cout statements in which I've tried calling a function multiple times in different programs?
Last edited on
why is it different for a "cout" statement and an assignment statement?

because the functions are called in a definite order and the variables assigned accordingly.
@mutexe
I understand your point... but in the cout statement, the order of the function call is definite(in writing the code) as well and to that Peter replied that the actual order is decided by the compiler. So because the the three function calls are in the same assignment statement, shouldn't the order of writing be irrelevant?
Or is it that " int v1 = check(), v2 = check(), v3 = check(); " are actually 3 different statements?
It's not about lines. You can put the whole program on the same line if you wanted and it shouldn't make a difference.

It's when you have multiple function calls in the same expression like that. But you need to check the rules for all operators involved because some do have a guaranteed order. When using the && the right hand side is guaranteed to run after the left side (but if the left side evaluates to false the right side will not run at all).
In int v1 = check(), v2 = check(), v3 = check();, each call to check() is a full-expression.
A full-expression is an expression that is not a sub-expression of another expression. Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.

In std::cout << check() << " " << check() << " " << check();, each call to check() is a sub-expression.
These are function calls, and they are indeterminately-sequenced; they may be evaluated in any order.

In int i = 6 ; std::cout << ++i << " " << i ;, ++i and i are sub-expressions.
int is a scalar type, and these evaluations are unsequenced. This engenders undefined behaviour.

The details: http://en.cppreference.com/w/cpp/language/eval_order
I see. The reference link is definitely a big help... Just one thing though, on the details page it has been written that the order may get changed the next time the program gets executed.. but in my case, no matter how many times I've executed the program, the order has remained the same. Why may that be?
Just because something might happen, doesn't mean it will happen on your particular platform, compiler, implementation etc.
Last edited on
I see. Thank you guys, for all your help. Much appreciated :)
> the order may get changed the next time the program gets executed.

The standard allows it, but it does not insist that it must be changed.
On typical implementations, it wouldn't change till the program is re-compiled.

(Note: The IS does not prohibit just-in-time compilation of source code by an implementation.)
Topic archived. No new replies allowed.