Web site hosting - 216 Methods Chapter 6 only the simplest case(s),
Thursday, August 2nd, 2007216 Methods Chapter 6 only the simplest case(s), or base case(s). If the method is called with a base case, the method returns a result. If the method is called with a more complex problem, the method divides the problem into two conceptual pieces a piece that the method knows how to perform (base case) and a piece that the method does not know how to perform. To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or smaller version of it. The method invokes (calls) a fresh copy of itself to work on the smaller problem this is referred to as a recursive call, or a recursion step. The recursion step also normally includes the keyword return, because its result will be combined with the portion of the problem that the method knew how to solve. Such a combination will form a result that will be passed back to the original caller. The recursion step executes while the original call to the method is still open (i.e., it has not finished executing). The recursion step can result in many more recursive calls, as the method divides each new subproblem into two conceptual pieces. Each time the method calls itself with a slightly simpler version of the original problem, the sequence of smaller and smaller problems must converge on the base case, so the recursion can eventually terminate. At that point, the method recognizes the base case and returns a result to the previous copy of the method. A sequence of returns ensues up the line until the original method call returns the final result to the caller. As an example of these concepts, let us write a recursive program to perform a popular mathematical calculation. The factorial of a nonnegative integer n, written n! (and pronounced n factorial ), is the product n ( n - 1 ) ( n - 2 ) 1 with 1! equal to 1, and 0! defined as 1. For example, 5! is the product 5 4 3 2 1, which is equal to 120. The factorial of an integer number greater than or equal to 0 can be calculated iteratively (nonrecursively) using for as follows: factorial = 1; for ( int counter = number; counter >= 1; counter– ) factorial *= counter; We arrive at a recursive definition of the factorial method with the following relationship: n! = n ( n - 1 )! For example, 5! is clearly equal to 5 * 4!, as shown by the following: 5! = 5 4 3 2 1 5! = 5 ( 4 3 2 1 ) 5! = 5 ( 4! ) A recursive evaluation of 5! would proceed as in Fig. 6.14. Figure 6.14a shows how the succession of recursive calls proceeds until 1! is evaluated to be 1, which terminates the recursion. Each rectangle represents a method call. Figure 6.14 shows the values returned from each recursive call to its caller until the final value is calculated and returned. Figure 6.15 uses recursion to calculate and print the factorials of the integers 0 10. The recursive method Factorial (lines 17 24) first determines whether its terminating condition is true (i.e., number is less than or equal to 1). If number is less than or equal to 1, factorial returns 1, no further recursion is necessary and the method
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.