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 10
// adds 2
// stops and returns 12
}
Call stack
m(0) returns 0
m(1) returns 2
m(2) returns 5
m(3) returns 7
m(4) returns 10
m(5) returns 12
Explanation
m(5) is the original call. m(5) returns 12.
Navigation
Back to Step 11
Back to main example
Review recursive method tracing with AP CS Tutor Brandon Horn.