What does a call keep while it waits?
What a waiting call keeps
Theo Brooks
A new call gets its own frame, so waiting calls keep their inputs and unfinished work.
Transcript
The call for the factorial of 2 starts with its own record. Watch what stays in that record.
Only the call with 2 has started, so the depth, which counts calls in this chain that haven't returned yet, is 1. So even the very first call counts toward the depth. Look at the input here: n is this call's name for the number it was given. Here n equals 2. Now the call pauses at 2 times the empty box because it needs the factorial of 1 before it can multiply. That box holds the place for the answer, so it's only the box that's empty, not the spot where the 2 goes. This whole record is a call frame because it keeps the call's values and remembers where to pick up the work. This call frame keeps the input right alongside the waiting multiplication.
To find the factorial of 1, a new call starts running above the waiting call, with n equal to 1 in the middle frame. Both calls use the name n, but the new input belongs to the middle frame, so the bottom frame still has n equal to 2 and waits at 2 times the box. Neither call has returned, so 1 waiting call plus 1 running call gives a depth of 2.
Now the middle frame pauses at 1 times the box, keeping its own input as the factor. So the box in the bottom frame stays empty because the multiplication above it isn't finished.
For the next input, 1 minus 1 gives 0, so a new call starts running in the top frame with n equal to 0. The waiting calls still count, so 2 waiting calls plus 1 running call gives a depth of 3. Read up from the bottom frame: 2 waits to multiply by its missing answer, then 1 waits for its own answer, and only the call with 0 is running. So each new call adds 1 to the depth, but because it gets its own frame, every waiting call keeps both its input and the multiplication it needs to finish.