Skip to main content

Timeline for What is tail recursion?

Current License: CC BY-SA 4.0

9 events
when toggle format what by license comment
Apr 25, 2024 at 14:05 comment added Anders You are right here @SupunWijerathne there are no need for maintain a call stack there. Tail recursion optimization is to remove call stack for the tail recursion call, and instead do a jump, like in a while loop. But if you do use the return value of a recursive call before return it, it is a ordinary recursion and can't be optimized. And yes, if you have several recursion cases in the function, you can have several no tail recursion and tail recursion calls. If your compiler doesn't do tail recursion optimization, there will be no difference between them though.
Jan 27, 2021 at 18:36 comment added Supun Wijerathne Peter Meyer, for your example, does the runtime really need to maintain a call stack? @FlySwat
May 22, 2019 at 18:24 comment added T.J. Crowder @IWantAnswers - No, the body of the function can be arbitrarily large. All that's required for a tail call is that the branch it's in calls the function as the very last thing it does, and returns the result of calling the function. The factorial example is just the classic simple example, that's all.
S May 10, 2019 at 6:17 history suggested Tanmay Agrawal CC BY-SA 4.0
Added fac=1 as the default value for the argument to make it easier to understand.
May 10, 2019 at 1:49 review Suggested edits
S May 10, 2019 at 6:17
Oct 23, 2018 at 19:27 comment added I Want Answers I found your explanation easiest to understand, but if it's anything to go by, then tail recursion is only useful for functions with one statement base cases. Consider a method such as this postimg.cc/5Yg3Cdjn. Note: the outer else is the step you might call a "base case" but spans across several lines. Am I misunderstanding you or is my assumption correct? Tail recursion is only good for one liners?
Aug 12, 2014 at 23:30 history edited Mr. Polywhirl CC BY-SA 3.0
Added C syntax highlighting language tags and removed extra line.
Oct 2, 2008 at 2:01 history edited Peter Meyer CC BY-SA 2.5
corrected algorithm
Aug 29, 2008 at 3:57 history answered Peter Meyer CC BY-SA 2.5