Understanding recursion
There is a famous programming wisdom quote that says:
“To understand recursion, one must first understand recursion.”
- Unknown
Recursion is a method to solve problems that consist 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); }
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...