Originally posted by Arun Kumarr:
int method1(int x){
return x++; //doesn't generate a compiler error. After returning x, value
is incremented . (Does it really increment? or the
compiler doesn't bother to do it.)
}
Hi Arun,
Well, the syntax of the return statement is return(<expression>
<expression> can be any valid arithmetic expression which should evaluate to a value.
x++ is a valid arithmetic expression. Here in return(x++), the operation of incrementing and assignment are being performed in one single step.
Of course the compiler increments the value of x, but x++ means that the old value of x is first returned and then incremented.
That's why you get the same result and do not see the incremented value.
Hope this helps.