Recursion, Recursion, Recursion
Michael Olorunnisola

Very well written article. However, I feel that there are two things that you did not address: the alternatives to recursion and the real purpose of recursion. I understand that this is designed for beginners, but I feel that these two points are worth mentioning.

To elaborate, you mentioned that recursion can cause a stack overflow, but you did not mention that most recursive functions can be rewritten as loops. A loop is typically the better solution, the disadvantage being that loops can be harder to understand in some situations. However, a loop will reuse the same variables while recursion saves new variables each iteration.

An example of loops vs. recursion:

//recursive - n variables are saved
function factorial(n){
if(n == 0) return 1;
return n * factorial(n - 1):
//loop - only 2 variables are saved
function factorial(n){
var total = 1;
while(n > 0){
total = total * n;
n = n - 1;
return total;

The second thing that I think was worth mentioning is the real purpose of recursion. You said that recursion is used so you don’t have to repeat yourself. Again, I understand that this is for beginners, but I think that is too much of an oversimplification. It would be better to say that recursion is useful when you can break up a problem so that you can work on part of the problem, then pass on the rest of the problem so that it looks similar to the full problem. For instance, factorials are n * (n-1) * (n-2), etc. If you take out the first n, the problem becomes (n-1) * (n-2), etc. This looks just like the original problem, so you can use the same function to solve it, just starting at n-1 instead of n.

Like what you read? Give SFMason a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.