The printable companion to the recursion lesson: definitions to learn, annotated code, a blank trace table to complete, and practice questions with worked answers at the end. Every lesson in the library has a handout built like this one.
Print this page (Ctrl/Cmd + P) to hand it out on paper.
A problem-solving technique where a function calls itself to solve a smaller instance of the same problem.
The condition under which the function returns a value directly, without calling itself again. This is what stops the recursion.
The part of the function that calls itself with a smaller or simpler input, moving closer to the base case each time.
The memory structure holding one frame per function call that has started but not yet finished.
One call's block of memory on the stack, storing its local variables and the line to return to.
The Python exception raised when recursion depth exceeds the interpreter's limit — almost always a missing or unreachable base case.
def factorial(n): if n == 0: # base case return 1 # recursive case: return n * factorial(n - 1) print(factorial(5)) # → 120
Trace factorial(4) by hand. Fill in what each call returns. Remember: nothing is multiplied until the base case has returned.
| Call | n | Calls next | Returns |
|---|---|---|---|
| factorial(4) | 4 | factorial(3) | |
| factorial(3) | 3 | ||
| factorial(2) | 2 | ||
| factorial(1) | 1 | ||
| factorial(0) | 0 | — (base case) |
How many stack frames exist at the deepest point?
State the two parts every recursive function must have, and state the purpose of each.
A student writes a recursive function to reverse a string but omits the base case. Describe what happens when the function is called, and outline the fix.
A programmer replaces a working loop that sums 100 000 numbers with a recursive function. Explain two disadvantages of the recursive version.
Construct a recursive Python function count_down(n) that prints every integer from n down to 1, then prints "Done".
RecursionError is raised (1). The fix is to add a base case returning the string itself when its length is 0 or 1, before the recursive call (1).RecursionError and produces no result, while the loop completes (1).def count_down(n): if n == 0: # base case print("Done") return print(n) count_down(n - 1) # recursive case
Close to 100 lessons across the 2027 syllabus, each with a printable handout, a unit revision booklet, and exam practice with worked answers.