Recursion
Pronunciationrih-KUR-zhun
Definition
When a function calls itself to solve a smaller version of the same problem, until it reaches a simple base case.
Where you hear it
Algorithm courses, interviews, and tree or folder traversal.
Examples
Walking through folders is a classic use of recursion.
The recursion has no base case, so it crashes with a stack overflow.
The recursive function sums a nested list by summing each sublist first.
Common mistake
Forgetting the base case. Without it the function never stops.
Don't confuse with
Recursion calls itself repeatedly until a base case is met, while a loop repeats a block of code using a conditional statement.
Say it at work
- In a meeting or chat
I think we can solve this tree traversal problem cleanly using recursion instead of a complex stack.
- In an email or ticket
Please make sure to add a proper base case to this recursion to avoid any stack overflow issues in production.
Keep exploring
More in Programming Fundamentals