Jobert Manosca

Posted on

# Recursion is actually easy!

It has come to my attention that the concept of recursion is a topic that some may find difficulty in learning. I believe that this is only because it is usually taught in some weird way using some mathematics that are beyond what we would use in our daily lives. Fibonacci numbers? Factorials? I don't think I even knew what those were and I took engineering for a year!(I probably would've been a bad engineer if I stuck with it.)

REJOICE! I shall try to teach it in a way so that even the mathematically inept, such as myself, can understand. I promise you that your brain doesn't actually need to be that big to understand what it's doing.

## What is recursion?

Recursion, in the most simplest of words, is when you use a function that calls itself until something tells it to stop. That's it. So why are examples out there making you get a PhD in Math to understand what they're doing?

Here's some pseudo code I use as a template when building a recursive function:

``````function recursion() {

if(your stop condition is met) {
stop running this function
}

do some stuff

// call the function again
recursion()
}
``````

So let's actually build a recursive function! The language of choice here is JavaScript but the concept is the same for any language. Here we're not gonna use any fancy math terms and whatnot. This example will just be a function that counts down from the number we pass to it. I'm hoping everyone knows what I'm talking about when I say "count down".

JavaScript Example:

``````
// This function will count down to 1 starting from the number we give it.
function countdown(number) {

// The Stop Condition:
// Stop the recursion when the number reaches 0
if(number === 0) {
return;
}

// Print out the current iteration of number
console.log(number);

// Call Again:
// Call the function again with a minor change to the argument
// that will eventually lead the recursion to the stop condition
countdown(number - 1);
}
``````

Now let's open up our console and call the method. I passed in 10.
The output should look like this:

As you can see it's not a difficult concept to learn at all!

Recursion is indeed a powerful tool but it is not without its fair share of problems. Similar to a loop, if you do not properly set up your stop condition you will keep running that set of code. HOWEVER, unlike a loop, it will usually reach the limit of your call stack and give you an error depending on the browser or language you're using.

Chrome will say "Maximum call stack size exceeded"
Firefox will say "InternalError: too much recursion"
Edge will say "Out of stack space"

If it doesn't, then something else has gone horribly wrong. The stack is a whole other topic on its own and I suggest looking it up after if you don't already know about it.

Generally speaking, a countdown is most likely better implemented with a for loop but it's a good way in my opinion to get started with learning recursion. Since it's a waste to use recursion on simple things like countdowns let's talk about the more practical uses of recursion.

## When Should We Use it?

From my own experiences with recursion, I'd say it's best used when you need to repetitively run a set of code but are unsure of when exactly you need to stop. Here are two algorithms as examples of what I mean along with guides by Free Code Camp that give a great breakdown of how the algorithms work.

Flood Fill
Merge Sort
(The plain English of what this does is between 2 math death blocks.)

## Let's Wrap it up

I hope I managed to help some people get a better understanding of how to use recursion. Let's stay away from the crazy math stuff. When you're learning recursion for the first time, do it with a countdown so it's easier to tell if you're messing it up. That's all for now, folks. Thanks for reading!