Today while preparing for myself a lemonade, I though: how would I explain recursive functions? I found out that I can’t think of an analogy or metaphor or whatever thing in reality to explain such a weird concept. So the question transformed into: What kind of abstract thing exists that everybody knows and allows one to create rules that, being absurd, can be accepted easily by most people? that is, dreams!
Or more exactly, nightmares. So, we can think of a recursive function with this story:
One day you fall asleep and suddenly you appear trapped on a chair. Then enters Freddy Krueger in scene with his evil claws, laughing at you and, when you are really, really, scared, he just makes you a scratch. Yup, a little scratch on your belly, it hurts, but, it wont kill you.
Fred realizes that you are not dead so he puts you some anesthesia and you fall asleep. Then you appear in the exact same room, sat down on the exact same chair, the only difference is that little scratch on your belly. Freddy enters, doing exactly de same things as before, scratching you with the same strength in the same place. Your injuries are now deeper, but they still far from killing you, so Mr Krueger decides to put you the anesthesia again, and you fall asleep, straight to another nested dream.
The process repeats once and again and again, nested dream after nested dream, the only thing that changes is you, whose injures get deeper and deeper until you are cut-half (or whenever such a repetitive action kills you).
Then Fred sees that you have passed away, “yay! my job is finished” says and then the dream collapses, and then, the dream that contained it, and the another, and the another until we get to the first layer of nested dreams and, finally, to reality.
Let’s illustrate this with a simple example:
Say we have an array like this [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
. We need to find a given number and we decided that a recursive function is the way to do it.
So, we have this:
function MyFunction(array, numberToLookFor, currentIndex = 0){
if(array[currentIndex] == numberToLookFor)
return array[currentIndex];
else{
currentIndex++;
MyFunction(array, numberToLookFor, currentIndex);
}
}
Note: For simplicity of code we are assuming that
numberToLookFor
is always present in the array.
Let’s call our function and see the events under the light of the story:
Function | Story |
---|---|
MyFunction(array, 3) |
You fall asleep. |
currentIndex++ |
Freddy scratches you. |
if(array[currentIndex] == numberToLookFor) |
Fred checks if you are dead. |
MyFunction(array, numberToLookFor, currentIndex); |
Puts you anesthesia and you fall in a nested dream. |
return array[currentIndex]; |
You are officially dead, so we are done. |
As you can see, here are the usual elements of a recursive function:
Element | Definition | Story | Code |
---|---|---|---|
A stop condition | Something that finishes the function | “You are dead” | “We found the number” |
A state | Something that changes by each call | “Your injuries” | “The current index” |
A state changer | An action that changes the conditions | “Fred scratches you” | “Add one to the current index” |
A recursive call | A call to the function | “Fred puts you anesthesia” | “The function calls himself” |
ObjectNotFoundException
)function MyFunction(array, numberToLookFor, currentIndex = 0){
//....
//Avoid this whenever possible as its life goes behod the inner function call!
var something = ....;
//Better do something like this:
if(true){
let something = ....;
}
//Or this if it is available
let(something = ...){
...
}
//Or just
var something = ...;
//do your thing with the variable....
//and then set it to null
something = null;
currentIndex++;
MyFunction(array, numberToLookFor, currentIndex);
//....
}
This was an attempt to explain recursiveness in a way that may be easier to understand, just remember, recursiveness is like a dream that sends you into the same dream and and does it again until some condition is met. I hope this post have helped you to have an understanding of recursive functions, feel free to disqus in the comments below, thanks for reading.
P.D.: This would be a little more complete version of the function:
function MyFunction(array, numberToLookFor, currentIndex = 0){
if(currentIndex > array.length)
throw "Number not found!";
if(array[currentIndex] == numberToLookFor)
return array[currentIndex];
else{
currentIndex++;
MyFunction(array, numberToLookFor, currentIndex);
}
}