When not to use recursion

mindless728

Distinguished
Jul 15, 2008
130
0
18,660
You don't need to use recursion. Anything that is done with recursion can be done iteratively. The reason recursion is usually not a good idea is that the system stack is limited in size where as a stack that you create uses normal system memory.
 

kyeana

Distinguished
May 21, 2008
230
0
18,860
Mmmmm.... Tasty stack overflow exceptions! :)

Personally, the only time i use recursion is when a recursive solution will be significantly easier to code/understand then the alternative, but that doesn't come up super often (mainly navigating through trees and the such.)
 

Ijack

Distinguished
Rather depends upon which language you are using. In some - LISP springs to mind - recursion is the most natural and efficient way to solve a problem. In others, such as C++ it doesn't come quite so naturally; but it can still be a very efficient technique if used wisely.

As kyeana says, something like navigating through trees or enumerating directories. For example, if you were writing a backup program that needed to process every directory and file on a hard disk then recursion would be a very natural approach.