DoAssignment study guide
A3.6 · Recursion, Base Cases and Call Traces
Learn to design a terminating recursive algorithm through clear examples and targeted practice.
Ontario Grade 12 Computer Science
Programming Concepts and Skills
Follow calls down and values back up
What you will learn
- Identify the base case.
- Order recursive calls and returned values.
- Explain why the example terminates.
Set a clear precondition
Follow calls downward
Follow results upward
Return trace
| Call | Calculation | Returned value |
|---|---|---|
| factorial(0) | base case | 1 |
| factorial(1) | 1 × 1 | 1 |
| factorial(2) | 2 × 1 | 2 |
| factorial(3) | 3 × 2 | 6 |
Worked example
Trace factorial of three
Each arrow means that the parent call asks the next call to solve a smaller input. Returns travel upward in the opposite direction.
Python 3
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(3))- Descend toward the base caseCalls use n = 3, 2, 1 and 0 in that order.
- Return from zerofactorial(0) returns 1 without another call.
- UnwindThe remaining calls return 1, 2 and then 6.
Common mistakes and how to avoid them
Lesson summary
- A base case returns without recursing.
- The argument must progress toward that case.
- Return values travel back through calls in reverse order.
Check your understanding
Question 1
- 0
- 1
- 3
- It calls itself
Show answer and explanation
Question 2
- 1
- 2
- 3
- 6
Show answer and explanation
Key terms
- Base case
- The input case answered without another recursive call.
- Unwind
- Return through pending calls after the base case is reached.
Continue through ICS4U
View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation A3.6. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.