Page 1 of 1

Tail Recursion

Posted: Fri May 03, 2019 3:31 am
by AndrewBuckley
My understanding of C is fairly basic, so I don't want to change the wiki if I am incorrect, but is the tail recursive example on https://wiki.osdev.org/Tail_Recursion_a ... timization correct?
It seems to think the order the lines occur is more important than the recusion being equivalent to the caller.

Re: Tail Recursion

Posted: Fri May 03, 2019 5:01 am
by Octocontrabass
The order of the lines makes no difference to any modern optimizing compiler. With sufficient optimizations enabled, GCC produces identical tail-call-optimized code for both example functions. Clang does the same thing (though its results are not the same as GCC's).

I tried a few other compilers, but I didn't find any others that wanted to do tail call optimization on the example functions.