public int mystery(int b)
{
if (b == 0) // checks if 4 == 0, which is false
return 0;
if (b % 2 == 0) // checks if 4 is even, which is true
return mystery(b - 1) + 3; // call 1
// stops at call 1 and calls m(3)
else
return mystery(b - 1) + 2; // call 2
}
Call stack
m(3)
m(4)1
m(5)2
Explanation
The new call to m(3)
is added to the top of the stack. m(4)
is suspended waiting for m(3)
to return. m(5)
remains suspended waiting for m(4)
to return. Only the topmost call on the stack that has not yet returned is active.