In this chapter, you learned about the essentials of recursion. Apart from looking at the theoretical foundation for recursion, you also were presented with a couple of typical recursion examples.

The important points covered in this chapter are reviewed in this section.

When a method calls itself from within its own method body, the method is called a recursive method. The use of recursive methods is called recursion.

Recursive method calls result in pending method instances of the recursive method.

A successful recursive method contains a branching statement, one or more base cases, and one or more recursive calls. Each recursive call must move toward the base case to avoid infinitely many recursive calls.

Recursion is suited to solve computational problems that can be solved by solving simpler versions of an original problem. Eventually, the problem becomes so simple (base case) that the recursive method knows the direct answer. By possibly combining the values kept in several pending method instances, an answer to the original problem can be found.

Any problem that can be solved with recursion also can be solved without recursion, using iteration instead. Recursion is not as effective as iteration, but sometimes provides clearer more compact algorithms, especially if the problem at hand is expressed recursively naturally.

Factorial and binary searches are two typical problems that can be solved recursively.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: