An Introduction to Recursion in Programming
By: Steven Parsons
Tags: JavaScript, Software Development, Coding, Flatiron School, Codingbootcamp

An Impossible staircase that leads to itself in either direction

If you have been coding for a bit, you may have used or written several recursive functions without knowing it, and this is a concept that is important to understand as you begin to work with more complex data structures. Recursive functions are everywhere in coding! Why are they used so frequently? One reason is it can be a cleaner alternative to iterators like loops. In fact, common functions like JSON.stringify and DOM traversal often run recursively.

So, what is a recursive function? A recursive function is a function that invokes itself. Pretty simple right? There’s only two basic rules that must be met to write a proper recursive function. The first rule is the function must have a base case — a condition in which the recursive action in the function stops. The second rule is, the function must change its input before calling itself again in order to reach the base case. Without these rules, recursive functions would get caught in an infinite loop, like that first picture of Penrose stairs.

Let’s take a look at some javascript functions to illustrate the point. If you wanted to write a function that when given a positive number, returns the sum of the range between one and that number, you could write a non — recursive function like the following.

function sumRange(num){

let total = 0

while(num > 0){

total += num

num--

}

return total

}

This works! It also fills one of our rules, the loop breaks once a condition is met. However, the function does not call itself. Let’s look at a recursive version.

function sumRange(num){

if(num === 1) return 1;

return num + sumRange(num-1);

}

In this example, the function runs, and if the input number is greater that one, it adds the input number to the return value of another occurrence of the function, ran again with an input 1 less that the previous input. This adds each recursion of the function to the top of the “call stack” (the list of functions currently being ran from top to bottom), with each instance of sumRange waiting to add its input to the return value of the next sumRange. Once this recursion has gone all the way down to calling sumRange(1), we get our first return value, which is 1. This allows the next occurrence of sumRange down the call-stack to receive the 1, and return 2 + 1, which allows the next one to return 3+(2+1) and so on, until we get our final sum.

Photo by Kelly Sikkema on Unsplash

This is a basic introduction to recursive methods and hopefully you can understand, recognise, and implement some recursion in programming. The most important take away when dealing with recursion is that you need to have a base case (a condition that breaks the recursion) and a change of input within the function to move towards the base case. Without doing this, you will add too many function calls to your call stack. This will result in and error stating that your maximum call stack size is exceeded, known as ‘blowing your stack’ or most commonly referred to as phrase every programmer has heard before, “stack overflow”.

I hope you have found this useful and continue to learn more about recursion and how it fits into more and more complex functions.