AQA 7517, section 4.1.1.16
A recursive subroutine is one that calls itself. Recursion solves a problem by breaking it into a smaller version of the same problem, again and again, until the problem is simple enough to answer directly.
The two parts of a recursive solution
Every recursive subroutine needs both of these:
- A base case: a situation simple enough to answer without calling itself. This is what stops the recursion.
- A general case (recursive case): the subroutine calls itself with a smaller or simpler input, moving it closer to the base case.
Watch out: without a base case, or if the general case never reaches it, the subroutine calls itself forever and the program crashes with a stack overflow.
Example: factorial
The factorial of , written , is . So .
Notice that . In general, , and .
def factorial(n):
if n == 0: # base case
return 1
else: # general case
return n * factorial(n - 1)
print(factorial(4)) # 24
How it works: the call stack
Each call gets its own stack frame (see 4.1.1.15). Calls pile up until the base case is reached, then the answers are passed back down as each frame is popped.
factorial(4) waits for 4 * factorial(3)
factorial(3) waits for 3 * factorial(2)
factorial(2) waits for 2 * factorial(1)
factorial(1) waits for 1 * factorial(0)
factorial(0) returns 1 <- base case
factorial(1) returns 1 * 1 = 1
factorial(2) returns 2 * 1 = 2
factorial(3) returns 3 * 2 = 6
factorial(4) returns 4 * 6 = 24
There are two phases:
- Winding (going in): the calls build up on the stack until the base case is reached.
- Unwinding (coming out): the results are returned one by one, and the frames are popped.
Another example: counting down
def countdown(n):
if n == 0:
print("Go!")
else:
print(n)
countdown(n - 1)
countdown(3) # 3, 2, 1, Go!
The base case is n == 0. Each general case call uses n - 1, so it always gets closer.
Recursion versus iteration
Anything done with recursion can be done with a loop, and the other way round.
def factorial_loop(n):
total = 1
for i in range(2, n + 1):
total = total * i
return total
| Recursion | Iteration | |
|---|---|---|
| Code | Often shorter and closer to the problem's definition | Often longer |
| Memory | Each call needs a stack frame, so uses more | Uses little extra memory |
| Speed | Calls add overhead, so can be slower | Usually faster |
| Risk | Stack overflow if too deep | Infinite loop if the condition never changes |
| Good for | Problems that split into smaller copies of themselves, such as trees | Simple repetition |
Worked example
Task: write a recursive function that adds the numbers from 1 to , and trace total(3).
def total(n):
if n == 1:
return 1
return n + total(n - 1)
print(total(3)) # 6
Base case: total(1) returns 1. General case: the total up to is plus the total up to .
Trace:
total(3)needs3 + total(2)total(2)needs2 + total(1)total(1)returns1(base case)total(2)returns2 + 1 = 3total(3)returns3 + 3 = 6
Exam tip: when asked to explain recursion, name both the base case and the general case, and say that the base case is what stops it. For tracing questions, write each call on its own line and fill in the returns from the bottom up.
Check yourself
What is a recursive subroutine?
A subroutine that calls itself.
What is the purpose of a base case?
It gives a direct answer without another recursive call, which stops the recursion.
What would happen if factorial had no base case?
It would keep calling itself with smaller and smaller values and never stop, filling up the call stack until a stack overflow error occurs.
Why does a recursive solution generally use more memory than a loop?
Each call adds a stack frame holding its return address, parameters and local variables. A loop reuses the same variables.