Two parts: a base case and a smaller call
Every recursive function needs a base case, an input small enough to answer straight away, and a recursive case that hands a smaller input to another call of the same function. In factorial below, n <= 1 is the base case and n * factorial(n - 1) is the recursive case: 5! is 5 × 4!, and 4! is 4 × 3!, until 1! is simply 1.
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
Going down: every call waits
A call to factorial(5) cannot finish its last line. It has to multiply 5 by factorial(4), so it starts that call and waits. That call waits on factorial(3), and so on. Each call is a separate frame with its own n: there are five different ns alive at once, one per frame, and none of them is overwritten.
Python keeps the waiting frames on the call stack. The deepest call, factorial(1), is on top. It is the only one running; the four under it are paused in the middle of their return line.
Coming back: how return values flow up
When factorial(1) reaches the base case it returns 1. A return does two things: it ends that call's frame, and it hands the value to the exact spot that made the call. Here, that is the factorial(n - 1) inside factorial(2)'s return line, which now reads 2 * 1. So factorial(2) returns 2 to factorial(3), and the answers climb back up one frame at a time:
| Call | Waits on | Then works out | Returns |
|---|---|---|---|
factorial(5) | factorial(4) | 5 * 24 | 120 |
factorial(4) | factorial(3) | 4 * 6 | 24 |
factorial(3) | factorial(2) | 3 * 2 | 6 |
factorial(2) | factorial(1) | 2 * 1 | 2 |
factorial(1) | nothing: the base case | 1 |
The value only ever goes to the caller one level up, never straight to the top. That is why a recursive call almost always needs a return in front of it, or its answer goes nowhere.
What you see in PyVisualizer
Run the example and the tool opens on its Recursion view: every call gets its own lane, labelled with its arguments, under the call that made it. Step forward and the lanes stack up; when a call returns, its lane shows the value it handed back and the caller's line continues with it. Step backwards to watch a return value un-happen. The call stack panel lists the frames waiting at each step, with their own n.
When one call makes two
factorial makes one call per level, so its calls form a line. A function that makes two calls per level, like the naive Fibonacci below, forms a tree: fib(4) calls fib(3) and fib(2), and fib(2) is worked out twice. Watching the lanes branch is the quickest way to see why that version is slow, and why memoization helps.
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
Common mistakes
- No base case, or one that is never reached. The calls never stop, and Python stops them with
RecursionError: maximum recursion depth exceeded. Check that every call's input is smaller than its caller's. - Calling without returning. Writing
factorial(n - 1)on its own line works out the answer and drops it; the function then returnsNone. The caller gets the value only throughreturn. - Expecting one variable. Each frame has its own locals. Changing
nin a deeper call does not change thenits caller is waiting with.
Try it yourself
- Change the input to 3 and count the frames before the first return.
- Paste your own recursive function, such as a sum of a list or a binary search, and run it on a small input.