-2

What (if any) advantages/disadvantages, does recursion have versus iteration? Is one better than the other? If so, why? Is one worse than the other? I'm looking to get a general pro's versus con's overview of the two. Obviously, there are times that you would (or could) only use recursion; this question is more along the lines of "if you could use either recursion or iteration, which would you use and why..."

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • 1
    Maybe these questions and answers help http://stackoverflow.com/q/13869894/1741542, http://stackoverflow.com/q/478570/1741542 or http://stackoverflow.com/q/72209/1741542 – Olaf Dietsche Apr 26 '13 at 21:44
  • @AndyThomas-Cramer, I'm not wondering if it's faster, I'm wondering what adavantages/disadvantages there are. If speed is one, great. Are there any more, or is speed the only thing we need to consider? – BlackHatSamurai Apr 26 '13 at 21:46

1 Answers1

0

not an expert of the topic, but here is my .5$

  • often math. functions are defined by recursion, so implementing the exact definition by recursion yields a program that is correct "by defintion"

  • properties of recursively defined functions (or to some extent methods) can be proved by induction

  • personally, I find it much harder to debug typical "procedural" code, there is a lot of book keeping going on as the evolution of all the variables has to be kept in mind.

  • Often it seems that compilers produce more efficient machine code from loops than from recursive definitions.. however, many recursive programs can be optimized "as loops" by the complier (e.g. tail-call optimization).

Summing up, I would say that recursion is more "high level" feature of a language than loops. Meaning that there is more to be done from the compiler side in order to produce performant code but it is more convenient to reason about recursive programs on the "human" side.

D.F.F
  • 914
  • 1
  • 6
  • 17