Call stack

m(9)   returns 27
m(3)   returns 9
m(15)2    15 + ____
m(5)   returns 15
m(27)2    27 + ____
m(9)   returns 27
m(45)2    45 + ____

Explanation

mystery(9) has already been called. As the stack indicates, mystery(9) returns 27.

In this method, 9 is a base case. If mystery(9) is traced again, as it was in Step 3, it would quickly return 27.

Some recursive methods repeatedly call themselves with the same value. Examples of this include CodingBat Recursion-1 fibonacci and at least 1 exercise in the Barron’s AP CS A prep book. Determining that a method has already been called with a specific value can reduce repetative work.

Forward to Step 12
Back to Step 10
Back to main example