#include <iostream>
usingnamespace std;
int fun1(int p){
++p;
return p++;
}
int fun2(int &p) {
++p;
return p++;}
int main()
{
int a = 1, b , c;
b = fun1(a);
c = fun2 (b);
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
fun1 gets int argument by value, so there is local copy of p. First preincrement just add 1 to it and the second one is postincrement so returned value will not change.
fun2 gets int argument by reference, so inside this function you are working with real varialbe declared outside not a local copy of it. So returned value will be incremented by 1 same way, but postincrement in return statement will add one more 1 to referenced variable.
For initial a = 1, b and c have random values;
b = fun1(a); - a won't changed (see fun1 description), and b will be a + 1 = 2;
c = fun2(b); - b will be b + 2 = 2 + 2 = 4 (see fun2 description), and c will be 2 + 1 = 3;