Recursion: How a Beginner Understands It.

·

3 min read

As a developer who has ever worked with Javascript or taken a Javascript course, there is a 99.9% chance that you would have heard, learned, or written a "recursive function". For many beginners, this function may be difficult to understand at first glance. It was for me too. Hence this article. I will attempt to break "Recursion" into its fundamentals such as, "What is recursion?", "features of a recursive function, "when to use recursion, and "examples of recursive functions".

1. What is Recursion?

Recursion is a technique of writing Javascript functions. It is when a function repeatedly calls itself until it meets a "stop" condition. Recursion is a functional programming technique. Functional programming is the system of writing software functions and conditions without mutating data or causing side effects.

A function that is written with recursion is called a recursive function.

**2. Features of a Recursive Function **

There are four parts of a recursive function, sometimes merged into three depending on the problem you might be trying to solve. A typical recursive function has the following parts:

  • A function

  • A terminating condition

  • A base case

  • The recursion

A Function: This is your typical Javascript Function.

function testRecursion() {

}

** A terminating condition:** This is the condition that eventually stops the recursive function from calling itself infinitely.

function testRecursion() {
 if (somethingGoesWrong) { 
      return "stop this function"; //the terminating condition 
}
}

A base case: This is usually the goal of the function. Sometimes, the base case and terminating condition can be the same.

function testRecursion() {
 if (somethingGoesWrong) { 
      return "stop this function"; //the terminating condition 
 }
if (testIsOver){
   return "yaaaay, we're done"; //the base case
}
}

The recursion: This is where our function calls itself and repeatedly does something until one of the two conditions above is met.

function testRecursion() {
 if (somethingGoesWrong) { 
      return "stop this function"; //the terminating condition 
 }
if (testIsOver){
   return "yaaaay, we're done"; //the base case
}
   testRecursion("this is what we want to do") //the recursion
}

**3. When to use recursion **

Recursion is made for solving problems that can be broken down into smaller repetitive problems. Unlike loops that are used when the specific number of times a repetition should be done is known, recursion is best used for when the number of repetitions is based on the validity of the base case or the terminating condition.

**4. Examples of Recursive Functions **

Below are two examples of common recursive functions.

//write a javascript function to return the range between numbers x and y

var range = function(x, y) 
{
  if (y - x === 2) // terminating condition
  {
    return [x + 1];
  } 
  else 
  {
    var list = range(x, y - 1);
    list.push(y - 1);
    return list;
  }
};

console.log(range(3,10));
//calculate the factorial of a number

function factorial(num) 
{ 
  if (num === 0) // terminating condition
 {
    return 1; 
 }
  return num * factorial(num-1);  // base case and recursion      
}
console.log(factorial(6));

The first function returns the range of numbers between x and y, where x is the start number, and y is the end number. This function will continually call the recursive function until it meets the terminating condition.

The second example seeks to return the factorial of a certain number "num". It iterates through the "factorial function" until num=0, which is the terminating condition.

So, that is the fundamental structure and function of recursion broken into simple bits. For further study and more examples or challenges using recursion, check out the following:

I hope you enjoyed reading, do let me know your comments and suggestions. Thank You.