01A smaller copy of the same problem
A recursive function solves a problem by calling itself on a smaller input. Every recursive function has two parts:
- Base case: an input so small the answer is known directly.
fact(1) = 1. - Recursive case: reduce the problem and combine.
fact(n) = n × fact(n−1).
Think of it like nested boxes: fact(4) contains a call to fact(3), which contains fact(2), down to fact(1), which answers immediately. Then the answers travel back out: 1, 2, 6, 24.
The trick is to trust the smaller call. When you write n × fact(n−1), assume fact(n−1) already works and only ask: does my step turn its answer into mine?