← VCE Software Development U3O1

Recursion: a function calling itself

A function that calls itself with a smaller problem each time, and stops at a base case that returns without calling itself. With no base case, Python raises a RecursionError.

Understanding Recursion: a function calling itself in VCE Software Development U3O1

What it does: The if n == 0 line is the base case that returns without a further call, and countdown(n - 1) is the recursive case with a smaller number. A recursive function calls itself. The first one calls countdown(n - 1) inside its own body, but the second only prints. Each call must make the problem smaller. Starting from a positive n, adding 1 moves away from 0 and never stops.

Common mistakes: A single = assigns a value, so it cannot be used in a condition. The base case check must use == to compare: if n == 0:. A function header must end with a colon. It should be def factorial(n):.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 142–155) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which function is recursive?
def countdown(n): ... print(n)def countdown(n): ... countdown(n - 1)
Matches
def factorial(n): return n * factorial(n - 1)
↓
There is no base case, so the function never stops and raises a RecursionError. Add if n == 0: return 1 first.
Applying
Meanings
Term
recursion
Meaning
A function that calls itself with a smaller problem each time, and stops at a base case that returns without calling itself. With no base case, Python raises a RecursionError.
Practice
def countdown(n):
Testing
Gaps
1
Check Answer
Spots
1def count_down(n):
2 if n = 0:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Arctic Fox' badge — Tundra Tracker, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which function is recursive?
A. def countdown(n): ... countdown(n - 1) ✓
B. def countdown(n): ... print(n)
A recursive function calls itself. The first one calls countdown(n - 1) inside its own body, but the second only prints.
2. What is the job of the base case in a recursive function?
A. It stops the recursion by returning without calling the function again ✓
B. It makes the function call itself one more time
The base case is the stopping point. It returns a value without another recursive call, so the chain of calls can end.
3. Which line is the base case of a factorial function?
A. if n == 0: return 1 ✓
B. return n * factorial(n - 1)
The base case returns straight away without calling factorial again. The other line is the recursive case.
4. Which line is the recursive case in a countdown function?
A. if n == 0: return
B. countdown(n - 1) ✓
The recursive case calls the function again with a smaller problem. Here n - 1 is closer to the base case.
5. What does Python raise if a recursive function has no base case?
A. SyntaxError
B. RecursionError ✓
With no base case the function keeps calling itself until Python hits its recursion limit and raises a RecursionError. The code is valid syntax, so it is a runtime error.

Match the Pairs

if n == 0: return 1 → The base case: it returns a value without calling the function again, so the recursion stops
return n * factorial(n - 1) → The recursive case: the function calls itself with a smaller problem (n - 1)
A countdown function stops at its base case and otherwise calls itself with a smaller number → def countdown(n): if n == 0: return countdown(n - 1)
A function with no base case never stops calling itself, so Python raises a RecursionError → def forever(n): return forever(n - 1)
def factorial(n): return n * factorial(n - 1) → There is no base case, so the function never stops and raises a RecursionError. Add if n == 0: return 1 first.
def countdown(n): if n == 0: return countdown(n + 1) → The recursive call makes the problem bigger (n + 1), so n never reaches the base case. It should be countdown(n - 1).

Key Term

recursion
A function that calls itself with a smaller problem each time, and stops at a base case that returns without calling itself. With no base case, Python raises a RecursionError.

Fill in the Blanks

1. def factorial(n): if n == 0: return 1
Hint: Type the keyword that ends the function at the base case without another call.
2. def fact(n): if n <= 1: return 1 return n * fact(n - 1)
Hint: Type the function name that the recursive case calls inside itself.
3. def total(n): if n == 0: return 0 return n + total(n - 1)
Hint: Type the operator that makes the problem smaller on each call.
4. def forever(n): return forever(n + 1) # Raises RecursionError
Hint: Type the error name Python raises when there is no base case.
5. def countdown(n): if n == 0: return countdown(n - 1)
Hint: Type the keyword that starts the base case check.

Spot the Error

Question 1
A def count_down(n):
B if n = 0:
C return
D print(n)
E count_down(n - 1)
Line B — A single = assigns a value, so it cannot be used in a condition. The base case check must use == to compare: if n == 0:
Question 2
A def factorial(n)
B if n <= 1:
C return 1
D return n * factorial(n - 1)
Line A — A function header must end with a colon. It should be def factorial(n):
Question 3
A def blast_off():
B if n == 0:
C print("Blast off!")
D return
E print(n)
F blast_off(n - 1)
Line A — The function needs the parameter n. The body uses n and the recursive call passes n - 1, so the header should be def blast_off(n):
Question 4
A def total(n):
B if n == 1:
C return 0
D return n + total(n - 1)
Line B — The base case should be n == 0 so that total(0) returns 0. Stopping at 1 returns 0 for 1, so total(3) gives 5 instead of 6.
Question 5
A def say_hi(n):
B if n == 10:
C return
D print("Hi")
E say_hi(n - 1)
Line B — n counts down from a small number such as 3, so it never equals 10. The base case is never reached and Python raises a RecursionError. It should be if n == 0:

Student with a class code? Join your class →