Exam-style question
Try this first
Consider this Python function: ```python def total(n): if n == 1: return 1 return n + total(n - 1) ``` Trace the calls made by total(4), give the final result, and explain how the answers from the recursive calls are combined.
Model answer
What a good answer should say
- The calls are: `total(4)` returns `4 + total(3)`.
- `total(3)` returns `3 + total(2)`.
- `total(2)` returns `2 + total(1)`.
- `total(1)` reaches the base case and returns `1`.
Explanation
Why this works
Each general-case call waits for the result of the next recursive call. Once the base case returns 1, the waiting calls finish in reverse order and add their own n value to the returned result.
Common mistake
No common mistake is linked to this question yet.
