Recursion to Iteration
A tail call is a loop in disguise: replace the call with an update of the arguments and jump back.
Decision · step 2 of 8The Call Stack: Tail recursion reuses the frame
factTail(5, 1) calls factTail(4, 1 x 5 = 5) and has nothing to do with the result but pass it on. The frame is reused: same slot, new arguments.
What you will see
The same algorithm with the call stack and with an explicit stack, moving in lockstep.
Cost
| Space | O(depth) |
|---|
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input.
Screen readers: Each frame announces its function, arguments and depth; each step announces a call or a return with its value.
Reduced motion: Frames appear and disappear with crossfades instead of sliding.
Before this
Related
Taught by the same lesson
The Call Stack covers these too, in the same run.