First you need to know about pre increment and post increment and also addresses
I'll explain your code function by function
so in fun1
, function when you pass a (a=1)
as p
++p
is a pre increment method
it first increment p by 1 then returns p , so here p is now 2
in next line when you return p++
, p++
is a post increment method i.e. returns first then increments by 1.
so fun1 returns 2
and value of b becomes 2;
now moving on to next function
here please note that you have passed reference to p not just p i.e. if you change p
here, original p
( reference to variable you have passed) will also change
So here you do ++p
its pre increment so it increases the value of p
by 1,
please note you have passed b
as p
, value of b will also change from 2 to 3,
now in next line, you return p++
, 3 is returned as the value of fun2.
Please note p++
returns 3 first, but also increases p by 1, since you passed the reference of p as b, b increases by 1, b becomes equal to 4.
I think its clear now,