Discover more from Richard’s Newsletter
Recursion, Recursion, Recursion
What is recursion?
We made it to another week! Yeah, I know, I didn’t think we’d get to the third release of this newsletter either. But I am glad that we did because today, we are going to quickly learn about recursion.
I made this tweet sometime ago stating how I won’t be explaining recursion to a five year old for free. The good thing is that I don’t have to. @otty_sunny sent me a DM with a very simple explanation of it. Thus, this newsletter.
Only a few people have known the frustration of being caught in an endless stack of dialog boxes on a windows computer. If you haven’t, see the image below. We can see that we have a stack of boxes supposedly doing the same thing, to get to the bottom one (the one that popped up first), we have to close all the top ones. This is the idea of recursion.
In recursion, we perform a large task by completing smaller similar tasks.
As a five year old, one of the things you may find very interesting is ice cream. Apropos of that, if you find an ice cream truck, the first thing you want to find out is “How many people are in front of me?”
If you’re older, then standing in a long line isn’t something you fancy as well. What’s the first thing you do when you get to a line? You try to find out how many people are in front of you. Always. We don’t necessary do anything with that information but we just have to know.
As a five year old, how would you know how many people are in front of you on the ice cream line? We are going to assume that no one knows their place on the line.
You could walk down the line and count. You are more likely to do this. However, you could just ask the person in front of you. Since the person in front of you doesn’t know their number as well, they’d ask the person in front of them. You see where I am going with this? At some point we would have asked everyone on the line. When we get to the first person, there is no one in front to ask. Voila! We have the number 1 on the line. Number 1 then tells the person behind him “Oh, yeah, I am number 1”. Which implies the person behind is number 2, so he/she tells the person behind his/her own number. You see where I am going with this? I know you see it. At some point, everyone would have known their number going backwards till it gets to you.
Recursion works because we have a big task (knowing how many people are in the line) that we can break down into smaller similar tasks (each person ask the next person) and get the result from the cumulation of all the smaller results. The base case is how we know we are at the end of the recursion (the first person in line).
The base case (first person in line) stops us from infinitely asking who is the next person.
Recursion is technically defined a process in which a function calls itself directly or indirectly. Recursive functions are thus functions that call themselves.
More can be found about recursion and its use cases below:
Thank you once again for reading. For those who did not clearly understand recursion, I hope this helps.
If you are a subscriber, feel free to respond to any of my newsletter with a suggestion of what you want me to explain next. I am looking forward to writing about something you are excited about.