What **is** recursion? 🤷🏼‍♀️
Tags: codenewbie, beginners
Published: 2021-02-10 16:08:02

I was challenged by [@100days100](https://twitter.com/100days100) to learn and write about recursion.
This is not something that I've looked into or used before..

This article will answer the question 'what is recursion?' and will hopefully give you a solid way with which to explain it to others.

**Recursion is a principle which occurs in many different languages and formats, but we are going to stick with explanations and examples using JavaScript so we don't go _completely_ mad.**

##So here we gooooooooo 🥳

Recursion (ree-curr-shon) is a Computer Science principle that is found in almost every coding language. It is a type of process or method that is used to break a problem down to solve it in smaller, repetitive ways. It's a method that calls itself.

###Like a loop?

Yeah, exactly like a for-loop, except a for-loop is a _loop_, not a _method_. 

_Remember that tech likes to be confusing..._


###So....a _method_ that keeps referring to itself...?

Imagine someone that keeps referring to themselves in the third person. 
Example: "Hi, Faye's name is Faye. Faye likes writing and learning about things that Faye doesn't understand yet - essentially Faye likes to teach people while learning."

**Yeah, sounds really weird. Give yourself a minute to come up with some funny sentences and then you'll be able to focus again.**


***************************************


**You got it out of your system?**

**Ok great, let's carry on...**


###Why would you ever need to make a method refer to itself?

Great question...
A popular 'task' in a coding quiz or even interview is:

> Create a function that returns the factorial of a given integer.

###Stop right there. What?

Ah yes, this mathematical concept that I also just learned about! So this seems to be the best way to explain recursion
'factorial' is all about multiplying numbers together
So 'factorial 6' - 6! would be 6 * 5 * 4 * 3 * 2 * 1.


You could work this out pretty easily, but what about factorial 97?

This is where a recursive function will be useful.

Your function needs to loop (yes, loop, don't @ me) around all of the numbers, **but** _(and hold on here)_ do you see the issue?

Let's take factorial 6 again - and I want you to imagine that each of the steps is a block of lego. **Also, I'm calling each answer (n). Stay with me for now, we will come back to that later.**

![image](https://dev-to-uploads.s3.amazonaws.com/i/xj8x65cxkda4d9e45edr.png) 

1. 6 * 5 = n(30)

_So here in our first block, we have the original number multiplied by itself-1_

2. 2nd block - (n) * 4 = n(120)
_So this block is **relying** on the first block because it needs to know what the first blocks' (n) answer was, and then it comes up with its own (n)_

3. 3rd block - (n) * 3 = n(360)
_This block is relying on the 2nd block for it's (n) answer, and is also, by extension, relying on the 1st block. This is why we are imagining them as lego - they all fit together and hold each other up)_

4. 4th block - (n) * 2 = n(720)
_This fourth block is relying on every other block before it for all of their versions of n, and then it's created another version!_

5. 5th block - (n) * 1 = n(720)
_Our 5th block doesn't really do much, it is just multiplying n by 1, nevertheless, we need it to finish our lego tower._

6. 6th block - n * 0 = n(0)
_Our last block feels like it's undoing all of that nice math that we were just doing, right? Yep, but that's math. Hold tight for the..._ **👀base case👀**😱

###Ok, that's factorial, what's that got to do with recursion?

The point of me explaining that was to show a recursive function, spread out like a scroll. Now imagine if you could squish **all** of those 6 lego blocks into 1.

Imagine all of the lego blocks for factorial 50, or even 700! 
Imagine it was your task to write that out in code. Sure, achievable with for loops and if/else statements but...



Think of the clean code you could have if you didn't have for loops and if/else statements everywhere.

#####Elegant 💃🏼

The recursive function that you'd need for the 6! (factorial 6) puzzle would be:


```
function factorial(n) {
  if (n < 0) return;
  if (n === 0) return 1;
  return n * factorial(x - 1);
}
```

In the above function, if you wanted to find factorial 6, you'd `console.log(factorial(6))`

_Let's break down exactly what the function would do next._

```
if (n < 0) return;
if (n === 0) return 1;
```

Well, 6 **is** greater than 0, and it **isn't** 0, so it would jump over these 2 ifs. We passed the termination condition and base case! Carry on!



```
return n * factorial(n - 1);
```

####Woah....

Yep, this is the _actual_ recursive function.

Bow down to it. This is what this whole article has been about.

What this little friend does is to say 'if you have passed the termination and base...' (we did) '...then multiply 6 by 6-1'

This function will run again and again, forever and ever, until it is stopped by the nets (the termination condition and base case. 

Our 6th lego block (the one that undid all our nice math) will be caught in the base case (because it **does** === 0) and the previous results will be logged in reverse order (like an unravelling spiral)



####References!


[Recursion In JavaScript](https://dev.to/dpkreativ/recursion-in-javascript-2ch4)

[What on Earth is Recursion? - Computerphile](https://www.youtube.com/watch?v=Mv9NEXX1VHc&t=434s)


As always, [Oliver](https://twitter.com/oliverjumpertz) and [Benedikt](https://twitter.com/Mastacheata) on Twitter