Recursion (computer science): Difference between revisions

Content deleted Content added
Line 22:
 
In addition, some numerical methods for finding approximate solutions to mathematical equations use recursion. In [[Newton's method]], for example, an approximate root of a function is provided as initial input to the method. The calculated result (output) is then used as input to the method, with the process repeated until a sufficiently accurate value is obtained.
==recursiveRecursive programming ==
One basic form of recursive computer program is to define one or a few base cases, and then define rules to break down other cases into the base case. This is analytic, and is the most common design for parsers for computer languages.
 
Line 50:
 
In this particular example the iterative implementation is slightly faster in practice than the recursive one. (Note that an even faster implementation for the ''Factorial'' function is that of using a [[lookup table]].) There are other types of problems that seem to have an inherently recursive solution, i.e. they need to keep track of prior state. One example is [[tree traversal]], which is possible to implement iteratively with the help of a [[stack]], but the need for the stack arguably defeats the purpose of the iterative solution. Another possible reason for choosing an iterative rather than a recursive algorithm is that in today's programming languages, thread stacks are often much smaller than memory available in the heap, and recursive algorithms tend to require more stack space than iterative algorithms.
===recursion vs. iteration ===
While recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code, it is often less efficient, in terms of both time and space, than iterative solutions. There are some methods to convert recursive algorithm into an iterative algorith using a loop and branching structure.
 
==Recursive functions==
{{main|recursive function}}