This comes up often; lots of people get confused about what i++ is. And writing 0,1 when you mean 1,0 causes even more confusion!
This is a confusing bit of code, written to confuse, and difficult to read because you haven't indented it correctly.
And is that question from a book or website, in which case you ought to Quote Your Sources.
The value of f1(i) is 0. It always is, because there is return 0; in that method.
The value of i passed to f1 is 1, which you know because f1 prints 1,
The value of i before i++ is 0, and after i++ the value of i is 1. But there are two values there. There is the value of i, and the value of the subexpression, i++. They are different. The value of i++ is the old value of i, which is 0. So you are adding 0 and 0, getting 0, then re-assigning i to 0.
Joined: Sep 03, 2007
prasanna kanth wrote:If the initial value of i = 0, then it will be 1 at the end.
No the value will be still 0 at the end.
There was a very good post about how the value of i becomes from 0 to 1, then back to 0 w.r.t memory.
It really explained well, will try to search and give you the link.
But the bottom line, i will be unchanged(here 0).
So in our case i = i++ + f1(i);
First value of i is used for the expression(ie 0), then it is incremented(i becomes 1) and passed to the method f1 which returns 0.
so the expression becomes 0 + 0 = 0 which is finally assigned back to i.