Reputation: 2445
I have a function that takes one argument by address and changes the value of the argument. But when I call the function, the order of execution is different in cases of addition and subtraction:
#include <iostream>
using namespace std;
int fun(int *i) {
*i += 5;
return 4;
}
int main()
{
int x = 3;
x = x + fun(&x);
cout << "x: " << x << endl;
int y = 3;
y = y - fun(&y);
cout << "y: " << y << endl;
return 0;
}
The output of the above code is:
x: 12
y: -1
which means x
is first changed in the function and then added to itself (I expected x
to become 7
), but the changes on y
don't affect the y
before subtraction operator.
Why is this happening?
Upvotes: 0
Views: 35