Recursion
It's time for something both dangerous and powerful that you can do with functions. It's called recursion.
Calls Itself
function myself() {
myself();
}
Recursion happens whenever a function calls itself.
You can clearly see where that could be dangerous. With this function, the program would just use up all of the available memory and run forever or until an error occurred.
Base Case
function countUp(num, max) {
if (num > max) return;
console.log(num);
countUp(num+1, max);
}
Every recursive function needs a base case.
A base case is a statement that exits the function when a condition is met...Can you identify the base case in the code above?
That's right. It's the statement with the return.
When our program runs, each one of the function coopies will create a set of internal variables called the stack.
Without a base case, javascript will continue to ask for new variables until it either runs out of memory or the browser runs into an internal limit.
That particular error is known as the Stack Overflow...which is also the name of a great site where you can ask other programmers questions.
You might be wondering if it wouldn't be easier to run a loop and in this case, you'd be right. Let's take a look at a more complex example.
Fibbonacci
let fibby = function (count) {
if (count===1) return [0, 1]
let arr = fibby(count-1)
let sum = arr[arr.length -1] + arr[arr.length - 2]
arr.push(sum)
return arr
}
fibby(8);
This is a classic interview question. How to build a fibbonacci sequence and recursion is the perfect solution.
You know if you did ask yourself, why not do the previous problem with a loop, you were on the right track. Recursion acts like a backwards loop that uses the power of closures in an interesting way.
Before we get started going through what's happening here...can you identify the base case here?
Right you are. The return statement gives it away.
A Fibbonacci sequence is a set of numbers where the next number is equal to the previous two numbers. It's a pattern that is common in nature and mathematics.
Let's walk through a version of this code next.
I added a couple more console log statements so we can see what's happening. I'll start from the bottom.
We call the function and it starts going through the sequence.
The base case checks to make sure that this isn't the last number. Recursion is basically like a backwards loop that exits on a return statement.
The reason it's so cool is that every function can remember what came before it and do something with the set of data gathered before.
For a fibbonacci sequence, I have to take the last two numbers and add them together.
The first thing that happens is that we create a new fibby function with a decreased count. We'll keep doing that until the count reaches one and then our base case will kick us out of our program.
The second time we create the function it will have a count of seven...and so on and so forth.
You can see in our log, that basically we quickly create 8 copies of this function...each with a different index.
So Fibby 8 is going to create Fibby 7, then it's going to do some calculations that adds the previous two numbers in our array and creates an array with the new number, then return that as a new array.
The next time we run Fibby, it does the same thing. In essence every new copy gets a version of the array with the two numbers added up.
It's a bit tough to get your head wrapped around this. If you want to see another way more complicated example of how recursion work, take a look at this video in one of my courses. It creates a visual version of a popular recursion problem called the eight queens problem.
That clinic problem has a more practical way of looking at recursion that also deals with how to visually display what's happening.