ArticlesLinear structures

Linear structures

The call stack

The stack the runtime uses for calls. Each call pushes a frame. A return pops it. Recursion is this structure.

The call stack is the stack you do not manage yourself. Each function call pushes a frame: the arguments, where to return, and the local variables. A return pops that frame. Recursion is the same structure, with your own function as the thing being pushed.

This is why a missing base case does not fail in quite the same way as a while loop that never ends. It pushes frames until the runtime refuses to push another one. The overflow is a stack running out of room. An explicit stack in your own code can do the same walk, and you can see the frames because you stored them.

When a recursive solution is hard to hold in your head, draw the frames. What gets pushed, what must be true when it pops, and which call is the one that stops. The call stack is doing that bookkeeping whether you draw it or not.