Forem

Cover image for Recursion is not a function calling itself!

Recursion is not a function calling itself!

Ayobami Ogundiran on February 14, 2025

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 fun...
Collapse
 
elizabethadegbaju profile image
Elizabeth Adeotun Adegbaju • Edited

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:

  1. Self-Referencing Call

    • The function calls itself inside the if statement:
     countdown(count);
    
  • This is the defining characteristic of recursion. This alone defines recursion, regardless of additional explanations.
  1. Base Case and Recursive Case

    • Base Case:
     if (count <= 1) return;
    

    (Stops recursion when count reaches 1.) This prevents infinite recursion, which would otherwise lead to a stack overflow.

    • Recursive Case:
     countdown(count - 1);
    

    (Keeps calling itself with a decremented value.)

  2. Function Call Stack

    • Each recursive call creates a new stack frame.
    • This is NOT how loops behave. Loops reuse the same execution context, whereas recursion generates new function calls for each step until the base case is reached. A common exercise in Computer Science involves solving a problem using both loops and recursion, demonstrating this difference.

Why it's NOT a loop:

  • A loop (for, while, do-while) executes code repeatedly within the same execution context, modifying variables without calling itself.
  • Example of a loop equivalent to your function:
  function countdownLoop(count) {
      while (count > 0) {
          console.log(count);
          count--;
      }
  }
  countdownLoop(5);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
codingnninja profile image
Ayobami Ogundiran • Edited

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.

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

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

Image description

While this a auto sports loop

Image description

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.

Thread Thread
 
codingnninja profile image
Ayobami Ogundiran • Edited

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.

Thread Thread
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

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

Thread Thread
 
codingnninja profile image
Ayobami Ogundiran

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.

Thread Thread
 
elizabethadegbaju profile image
Elizabeth Adeotun Adegbaju

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.

Thread Thread
 
codingnninja profile image
Ayobami Ogundiran

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!

Collapse
 
elizabethadegbaju profile image
Elizabeth Adeotun Adegbaju

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...

Collapse
 
codingnninja profile image
Ayobami Ogundiran • Edited

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!

Collapse
 
lexlohr profile image
Alex Lohr

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.

Collapse
 
codingnninja profile image
Ayobami Ogundiran • Edited

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.

Collapse
 
lexlohr profile image
Alex Lohr

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.

Thread Thread
 
codingnninja profile image
Ayobami Ogundiran • Edited

It follows that a recursion can be used to iterate over enumerables, but not every iteration is a recursion.

Image description

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.

Thread Thread
 
elizabethadegbaju profile image
Elizabeth Adeotun Adegbaju

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 an if condition, decrementing count 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.

Thread Thread
 
lexlohr profile image
Alex Lohr

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.

const recData = [];
recData.push(the data);

const reverseStr = (input, reverse = "") =>
  input 
  ? reverseStr(input.slice(1), output + input[0])
  : output;
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
codingnninja profile image
Ayobami Ogundiran • Edited

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.

Collapse
 
elizabethadegbaju profile image
Elizabeth Adeotun Adegbaju

A loop is iterative not recursive, sir. geeksforgeeks.org/difference-betwe...

Thread Thread
 
codingnninja profile image
Ayobami Ogundiran • Edited

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.

Collapse
 
houbou2468 profile image
Claude Gauthier

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.