Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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

share|improve this question

marked as duplicate by jk., MichaelT, gnat, GlenH7, ChrisF Sep 26 '13 at 9:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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.

share|improve this answer
+1 @Upadhyay... Particularly for the last two statements. – Lou Jul 5 '11 at 19:52
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. – Scott Whitlock 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))
share|improve this answer
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.

share|improve this answer

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.

share|improve this answer

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.
share|improve this answer
Please see Can and do compilers convert recursive logic to equivalent non-recursive logic? - some languages / compilers convert recursion into for loops which is no slower and requires no additional overhead. – user40980 Sep 24 '13 at 15:37

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