#include <iostream>
using namespace std;
void f(int& p)
{
p += 2;
}
int main()
{
int x = 10;
f(x);
int y = x + 1;
f(y);
cout << "x is " << x << endl;
cout << "y is " << y << endl;
system("PAUSE");
return 0;
}
Now the answer is that x
is 12 and y
is 15.
I kind of understand maybe that x
is 12. To explain if I got it right is that as
void f (int &p)
{
p += 2;
}
and as int x = 10
so you 10 += 2
which is 12 so x
is 12.
But I don't quite understand why y
is 15.
Is it because I use 12 as x
for int y = x + 1
so it's 12 + 1
which is 13 and then 13 += 2
which is 15?