I noticed the common definition of recursion that "It is a function calling itself" is wrong.
Wait! Don't react yet. Hear me out first; is the function below a loop or recursion?
Note: If you want a video version of this explanation, watch What is recursion.
function countdown(count) {
console.log(count);
if(count > 1) {
count = count - 1;
countdown(count);
} else {
return;
};
};
// access function
countdown(5);
I know you're probably going to say it is recursion because of the common definition but it is not β it is a loop.
If you check the console, nothing will be repeated but the output of recursion must be a repetition of a structure.
Is recursion really a case of a function calling itself? I don't think so.
I came to this realization when I was building koras.jsx that uses Constant Reactivity and it is implemented in a way that looks like it is recursive but it is not.
I will get back to that later but let's focus on recursion first. Then, what is recursion if it is not a function calling itself?
Before I define what recursion is, it is important I define what a loop is for clarification.
What is a loop?
A loop is an operation that applies a process to a series or sequence of elements once until the last element (if any) or continue till infinity.
That means a loop doesn't go back to reapply the same process to already processed elements.
Now, let's go back to the previous function so that you realize it is a loop:
function countdown(count) {
console.log(count);
if(count > 1) {
count = count - 1;
countdown(count);
} else {
return;
};
};
// access function
countdown(5);
You will notice that, even though the function calls itself to move to the next line of action, it doesn't reapply the same process to the same element(s).
If you log the count in the console, nothing will be recursive, that is, repeat the same index, structure or item(s).
That means, you can implement a loop with a function. So a function calling itself is not a criterion to determine a recursion.
Let's compare the function above with a while loop for clarification.
let depth = 5;
while(depth > 0){
console.log(depth)
depth--;
}
The countdown function
is very similar to the above while loop because they get similar results with similar structures. Can you see it is a loop? Okay.
Did you say "How do you define and identify a recursion?"? Keep on reading.
What is recursion?
It is a repetition of an operation on the same element(s) by always starting from the beginning to the end and then starting again, again and again until the stopping point (if any); if not, it continues till infinity.
Recursion always repeats the same process on the same elements several times until the stopping point is reached or it continues till infinity.
We have some examples of recursion in the real world. A good example are the days of the week or February 14.
A week always starts from Monday; continues to Sunday and goes backs to Monday again, again and again.
Here we are again; it is VALπ₯°. Isn't VAL recursive? Yes, it is.
That is recursion.
If you write a function to achieve weeks in continuity, it will automatically be recursive because you will always reapply the same process to the same elements again and again.
And if you log it to the console, you will see that items will be repeated in a certain structure, if not, it is not recursion.
You can achieve recursion with a loop or a function. So, instead of saying a recursion is defined as a function calling itself, you had better say it is a repeated process reapplied to or within the same element(s).
Now, let's write true recursion with a loop and a function.
We're going to turn the previous countdown
function to recursion by backtracking it.
function startCountdown(n, backtrackCount = 0) {
if (backtrackCount >= 5) {
console.log("Countdown stopped after 5 backtracks.");
return;
}
console.log(n);
n--;
if (n === 0) {
console.log("Restarting countdown!");
startCountdown(5, backtrackCount + 1);
} else {
startCountdown(n, backtrackCount);
}
}
startCountdown(5);
That is true recursion. If you check the console, you will see that a structure is repeated.
We can achieve the same with a while loop as in below:
function countdown(n) {
let backtrackCount = 0;
while (backtrackCount < 5) {
console.log(n);
n--;
if (n === 0) {
console.log("Restarting countdown!");
n = 5;
backtrackCount++;
}
}
console.log("Countdown stopped after 5 backtracks.");
}
countdown(5);
We get the same results despite the second function doesn't call itself.
If it achieves recursive effects without calling itself, isn't it wrong or incomplete to say recursion is a function calling itself? Yes it is wrong!
That is an obvious explanation to claim a function calling itself is not a correct definition of recursion and so it is misrepresenting what recursion is.
Top comments (20)
You should know that the first code example you gave is in fact recursion. Your base case is present, and the function is called recursively with a consistently modified parameter until the base case is satisfied. Your
if-else
block is an example of a base statement.Why this is a recursive function:
Self-Referencing Call
if
statement:Base Case and Recursive Case
(Stops recursion when
count
reaches1
.) This prevents infinite recursion, which would otherwise lead to a stack overflow.(Keeps calling itself with a decremented value.)
Function Call Stack
Why it's NOT a loop:
for
,while
,do-while
) executes code repeatedly within the same execution context, modifying variables without calling itself.See, you're giving implementation details while I am telling you what it really is. I already know the implementation details, after my findings, I learned that the common thinking is wrong.
My question to you use is do you know you can build your own while or for loop using a function?
Do you know you can achieve recursive effects without a function calling itself? If you say yes, then the case you just made is faulty.
Base-case is not what determines a recursion though it is an important element of it. Recursion is a real-world phenomenon.
Everything in computer science is a representation of the real world.
We do know that the same effects can be obtained by either a loop or recursion. That's why there is always the recursive and the iterative implementation of the same algorithm.
In the real world, this is recursion
While this a auto sports loop
I'm not sure what you are trying to achieve with this article. You are taking an existing word and giving it a different meaning than most people use it for, that's a recipe for frustrating communication, everyone assuming she has the right definition and talking over each other's shoulder.
Don't you think saying a recursion is function calling itself is misrepresenting what recursion is?
If it does why shouldn't we make attempts to correct it to prevent the spread of misinformation, I think we should.
It is better to have a tough conversation than to allow misinformation.
I am making a case that the definition forces people to assume a loop is a recursion because they don't really understand what a recursion is.
My goal for writing the article is to prevent the spread of misinformation about recursion because what we mostly call recursion is a loop.
And people are misrepresenting it preventing them from writing more useful algorithms.
I only said probably β only to be intellectually honest because scientific findings have no end.
Thanks.
I don't think so, no, because words do not have a true objective meaning.
They are just a bunch of letters which people use to convey a meaning, hopefully the same one.
For example, coming from continental Europe, the true meaning of "liberal" for me is "economically right wing", but for some reasons in the US, it means "left wing moderate".
We could argue who is right and wrong according to reason, logic and history
...but that would be absolutely annoying and pointless because nobody can dictate how others choose to communicate
I see your angle but this is not a case of who is wrong or right.
Anything science or fact has to be the same everywhere, if it is not, it is missing something.
There are terms that have relative meanings across fields but recursion is not one of them because it has a universal meaning.
Your comments seem to introduce new points rather than reinforcing your articleβs argument. If multiple readers are "misunderstanding", perhaps the explanation could be clearer. Have you considered revising the article or asking someone to review it for clarity or accuracy? Ensuring correctness in technical discussions is important. If you alone think your position is accurate but are not open to review, then the work lacks credibility, as it hasnβt been peer-reviewed.
My comments are re-enforcing my article. If the definition of a concept is wrong or incomplete, it's implementation will be wrong or incomplete.
That is my point. Maybe you should prove to me that we can't achieve recursive effects with a loop?
If we can, then it is faulty to say recursion is a function calling itself.
Or maybe you should re-read the article. Thank you!
You can check out this link for better understanding on recursion and also compare the iterative and recursive solution to the same problem ocw.mit.edu/ans7870/6/6.005/s16/cl...
Thanks for the paper.
I have read papers that even established Recursion in computer science before I came here to make a case.
The point I am making is recursion is misrepresented. Anyway, it is okay that you are not even arguing whether recursion is a function calling itself or not.
If you accept the definition is incorrect or incomplete, then anything related to it should be incomplete too.
Thank you!
The difference between recursion and loop is the function scope. If each step of the iteration has its own scope, it is recursion, otherwise it is a loop. Your definition is plain wrong.
Nope, recursion exists in the real world before the advent of computer science. I am telling you it is probably (to be intellectually honest) misrepresented.
Let me add. My definition may also be wrong or incomplete but I have been able to establish that recursion is not a function calling itself because we can achieve the same it a loop.
This is a misunderstanding on your part. It is exactly the other way around. The term "iteration" is derived from the Latin word "ite" (walking, stepping), whereas recursion comes from the French "recourse" ("step back").
It follows that a recursion can be used to iterate over enumerables, but not every iteration is a recursion.
I really do not misunderstand it. My claim is that not all function calling itself is recursive. Maybe you can prove to me otherwise.
My case is, if we can't define a thing correctly, then, how do we know how to implement it?
Maybe we should define recursion first but no one has tried to do that.
Don't take me out of context. I am not saying all iterations are recursion.
What I am saying is a recursion is not necessarily a function calling itself.
A function may be calling itself but still don't achieve recursive results.
Programming is about input, process and output. A function calling itself may seem recursive but is it really recursive if it's output is not?
Pay attention to the cup above! You have to repeat the same process to achieve the effect in the image.
I ask that question because in the real world, every recursive process always gives recursive output. Why isn't that the case in computer science?
I will agree with whoever can prove to me that recursion in the real world only happens in process but not output.
No one can really do that as it seems.
Is your article attempting to define recursion in programming accurately? If so, I find the approach problematic because it seems to prioritize a 'real-world' analogy over the technical implementation of recursion. I donβt understand why the article ignores how functions and the execution stack work, which are fundamental to understanding recursion.
When you call a function, it always has a return value, even if it's null or void. In recursion, each function call is placed on the call stack, a LIFO data structure that tracks function calls (and this ensures proper management of the recursive calls). When the base case (the condition that stops the recursion) is reached, the stack unwinds, returning values to the previous calls. Think of it like building a stack and unpacking it once the base case is satisfied. So, whether you output the values or use them or not, the return values of your function calls are still managed.
Recursion isnβt just about a function calling itself, itβs about solving a problem by breaking it down into smaller instances of the same problem, which inherently requires the function to call itself.
Even if you donβt explicitly use return values in your code, they still exist and are processed by the call stack (check out en.wikipedia.org/wiki/Call_stack). Comparing recursion to a simple loop is misleading because loops donβt involve the call stack in the same way.
The example provided in your article is undeniably recursive. The function
countdown(count)
calls itself within anif
condition, decrementingcount
each time until it reaches the base case (count <= 1
), at which point it returns.You claim that recursion must produce a repetition of a structure, but this is incorrect. Recursion is about solving a problem by breaking it down into smaller subproblems, not necessarily about the output structure. Linear recursion, for instance, refers to a recursive function that makes a single call to itself each time, progressing directly toward the base case, and is still recursion.
Your confusion likely stems from conflating output patterns (e.g., nested structures) with the definition of recursion. If you want to distinguish between types of recursion (check out this article: geeksforgeeks.org/types-of-recursi...), that would be a more productive discussion. However, claiming that the example is a loop instead of recursion is incorrect. Achieving the same result with a loop doesnβt negate the recursive nature of the function. Iβd prefer not to delve into arguments about comparing known with unknown (as you claim), as it seems to introduce unnecessary complexity.
I appreciate the effort to clarify recursion, but this explanation introduces confusion rather than resolving it. The function you provided is, without question, recursive, and the distinction between recursion and loops should not be blurred this way. As someone who mentors Computer Science students, I genuinely worry they might come across this post and be misled as this could spread misunderstandings about a fundamental concept.
Recursion is self-reference. A recursive function calls itself. Recursive output will reference (parts of) itself.
You can create recursive output with a recursive function and create non-recursive output with a recursive function.
I wrote something about it already on Twitter and I made this conclusion.
You can see the time on my Tweet comes before yours.
A recursive function calls itself while a recursion makes a reference to itself.
Yeah! That is the difference. Can you see the case I am making is correct.
Thanks for pointing out the typo in my Tweet.
A loop is iterative not recursive, sir. geeksforgeeks.org/difference-betwe...
I have already explained what a loop is.
Again, what is recursion? If it is not a function calling itself, then what is it?
There is not way to compare what is known with what is unknown. The first thing is to establish an acceptable definition of recursion.
If we get it's definition wrong, how do we know it's correct implementation except "a function calling itself" is a correct definition of recursion to you.
Why are you trying to redefine terms?
A recursive function is a function that calls itself 'recursively' in an iterative manner until it is resolved and then exits itself which is why recursive function have logical statements within them.
Without a logical statement to determine when it ends, it would be stuck in a "loop".
Synonym for the word "recursion" are: looping. iterative. repetitive. recurrent
See the above? So, maybe, you should learn to master the language you communicate with before you decide to preach others about its meaning.
A programming language that supports recursive functions internally provide a unique pointer in memory for each iteration. Each recursive call is placed in its own program stack.
Excessive calls may end up with one's program running out of memory and/or memory leaks.