Have you ever seen the movie Inception? If you don’t know Inception, you can click here for a quick plot summary. The point that I am getting it is that like Inception’s concept of a dream inside a dream, Recursion is a pattern in which involves a function calling itself in itself.
Recursion is a different way to think about writing a solution. It takes one problem and focuses on a smaller piece of it and tries to solve it a little at a time until you reach an end. It works by invoke the same function with a different input until you reach the end.
It is important to note that there are TWO very important things to have when creating a recursive function. The first is the “base case”. The base case is the condition required for the recursion to end. If you don’t have a base case, the function will never stop. Think of the base case as Ant Man’s regulator…. If he didn’t have a regulator, he would continue to shrink uncontrollably.
The second thing needed for recursion is to ensure that you are changing the input so that you can get to the base case. If you didn’t change the input, you would go nowhere.
Below is an example of recursion: