I ran this program in two different systems using Mingw compiler and Sublime Text 3 for writing code, but I got two different results:
In first system:
89842414i love 5
#include <iostream>
using std::cout;
int myfunction(int p)
{
p++;
cout << "F " << p << '\n';
return 5;
}
int main()
{
int x=7;
x++;
cout << "Ma " << x << '\n';
myfunction(x);
cout << "Mb " << x << '\n';
cout << "i love " << myfunction(42413);
}
Ma 8
F 9
Mb 8
F 42414
i love 5
This all boils down to expression cout << "i love " << myfunction(42413)
Lets write that as cout << A << B
Operator evaluation order says that it is equivalent to (cout << A) << B
That is, that cout << A evaluates before cout << B.
Good so far.
The B is an expression, whose value has to be evaluated before we can do cout << B.
However, there is no rule saying when. Only rule is that it must be within the statement and done before<< B
One system does it:
1. evaluate B
2. cout << A
3. cout << value_of(B)
The other:
1. cout << A
2. evaluate B
3. cout << value_of(B)
Both are valid. Your function has a side effect with unpredictable manifestation.