views:

684

answers:

5

Can someone show me a simple tail-recursive function in C++?

Why is tail recursion better, if it even is?

What other kinds of recursion are there besides tail recursion?

+14  A: 

A simple tail recursive function:

unsigned int f( unsigned int a ) {
   if ( a == 0 ) {
      return a;
   }
   return f( a - 1 );   // tail recursion
}

Tail recursion is basically when:

  • there is only a single recursive call
  • that call is the last statement in the function

And it's not "better", except in the sense that a good compiler can remove the recursion, transforming it into a loop. This may be faster and will certainly save on stack usage. The GCC compiler can do this optimisation.

anon
So is tail recursion then just when the return statement has the recursive call?
Phenom
Not quite. Tail recursion is when the last computed statement is recursive. If, for instance, f(a-1) were assigned to a variable in this example, that would still be tail-recursive.
Joel
To clarify Neil's answer, a compiler can usually optimize a tail-recursive function into a loop if the recursive function is the absolute final operation in the function (aside from `return`). That is, Neil's example could be auto-optimized, but if it was modified to end with `return f(a-1) + 1;` then it would not typically be optimized (since the `+1` would be performed after the recursive call).
bta
Interestingly, looking at the assembly output, `gcc 4.3` seems to optimize away the recursion in both a naive and a tail-recursive factorial function starting at `-O2`.
Mike Dinsdale
@Joel: i don't think it would still be tail-recursive, since the last statement would be the assignment and not the call. unless a very smart compiler could hoist the assignment out of the function; but i doubt it.
Javier
@Javier : Usually assignment to a local variable is just an illusion for the programmers sake; just allowing her to use the name. Most any compiler with any level of optimization at all could see x=f(a-1);return x; as the same as the last statement in the example as it would just apply the label x to the location of the value returned by f and would not have any reason to move the value from that location.
nategoose
@nategoose: i see that a line like `x=f(a-1);return x;` doesn't do any 'real' assignment if `x` is local. that's why i didn't even thought of your initial comment as talking about a local variable. if `x` is a global (or, more likely, something like `*out=f(a-1);return x;`), then i don't think you can keep the tail recursion.
Javier
+8  A: 

Tail recusion in C++ looks the same as C or any other language.

void countdown( int count ) {
    if ( count ) return countdown( count - 1 );
}

Tail recursion (and tail calling in general) by definition clears the function's stack frame before executing the next call. Tail recursion is the same as a loop, almost a mere stylistic difference. Most compilers support it, but loops are both easier and less risky.

Tail calls can enable random branching (like goto to the first line of a function), which is a more unique facility.

Note that in C++, if you have objects to be destroyed, the end-of-function cleanup might preclude this.

EDIT: Since there seems to be some confusion here, note that tail recursion requires that the state of the algorithm be entirely passed into the function's arguments at each step. (This is clear from the requirement that the function's stack frame be eliminated before the next call begins… you can't be saving any data in local variables.) Furthermore, no operation can be applied to the function's return value before it's tail-returned.

int factorial( int n, int acc = 1 ) {
    if ( n == 0 ) return acc;
    else return factorial( n-1, acc * n );
}
Potatoswatter
Finally a real tail-recursive factorial. +1
sepp2k
I'm guessing I just got downvoted because of confusion about the functionality of the `countdown` function. Granted it isn't particularly useful, but it is correct. You are allowed to return a void expression from a void function; in this case `return expr;` is equivalent to `expr; return;` **except that it explicitly denotes tail calling.**
Potatoswatter
A: 

Tail recursion does not exist really at compiler level in C++.

Although you can write programs that use tail recursion, you do not get the inherit benefits of tail recursion implemented by supporting compilers/interpreters/languages. For instance Scheme supports a tail recursion optimization so that it basically will change recursion into iteration. This makes it faster and invulnerable to stack overflows. C++ does not have such a thing. (least not any compiler I've seen)

Apparently tail recursion optimizations exist in both MSVC++ and GCC. See this question for details.

Earlz
Whether or not a compiler does or doesn't optimize tail recursion depends on the compiler. gcc does (or can at least).
sepp2k
Really? I'll have to look into this. I thought it may have broken the C++ spec or something
Earlz
Generally people writing C don't use recursion, so the optimization is left out. Gcc is the exception here, as far as I know.
Joel
@Joel I use recursion heavily in all my C++ code and used to do so when I wrote C. How else do you deal with trees, expression evaluation, parsing etc. etc.
anon
@Neil: recursive expression evaluation? You want to get yourself a nice shunting yard ;-)
Steve Jessop
@Neil: Any recursion can be transformed into an iteration (by using a stack).
Nemanja Trifunovic
@Earlz, http://lambda-the-ultimate.org/node/3674, check the pdf from page 40.
liori
@Nemanja Of course it can, but are you suggesting that C programmers are routinely doing this? Just because something can be done doesn't mean that it is done in practice.
anon
Tree traversal algorithms generally don't admit tail recursive implementations, however. Not claiming we don't use recursion, just that tail recursion is unusual. It would be far more common to see a loop based factorial than the recursive version you see most often in functional languages, for instance. The same for list traversals.
Joel
When I write trees I make sure to either use tail recursion or iteration whenever possible. Recursion is much easier for node visitors, but for lookup I typically do iterative. For doing recursive visitors I dream that the compiler is smart enough use a stack depth counter and push/pop iteratively rather than doing real recursion (pushing return addresses).
nategoose
Tail recursion is available in gcc and MSVC: http://stackoverflow.com/questions/34125/which-if-any-c-compilers-do-tail-recursion-optimization
plinth
@plin I've updated my answer to reflect that. I was not actually aware of it
Earlz
A: 

