Whats the difference between the two procedures? When should I be using a recursive function instead of a normal for-loop?

  • This will depend a lot on the language. Some are much better at optimizing recursion than others.
    – KChaloux
    Sep 24 '13 at 15:24

Your "for cycles" are basically iterative functions (can be recursive as well, but not as likely). Iterative functions are typically faster than their recursive counterparts.

Sometimes a recursive solution is very natural while an iterative solution is very awkward. Go with recursion in those cases.

Lastly, if you have limited space on the stack, go with iteration.

  • +1 @Upadhyay... Particularly for the last two statements.
    – Lou
    Jul 5 '11 at 19:52
  • 15
    Lastly, if you have limited space on the stack, go with iteration. - unless you can implement the recursive function as tail-recursive and your language implements Tail Call Optimization. Jul 5 '11 at 20:59

Do not use recursive functions where you could use a simple cycle, unless told to in an interview (ahh, Fibonacci, how I love and hate you!). Even then you might want to ask why; you may get bonus points.

Recursive functions are perfect for tree structures. Loops are perfect for iterations and sequences.

public List<string> GetAllFilesInTree(string path)
    List<string> files = new List<string>(GetAllFilesInFolder(path));
    foreach (string subdir in GetAllFoldersInFolder(path))
  • 1
    There is an API call for this ;)
    – Job
    Jul 6 '11 at 0:08
  • @Job One homework task less for our students.
    – Carra
    May 31 '12 at 7:30

Rule of the thumb: use recursion when it will shorten your development effort (usually in tree structures) and when performance is not an issue.

Recursion can be allot easier and faster to implement in many cases, thus commercially it makes sense to use recursion. In most cases you can write better performing code that uses less resources by not using recursion. Your boss would probably choose for the recursion solution if you give him the choice :-)

I remember something about a specific procedure how to convert a recursive algorithm into a non recursive one. It involved extracting the arguments and making local variables or arrays instead. Can't remember the specifics though, but with some common sense it should be a straightforward job.


Recursive function calls can solve all problems loops can solve, and then some. Most problems fall into the category that can be solved using either, and in most languages, the iterative solution is more efficient, so that's what you should use if in doubt.

Regardless, finding an iterative solution when you already know the recursive one, or vv., can be a nice exercise, and often tricky.


When function itself calls repeatedly until some specific condition is met.

  1. recursion is slower in terms of speed and execution time.
  2. recursion requires extra amount of runtime overload.

Not the answer you're looking for? Browse other questions tagged or ask your own question.