Recursion and the Call Stack
Where does a returned value go when several calls are waiting? What makes the calls stop, and how do code order and saved values change what reaches Output?
↺
Where does a returned value go?
Case 1 of 3 · Press Run, then drag each Value to its Hole until Main finishes.
Code
One callMultiply the digits of a non-negative integer. 0 has one digit; inputs have no leading zeroes. Write a recursive routine.
Wanted
Output
Stack
0 of 6 routine framesCompiler’s call code · save Values · save Address · call · restore
Loop
One frameOutput
Calls that ran
Trace
No calls yet
A run stops after 1,000 events if it is still running. The stack holds six routine frames.
Frames are simplified. Real return addresses are machine addresses. This stack is deliberately small; the loop comparisons do not measure running time.