Dynamic Programming21 sections · 916 units
Open in Course

Common Recursion Mistakes

Pitfalls to avoid

Four mistakes that trip up beginners:

1.1. Missing base case. Your function calls itself forever until stack overflow.

2.2. Base case never reached. Each call must move toward the base case. If power(22, nn) called power(22, nn+11), you'd recurse forever.

3.3. Wrong return value. Forgetting to return the recursive result loses the answer.

4.4. Modifying shared state. If your function changes a global variable, recursive calls can interfere with each other. When your recursive code crashes or gives wrong answers, check these four things first.