How recursion works Intermediate
Theo Brooks
Curious · Analytical · Clear
Trace a function that calls itself, spot what stops it, and follow each answer back to the call that needed it.
What is covered in this lesson
- Identify which path a call takes by testing its own input.
- Explain why the smaller call asks for precisely the missing factor.
- Track the separate unfinished calculation belonging to each waiting call.
- Verify that this factorial rule reaches a direct answer on every allowed input.
- Trace a returned value through the saved calculations all the way to the original caller.
Questions this lesson answers
When does factorial stop calling itself?
If this call’s n is zero, return 1, and otherwise call factorial again.
Why multiply a smaller factorial by the input?
For any positive whole number n, keep n outside and the remaining product is (n − 1)!.
What does a call keep while it waits?
A new call gets its own frame, so waiting calls keep their inputs and unfinished work.
Why does factorial stop making smaller calls?
The trail from 2 to 1 to 0 is three calls deep because 0! = 1 needs no further call.
Who gets a finished call’s answer?
Each result fills just its caller’s blank, until factorial of 2 gives the original caller its answer: 2.
Transcript
How factorial chooses a path
When you call factorial with 2, that number becomes this call’s n. It decides which of 2 paths the call takes.
Your last line asks for the factorial of 2, so n is 2, and that’s the number this call checks. In the zero test, the double equals sign asks whether n has the same value as 0. The double equals sign gives true only when those values match. Here it compares 2 with 0, so the result is false because those numbers don’t match.
Notice how return 1 sits farther to the right than the if line, so it belongs only to the true path. So the marker skips return 1 and follows the false path to the product line, which returns n times the factorial of n minus 1. That’s the recursive case because factorial calls itself to find its answer. In this recursive case, the product has to wait for that other call’s answer.
Look at the input that product needs next: 2 minus 1 is 1. So the current call asks for the factorial of 1, and its n is 1. The marker goes back to the zero test, which now compares 1 with 0 and gives false again. Since there’s no match, the marker skips return 1 and follows the false path to the product line for another recursive case.
Now look at the input this product needs: 1 minus 1 is 0. So the current call asks for the factorial of 0, with 0 as its n. The marker goes back to the same zero test, and this time 0 equals 0, so the result is true. Now the marker follows the true path to return 1, so this call ends with the answer 1. This is the base case because it gives an answer before the call reaches the product line that would call factorial again. Here the base case gives back 1 even though its input was 0. So each call tests its own n: if it’s 0, the call answers right away, and if it isn’t, factorial calls itself again.
The factorial recurrence
Let's open up 2 factorial and keep the 2 outside so you can see what the smaller call needs to supply.
See how writing 2 factorial as 2 times 1 lets you keep the 2 outside? That leaves the next number down, and since 2 minus 1 is 1, that's the only factor you still need. You can write that last factor as 1 factorial because its whole product is just 1. So 2 factorial equals 2 times 1 factorial, with the smaller factorial supplying just the 1, not the 2 you kept.
Now instead of starting with 2, pick any whole number greater than 1 and use n for that input. In n factorial, n is the largest factor, so that's the one you keep outside. Since you kept only the largest factor, the remaining factors start at n minus 1 and run all the way down to 1, each appearing just once. So you can write the remaining product as the factorial of n minus 1 because you've got every factor it needs.
Now take the smallest positive input: in 1 factorial, the only factor is 1. Keep that 1 outside and there's nothing left to multiply, so whatever stands beside it must leave it unchanged. Since 1 minus 1 is 0, what you multiply by here is 0 factorial. And 0 factorial is 1, so 1 times 0 factorial still equals 1 factorial.
So the split works for every positive whole number: n factorial equals n times the factorial of n minus 1. That's a recurrence because it tells you how to find one answer using the answer for a smaller input. The recurrence tells you exactly which smaller factorial to ask for. In the return statement, asking for the factorial of n minus 1 gives you the product of everything except the factor you kept. So multiply that answer by n and you've got the full product back, with nothing missing and nothing counted twice.
What a waiting call keeps
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.
Why factorial stops
To find the factorial of 2, the call on 2 starts a call on 1. That smaller input is your clue to why factorial finishes.
Look at the call on 1: its input is closer to zero because 2 minus 1 equals 1. That's still positive, so the call on 1 waits while it starts the call on 0, because 1 minus 1 equals 0. Subtracting exactly 1 means you can't stay put, and even from the smallest positive whole number, you land right on zero instead of skipping past it.
Now the call on 0 passes the test for a zero input, so it stops the call trail here and answers directly. Only a positive input needs a smaller call, so there's no fourth call on minus 1. Count the calls that haven't finished: the first 2 are waiting while the third is about to answer. The most calls open at once, counting the one you started with, is the maximum call depth. So for the factorial of 2, the maximum call depth is 3.
So the call on 0 gives its answer straight back: zero factorial equals 1. That 1 comes from the rule itself, so there's nothing a smaller call needs to find.
From 2 it took 2 steps down, and that works for any positive whole number because each step takes 1 off the distance left to zero. If you start at zero, you already have the answer without a step down. With that answer, the call on 1 finishes because 1 times 1 equals 1, then the call on 2 finishes because 2 times 1 equals 2. Follow the call trail back: each step down added just one call, so there are only so many calls to finish. So you reach zero and work your way back in finitely many steps, and finishing in finitely many steps is what termination means. That's why termination is guaranteed for factorial whenever you start with a whole number from zero up.
Where a return goes
The 1 is leaving factorial of 0, but where does it go when both multiplications are waiting for a number?
That 1 is the answer from factorial of 0, but it’s not the answer to your original request. Each call has its own row to remember its input and where it paused, and that record is its call frame. The call frame for factorial of 1 keeps its multiplication separate from the one below. Now the frame for factorial of 0 leaves as its 1 fills the blank in factorial of 1. So the top call is done, factorial of 1 has 1 times 1, and factorial of 2 still needs a number to multiply its 2 by. Factorial of 1 is the immediate caller because it asked for factorial of 0 directly. Only the blank in the immediate caller needed that answer. That 1 is the return value, the answer a finished call gives back to its caller. Factorial of 2 is waiting for a different call’s return value because it asked for factorial of 1.
Look at the first multiplication: the 1 on the left is the number this call kept, and the 1 on the right is the answer it needed. So factorial of 1 can finish: 1 times 1 equals 1. It’s the same number, but now it’s this call’s own answer.
Now the frame for factorial of 1 leaves as its 1 fills the blank in factorial of 2. So factorial of 1 is done, and factorial of 2 has 2 times 1. Only the blank needed that answer, because this call already had its own 2.
Now factorial of 2 can finish its multiplication: 2 times 1 equals 2. Its caller needs that whole product, not just the 1 it received.
Your original assignment asked to store factorial of 2 in answer, so it needs the result of that call. Now the frame for factorial of 2 leaves as its 2 takes the call’s place in the assignment. So answer holds 2, and no factorial call is left waiting. Each call gives its result only to its immediate caller, so each multiplication had to finish in turn before you could get your answer.