Quote:
Originally Posted by grumpy
Not quite so, Fallback Son. Tail recursion simply means that the last operation in a function is a call to itself. So;
int CallMySelfAgain(int i)
{
if (SomeResult(i))
return some_value;
return CallMySelfAgain(SomeTransformation(i));
} is tail recursive.
|
I was trying to indicate that tail recursion means there are no "pending operations" (mult, add, printing something, whatever else) on a recursive call after it returns. But you are right to clear up the misleading examples.
Also, I'm glad you gave that example in particular, I never considered whether a linearly recursive function which calls another function can always be written tail recursively. But I imagine those can also.