View Single Post
Old May 15th, 2008, 10:55 AM   #7
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 258
Rep Power: 2 Fall Back Son is on a distinguished road
Re: Anyone a genius?

Quote:
Originally Posted by grumpy View Post
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.
Fall Back Son is offline   Reply With Quote