Question:
What is a recursive procedure and what are the advantages of recursion, compared to standard loops?
Alexander M
2007-04-17 11:48:12 UTC
What is a recursive procedure and what are the advantages of recursion, compared to standard loops?
Four answers:
Smutty
2007-04-17 14:16:49 UTC
Repetition in computer programs is accomplished in one of two ways: either through recursion or through iteration.



Advantages of recursion:

A common method of simplification is to divide a problem into subproblems of the same type. As a computer programming technique, this is called divide and conquer and is key to the design of many important algorithms, as well as being a fundamental part of dynamic programming.



The great advantage of recursion is that an infinite set of possible sentences, designs or other data can be defined, parsed or produced by a finite computer program.



For a comparison of the advantages or disadvantages of each, plz check:



http://www.stanford.edu/~blp/writings/clc/recursion-vs-iteration.html



Hope this helps
anonymous
2007-04-17 12:00:51 UTC
A recursive procedure

can be reused in itself



where as a loop would take a bit more

programming to do the same thing.



where as they can both be used as loops



yet in the recursive one

it can call its self and be used over and over



for example

say you had a procedure to subtract a number

from a larger one



you could call the procedure up

and while it subtracts the number

before it exits out the loop it can check to

see if its zero and if not

call the same procedure again and again

till the end result is met



I know its a simple explanation

yet to get a loop to do the same thing

would take a bit more programming



Hope this helps



plus recursive procedures save a lot

of programming steps.
Fabian
2007-04-17 11:55:50 UTC
Recursion is when the same procedure is invoked again from within itself.



Advantages?

I would say you either require recursion in a procedure or you don't.



A good example of a recursive procedure would be one that collects file names from a directory. If you encounter another directory you may wish to fork off into that directory, and so on.



A caveat of recursion is infinite loops. Good coding and common sense should avoid this.
rojaz
2007-04-17 12:07:14 UTC
They can also be used to traverse through linked list and trees. You could do this with a loop, but recursion would be simpler. The main problem with recursion is that it costs a lot (use a lot of resourses) while a loop would not.



i can send you a program of mine that using recursion if you wish, its in c++.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...