dinkumthinkum
Senior member
- Jul 3, 2008
- 203
- 0
- 0
A recursive function written like that (with the recursive call executed last) is called "tail-recursion" and is tantamount to iteration. A Sufficiently Smart Compiler (tm) will recognize that situation and transform the recursive call into a loop without any function call overhead. gcc knows how to do this in many situations, though I don't know whether VS2008 can (it is Microsoft, so I have my doubts). It's actually quite a common and simple compiler optimization, and is absolutely necessary for functional language compilers.
It is extremely naive to say that "recursion" is always less efficient. Especially if you go so far as to build your own stack in your attempt to "avoid recursion". That is a classic newbie mistake.
It is extremely naive to say that "recursion" is always less efficient. Especially if you go so far as to build your own stack in your attempt to "avoid recursion". That is a classic newbie mistake.