Recursion is when something repeats itself in a clever way, just like a story that keeps telling the same kind of story inside it.
Imagine you're stacking plates. You put one plate on top of another, and then another, each time, you do the same thing: place a plate on top. That’s recursion! It's not magic; it's just doing the same action over and over again, but each time with one less plate to go.
Like a Russian Doll
Think of Russian dolls, those little nested figures where you open one, and inside there's another, smaller doll. You keep opening them until you get to the tiniest one. That’s recursion too! Each doll is like a step in a bigger story: open this one, then do the same with the next.
A Simple Example
Let's say you want to count down from 5. You say "5", then you count down from 4. Then from 3, and so on until you get to 0. That’s recursion, each step calls for the same action, just with a smaller number.
You're doing it all the time! Just like stacking plates or opening dolls, no magic needed, just repetition with purpose.
Examples
- A child asks their parent for a cookie, and the parent says they'll get one if the child asks their grandparent, who then asks the great-grandparent, creating an endless chain of requests.
Ask a question
See also
- What are persistent data structures?
- What are recursive calls?
- How Does Recursion in 100 Seconds Work?
- What is recursion?
- How Does Intro to Algorithms: Crash Course Computer Science #13 Work?