Chapter12

Chapter12 - Chapter 12 Recursion Recursion occurs when a...

This preview shows pages 1–5. Sign up to view the full content.

Chapter 12 – Recursion Recursion occurs when a method calls itself, either directly or indirectly. Direct recursion (function factorial calls itself): int factorial (int n) { factorial (n - 1); // recursive call } Indirect recursion (function1 calls function 2, which calls function1): int function1 (…) { … function2 (…); } int function2 (…) { … function1 (…); } The classic example of recursion is a method which computes N! (N factorial). int factorial (int n) { if (n <= 0) { // the ‘<’ ensures reasonable behaviour for unreasonable “n” return 1; } else { return n * factorial (n - 1); // recursive call } } The method follows naturally from the mathematical definition of N! (for N >= 0): n! = n * (n-1) * (n-2) * … * 1 0! = 1 n! = n * (n-1)! // recursive definition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The example is widely used only because it’s very simple. Using recursion isn’t a very good way of computing N!. The simple iterative solution shown below is far more efficient. Note that it too will behave reasonably if given an unreasonable value. If “n” is less than zero, the “for” loop will be executed zero times, and the function will return 1. int factorial (int n) { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } In general, recursion should be avoided if there’s an “iterative” alternative. There are, however, situations (e.g. the Towers of Hanoi - coming up) in which recursion is a very powerful and useful programming tool. In languages which allow recursion (e.g. C++, JAVA and most other modern languages), a recursive call is no different from any other call. The method is invoked and, presuming that it works properly, a correct result is produced. Recursion works because, each time a function is invoked, a new set of variables is created. When a function calls itself, the new invocation gets its own set of variables, and thus does not “clobber” those being used by the original invocation. Imagine that somebody, somewhere, calls our recursive factorial method with “3” as the argument (as shown below). The following page illustrates the resulting series of function calls. Note that each function invocation gets its own “n” to play with. i = factorial (3);
n = 3 1st invocation argument = 3 result = 6 “i = factorial (3)” executed n = 2 2nd invocation argument = 2 result = 2 “factorial(n - 1)” evaluated n = 1 3rd invocation argument = 1 result = 1 “factorial(n - 1)” evaluated n = 0 4th invocation argument = 0 result = 1 “factorial(n - 1)” evaluated

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
“Playing computer” is probably not the best way of coming to grips with recursion. Trying to follow the details is in fact a recipe for getting hopelessly confused. Instead a more abstract view is called for. In this case, we might make the following observations: - It is obvious that the method works for n = 0. - If the method works for n = 0, it will also work for n = 1.
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 13

Chapter12 - Chapter 12 Recursion Recursion occurs when a...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online