Let’s talk about recursion, baby!

Let’s talk about you and me.

Robert M Ricci
3 min readFeb 16, 2021
Photo by Cookie the Pom on Unsplash

Actually, we aren't really going to talk about you and me(I), I just thought it would be funny to say that, you know like the song…no?..man I feel old. So, in this week's post, I’m going to go over recursion. What it is, how it used, and why we should use it when we can. For the examples, I’m going to be using JavaScript.

So first what is recursion. Simply put, it is a process or function that calls itself. This seems pretty straightforward but doesn't really tell us anything. More specifically it is a process of solving a problem where the solution depends on solution smaller versions of the same problem. Here is an example:

function countDownTo(n) {
if(n <= 0){
console.log("fin")
return;
}
console.log(n);
n--;
countDownTo(n);
}countDownTo(5)
5
4
3
2
1
fin

This functions as the name suggest counts down from a number, when it gets to the end it prints “fin”, like one of those French movies. To briefly explain what’s going on I will add in some comments.

function countDownTo(n) {
if(n <= 0){ //First the function tests if the number
console.log("fin") //less than or equal to zero. It's not
return; //so it moves on.
}
console.log(n); //Here it will print n
n--; //Here it subtracts 1 from n
countDownTo(n); //Here it repeats the process until n
// is less than or equal to zero.
}

There are a few important things to point out, that are necessary for a recursive function. Those are:

  • Base Case — otherwise known as the stopping point.
  • Changed Input — the input that is being changed(duh)
  • return — puts a hard stop on the function.

BASE CASE & RETURN

The base case and return are going to be the part of the function that caused the function to stop. In our example above that is this part:

if (n<=0){
console.log("fin");
return;
}

This bit of code is going to check if the number is less than or equal to zero. If it is it we execute the very next line, which is return. That will end the function, and print “fin. If it’s not then it will continue to the section after that and print a number.

CHANGED INPUT

This part of the code is what we are waiting to be changed before we can move on. In the case above it's just n -1. In the next example, it will be a little more involved. This example is known as factorial. Which is where you multiply all the numbers leading up to your stopping point.

3! // would equal 1 * 2 * 3 = 6function factorial(n) {
if(n==1) return 1; // base case
return n * factorial(n-1); // changed input
}

Let’s go a little more in-depth and explain how this is going to work exactly. First, we will explain the base case.

function factorial(n) {
if(n==1) return 1; // Here we are setting one as the
stopping point. Otherwise if we
let it get to zero, everything
would be erased, because
anything multiplied by zero is
zero.
}

Now the second line is a little more complicated. What’s happening there is that we are multiplying n * factorial(n-1). So what that means is that to get to an answer we first need the sets previous to n. Look below to see what I mean.

function factorial(n) {
if(n==1) return 1; // base case
return n * factorial(n-1); // changed input
return 1 * 1 = 1 //Because of the base case we get 1
return 2 * 1 = 2 //We have 2 in for n
return 3 * 2 = 6 //We have 3 in for n

}

CONCLUSION

I hope this gives you a little insight into recursion. There is a lot to unpack, and I hope you do a deep dive. I’m currently working on a udemy course on data structures and algorithms, which has been very enlightening. I will link it below.

RESOURCES

https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/

--

--

Robert M Ricci

Full Stack Developer Ruby and Javascript. Recent grad of the Flatiron School.