public int mystery(int b)
{
if (b == 0)
return 0;
if (b % 2 == 0)
return mystery(b - 1) + 3; // call 1
else
return mystery(b - 1) + 2; // call 2
// finished call 2, got back 0
// adds 2
// stops and returns 2
}
Call stack
m(0) returns 0
m(1) returns 2
m(2)1
m(3)2
m(4)1
m(5)2
Explanation
After m(0) returns, control returns to the topmost method on the stack that has not yet returned, which is m(1).
The stack (in the previous step) shows a 2 next to m(1), so call 2 just finished. call 2 returned 0. When a method returns a value, the return value is used in place of the method call.
The statement return mystery(b - 1) + 2;
becomes return 0 + 2;