Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • I'm trying to think of any way an aggressively optimizing compiler could do better, but it does seem like this is probably about it. The best I can come up with is that in the return n+f(); case, maybe the compiler could keep an int hanging around that it adds n to on every f() call because it knows that n+...+n+f() reduces to xn+f() and that last addition can wait until the final f() call. But that seems unlikely to be worth the effort. Commented Jan 10, 2016 at 17:50
  • @Ixrec: A good optimizers might sometimes inline a call, tail-call or not, and/or replace recursion with a loop. Commented Jan 10, 2016 at 17:54
  • You could have a function timesfactorial(x, n) which returns x * n! That function could call timesfactorial (x*n, n-1) which is tail recursive. Commented Nov 15, 2023 at 23:30