Edexcel International Advanced Level Computer Science, point 5.1.3
Definition and function
Recursion is when a subroutine calls itself. It solves a problem by reducing it to a smaller version of the same problem, until it reaches a case that can be answered directly.
Function: it gives a short, clear solution to problems that are naturally repetitive or self-similar, such as factorials, tree traversal, searching and sorting (10.4, 21.1).
The two parts
- Base case (stopping condition): a case with a direct answer, which needs no further call. It ends the recursion.
- General (recursive) case: the subroutine calls itself with a simpler or smaller input that moves towards the base case.
If there is no base case, or the input doesn't get closer to it, the calls never stop, until the call stack overflows and the program crashes.
Operation
Each call gets its own stack frame (1.2.4): its parameters, local variables and return address. The frames pile up on the call stack while the calls go down, and are removed one by one as each call returns.
Factorial
, and .
def factorial(n):
if n == 0: # base case
return 1
return n * factorial(n - 1) # general case
Hand tracing factorial(4)
Going down (calls are made, nothing is multiplied yet):
| Call | Becomes | Returns (unwinding, from the bottom up) |
|---|---|---|
| factorial(4) | 4 × factorial(3) | 4 × 6 = 24 |
| factorial(3) | 3 × factorial(2) | 3 × 2 = 6 |
| factorial(2) | 2 × factorial(1) | 2 × 1 = 2 |
| factorial(1) | 1 × factorial(0) | 1 × 1 = 1 |
| factorial(0) | base case | 1 |
The call stack at its deepest holds 5 frames (for = 4, 3, 2, 1 and 0).
Another example: sum of a list
def total(items):
if len(items) == 0: # base case
return 0
return items[0] + total(items[1:])
total([3, 5, 2]) total([5, 2]) total([2]) total([]) .
Recursive and iterative compared
def factorial_iter(n):
result = 1
for i in range(2, n + 1):
result = result * i
return result
| Recursion | Iteration (loops) | |
|---|---|---|
| Code | Often shorter and clearer for self-similar problems | Can be longer for those |
| Memory | A stack frame for every call | Little extra memory |
| Speed | Slower (call overhead) | Usually faster |
| Risk | Stack overflow | Infinite loop |
| Best for | Trees, divide and conquer, backtracking | Simple repetition |
Anything done recursively can be done with a loop (sometimes with a stack), and the reverse.
Worked example
Task: trace multiply(3, 4) and say what it does.
def multiply(a, b):
if b == 0:
return 0
return a + multiply(a, b - 1)
Down: multiply(3, 4) calls multiply(3, 3), which calls multiply(3, 2), then multiply(3, 1), then multiply(3, 0).
Up:
| Call | Returns |
|---|---|
multiply(3, 0) |
0 (base case) |
multiply(3, 1) |
|
multiply(3, 2) |
|
multiply(3, 3) |
|
multiply(3, 4) |
It calculates 3 × 4 = 12 by repeated addition: the base case is b == 0, and each call reduces b by 1, so it reaches the base case.
Exam tip: always identify the base case and show that each call gets closer to it. When tracing, show the calls going down and then the values returning up. Say that recursion uses the call stack, and that too many calls cause a stack overflow. You'll use recursion in code in Unit 2 (10.4).
Check yourself
What is a base case?
A case that can be answered directly without another recursive call. It stops the recursion.
What happens if a recursive subroutine has no base case?
It keeps calling itself until the call stack overflows and the program crashes.
What does factorial(3) return, and how many calls are made in total (including the first)?
6, using 4 calls (n = 3, 2, 1 and 0).
Give one advantage and one disadvantage of recursion compared with iteration
Advantage: shorter, clearer code for self-similar problems. Disadvantage: uses more memory (a stack frame per call) and risks a stack overflow.