Wikipedia has a decent article on tail recursion. Basically, tail recursion is better than regular recursion because it's trivial to optimize it into an iterative loop, and iterative loops are generally more efficient than recursive function calls. This is particularly important in functional languages where you don't have loops.

For C++, it's still good if you can write your recursive loops with tail recursion since they can be better optimized, but in such cases, you can generally just do it iteratively in the first place, so the gain is not as great as it would be in a functional language.

Jonathan M Davis
That's still not tail-recursive.
sepp2k
Isn't it easier to write a tail-recursive function than an iterative loop?
Phenom
Whether it's easier to write a tail-recursive function or an iterative loop will likely depend on what you're trying to do and how you think. If you're really used to writing recursive functions or the problem is really easy to define recursively, then the recursive function could be easier to write. However, if you're not good with recursion or if the problem in question can easily be treated iteratively, then it can be easier to write an iterative loop.
Jonathan M Davis
Careful with the terminology "recursive loop." All loops are recursive. The problem with using this mathematical terminology is that there's no mathematical difference between the concepts.
Potatoswatter
+5  A: 

Tail recursion is a special case of a tail call. A tail call is where the compiler can see that there are no operations that need to be done upon return from a called function -- essentially turning the called function's return into it's own. The compiler can often do a few stack fix-up operations and then jump to (rather than call) the address of the first instruction of the called function.

One of the great things about this besides eliminating some return calls is that you also cut down on stack usage. On some platforms or in OS code the stack can be quite limited and on advanced machines like the x86 CPUs in our desktops decreasing the stack usage like this will improve data cache performance.

Tail recursion is where the called function is the same as the calling function. This can be turned into loops, which is exactly the same as the jump in the tail call optimization mentioned above. Since this is the same function (callee and caller) there are fewer stack fixups that need to be done before the jump.

The following shows a common way to do a recursive call which would be more difficult for a compiler to turn into a loop:

int sum(int a[], unsigned len) {
     if (len==0) {
         return 0;
     }
     return a[0] + sum(a+1,len-1);
}

This is simple enough that many compilers could probably figure it out anyway, but as you can see there is an addition that needs to happen after the return from the called sum returns a number, so a simple tail call optimization is not possible.

If you did:

static int sum_helper(int acc, unsigned len, int a[]) {
     if (len == 0) {
        return 0;
     }
     return sum_helper(acc+a[0], len-1, a+1);
}
int sum(int a[], unsigned len) {
     return sum_helper(0, len, a);
}

You would be able to take advantage of the calls in both functions being tail calls. Here the sum function's main job is to move a value and clear a register or stack position. The sum_helper does all of the math.

Since you mentioned C++ in your question I'll mention some special things about that. C++ hides some things from you which C does not. Of these destructors are the main thing that will get in the way of tail call optimization.

int boo(yin * x, yang *y) {
    dharma z = x->foo() + y->bar();
    return z.baz();
}

In this example the call to baz is not really a tail call because z needs to be destructed after the return from baz. I believe that the rules of C++ may make the optimization more difficult even in cases where the variable is not needed for the duration of the call, such as:

int boo(yin * x, yang *y) {
    dharma z = x->foo() + y->bar();
    int u = z.baz();
    return qwerty(u);
}

z may have to be destructed after the return from qwerty here.

Another thing would be implicit type conversion, which can happen in C as well, but can more complicated and common in C++. For instance:

static double sum_helper(double acc, unsigned len, double a[]) {
     if (len == 0) {
        return 0;
     }
     return sum_helper(acc+a[0], len-1, a+1);
}
int sum(double a[], unsigned len) {
     return sum_helper(0.0, len, a);
}

Here sum's call to sum_helper is not a tail call because sum_helper returns a double and sum will need to convert that into an int.

In C++ it is quite common to return an object reference which may have all kinds of different interpretations, each of which could be a different type conversion, For instance:

bool write_it(int it) {
      return cout << it;
}

Here there is a call made to cout->operator<< as the last statement. cout will return a reference to itself (which is why you can string lots of things together in a list separated by << ), which you then force to be evaluated as a bool, which ends up calling another of cout's methods, operator bool(). This cout->operator bool() could be called as a tail call in this case, but operator<< could not.

EDIT:

One thing that is worth mentioning is that a major reason that tail call optimization in C is possible is that the compiler knows that the called function will store it's return value in the same place as the calling function would have to ensure that its return value is stored in.

nategoose