Computer Science / Unit 2 CS 0057 worked example, 60 to 90 seconds
Running a Recursion to the Floor
The idea
A recursive sum computes the total from zero up to a number by one rule — the sum up to x equals x plus the sum up to x minus one — with zero as the base case.
In practice
Ask for the sum up to 10. The rule answers: 10 plus the sum up to 9. That call answers: 9 plus the sum up to 8. The chain keeps descending — 8, 7, 6, 5, 4, 3, 2, 1 — each call holding its number and waiting, until the sum up to 0 hits the base case and answers 0 directly, no call needed. Now the chain unwinds in reverse: 1 plus 0 is 1; 2 plus 1 is 3; 3 plus 3 is 6; then 10, 15, 21, 28, 36, 45 — and at the top, 10 plus 45 is 55. Eleven answers, one rule, and the base case did the only direct work.
Where people slip
A classroom of students each writing one line of the chain on the board sees the whole trick: every call trusts the next, and the trust bottoms out somewhere real.
Your place on the trail
Every lesson rests on earlier ones and opens later ones. Tap any to jump.
Builds on
Unlocks
- Nothing yet depends on this.
Something wrong in this lesson? Flag it · Sources for Computer Science