‘To understand recursion, you must first understand recursion’ — Recursion Explained

Leonardo Guzman
4 min readJan 17, 2018

--

There is an old joke about Recursion:

‘To understand recursion, you must first understand recursion’

When I first read the joke, I didn’t understand it because I didn’t understand recursion! Now that I feel I have a grasp on recursion, it is funny! (Yes, its a nerd joke and I like nerd jokes)

There seem to be lots of concepts in programming that for some reason are explained in more complicated ways than they need to be.

Recursion seems to be one of those things. Here is Recursion according to Wikipedia:

Recursion is one of those words that if someone said it to me before I would probably respond with “What did you call me?”

Many people think Recursion is hard, I myself included. The reason we think its hard is not because it is. It is because many of the lessons on Recursion use the same explanations as an example : Fibonacci Numbers, Factorials etc..

If you have taken a CS course or a Coding Bootcamp and you seen Fibonacci for the first time you were probably like ‘What the heck am I looking at?’

Mattias Petter Johansson — Fun Fun Function

When somebody tries to explain recursion to you using Fibonacci numbers, you must murder them — Mattias Petter Johansson

So what is Recusion?

Recursion is when a function calls itself, until it doesn’t.

That is it!

How I felt after learning WHAT recursion is

To quote one of my favorite anime’s:

Having strength is one thing, but knowing how to use it is another! — Roa

So yeah, I know what is now. But how do I use it?

This example will be…. MATH! ‘AARRGHHH’ you say! But its basic math , subtraction. No modulo, no adding last 2 numbers, or what numbers are what. Simple.

The function below is recursive:

Recursion!

Lets break this down..

We create a function called countDownFrom:

We want the function to count down from what number is provided, down to 0, outputting each number to the console.

We will output the number; and countdown 1 from that number; output that and then countdown 1 from that number, until it is done. This is called the Recursive Call. We are using the the function countDownFrom(num) to call itself countDownFrom(num)-1. So the first part of Recursion is done .(Recursion is when a function calls itself)

If you run this code you will see that it works but will go on forever or until you get the RangeError: Maximum call stack size exceeded error, mine stopped after the number -17606

Range Error for not stopping the Recursive Call

Now lets add our stopping point, which is called the Base Case, which is our second part of Recursion(until it doesn’t)

Adding the Base Case, if(num === 0) return , will stop the recursive call when the number equals 0. Now our Countdown Recursive Function is working.

Recursion is when a function calls itself, until it doesn’t.

I am still getting the hang of Recursion but this explanation makes a lot more sense than what I have seen out there before.

Special thanks to Mattias Petter Johansson on the great walkthrough of Recursion that made this post possible! If you never seen his videos take a look. He is great at explaining programming concepts!

Till next time,

Happy Coding!

--

--