Recursion is a method to solve problems that consists of solving smaller portions of the same problem until you solve the original larger problem. It usually involves calling the function itself.
A method or function is recursive if it can call itself directly, as follows:
function recursiveFunction(someParam){ recursiveFunction(someParam); };
A function is also called recursive if it can call itself indirectly, as follows:
function recursiveFunction1(someParam){ recursiveFunction2(someParam); }; function recursiveFunction2(someParam){ recursiveFunction1(someParam); };
Let's suppose we have to execute recursiveFunction
. What would the result be? In this case, it would be executed indefinitely. For this reason, every recursive function must have a base case, which is a condition in which no recursive call is made (a stop point) to prevent infinite recursion.