

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Recursion
incomplete
2: Recursion Review
incomplete
3: Zipmap
incomplete
4: Recursion Quiz
incomplete
5: Nested Sum
incomplete
6: Recursion Review
incomplete
7: Recursion on a Tree
incomplete
8: Dangers of Recursion
incomplete
9: Recursion Practice
incomplete
10: Recursion Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Recursion is great because it's simple and elegant (simple != easy). It's often the most straightforward way to solve a problem. But there are some dangers to be aware of:
Stack overflow: Each function call requires a bit of memory. So if you recurse too deeply, you can run out of "stack" memory, which will crash your program. (This is what the famous website is named after.)
If you don't have a base case, or your recursive calls never reach it, the function can keep calling itself until Python raises a RecursionError for exceeding the maximum recursion depth.
Especially in a language like Python, recursion is often slower than a for loop because each function call adds overhead. Tail call optimization can help with this, but Python doesn't support it.