Wednesday, April 15, 2009

What is a recursive procedure and what are the advantages of recursion, compared to standard loops?

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/cl...





Hope this helps

What is a recursive procedure and what are the advantages of recursion, compared to standard loops?
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.
Reply: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%26#039;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.
Reply: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++.



tanning

No comments:

Post a Comment