Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
    
This will depend a lot on the language. Some are much better at optimizing recursion than others. –  KChaloux Sep 24 '13 at 15:24
add comment

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.

5 Answers

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
14  
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
add comment

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))
    {
        files.AddRange(GetAllFilesInTree(path));
    }
}
share|improve this answer
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
add comment

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
add comment

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
add comment

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

  1. recursion is slower in terms of speed and execution time.
  2. recursion requires extra amount of runtime overload.
share|improve this answer
1  
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. –  MichaelT Sep 24 '13 at 15:37
add comment

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