Download presentation
Presentation is loading. Please wait.
Published byMarjory Oliver Modified over 9 years ago
1
Recursion A function that calls itself
2
Recursion A function which calls itself is said to be recursive. Recursion is a technique which will allow us to solve certain problems that would be difficult to solve in other ways.
3
Recursion To illustrate recursion, let's consider the mathematical concept of factorial (the expression n! is read "n factorial"). Here are two ways to define the factorial of a non-negative integer n:
4
Recursion Note that 0! is defined to be 1. Note also that in the recursive definition, factorial is defined in terms of itself!
5
Recursion Using the iterative definition, we would calculate 5! as
6
Recursion Using the iterative definition, we would calculate 5! as 5! = 1 * 2 * 3 * 4 * 5 = 120
7
Recursion Using the iterative definition, we would calculate 5! as 5! = 1 * 2 * 3 * 4 * 5 = 120 Using the recursive definition, 5! is calculated as 5! = 5 * 4! = 5 * 4 * 3! = 5 * 4 * 3 * 2! = 5 * 4 * 3 * 2 * 1! = 5 * 4 * 3 * 2 * 1 * 0! = 5 * 4 * 3 * 2 * 1 * 1 = 120
8
Recursion Here is a C++ function that implements the iterative definition of factorial. Note that it uses a loop to do the iteration: int Factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }
9
Recursion Here is a C++ function that implements the recursive definition of factorial. Note that it contains no loop: int RecursiveFactorial(int n) { if (n == 0) { return 1; }else { return n * RecursiveFactorial(n - 1); }
10
Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions:
11
Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked.
12
Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case.
13
Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0.
14
Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0. –If the base case test evaluates to true, recursion ends with the return of the value 1.
15
Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0. –If the base case test evaluates to true, recursion ends with the return of the value 1. –If the base case condition is false, an expression involving a recursive call is returned.
16
Recursion RecursiveFactorial illustrates the structure that is common to all recursive functions: –First, a stopping condition is checked. –This is called the base case. In the case of RecursiveFactorial, the base case is n == 0. –If the base case test evaluates to true, recursion ends with the return of the value 1. –If the base case condition is false, an expression involving a recursive call is returned. In the case of RecursiveFactorial, the returned expression is n * RecursiveFactorial(n - 1).
17
The figure below illustrates how RecursiveFactorial calculates the value of 3!:
18
Recursion By the way, recursion is not the technique to use for calculating the factorial of n. –The iterative factorial function is much more efficient and should be used instead. –This example was used because its recursive calculation is easy to implement and understand. As you continue your study of computer science you will see some very elegant recursive solutions to problems.
19
As a second example of a recursive function, let’s look at the following recursive definition for a n :
20
This definition is implemented as the Power function, which returns an integral power of a number: double Power(double base, int expo){ if (0 == expo) { return 1.0; // x 0 = 1 } else { double semi = Power(base,expo/2); if (expo % 2 == 0) { // even exponent return semi * semi; } else { // odd exponent return base*semi*semi; }
21
Recursion Note again that, like all "good" recursive functions, Power begins by testing a stopping condition.
22
Recursion Note again that, like all "good" recursive functions, Power begins by testing a stopping condition. –Only if the stopping condition is false does Power call itself recursively.
23
Here are general rules for writing recursive functions: 1. Identify a base case that does not make any recursive calls. – Each call should make progress toward reaching the base case; – this ensures termination since the function will end.
24
Here are general rules for writing recursive functions: 1. Identify a base case that does not make any recursive calls. – Each call should make progress toward reaching the base case; – this ensures termination since the function will end. 2. Solve the problem by making recursive calls that are similar but simpler (i.e., that move toward the base case). –The similarity ensures that the recursion works: –you'll be solving a similar problem.
25
Here are general rules for writing recursive functions: 1. Identify a base case that does not make any recursive calls. – Each call should make progress toward reaching the base case; – this ensures termination since the function will end. 2. Solve the problem by making recursive calls that are similar but simpler (i.e., that move toward the base case). –The similarity ensures that the recursion works: –you'll be solving a similar problem. Note that RecursiveFactorial and Power satisfy both rules.
26
Okay, let's see how well you understand recursion: Determine the values returned by each of the following function calls by hand-executing the functions: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); }
27
Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) b. Problem1(13) c. Problem1(x), where x is a nonnegative integer
28
Problem1(8) int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n-2); } Problem1(8) = 1 + Problem1(6) = 1 + 1 + Problem1(4) = 1 + 1 + 1 + Problem1(2) = 1 + 1 + 1 + 1 + Problem1(0) = 1 + 1 + 1 + 1 + 0 = 4
29
Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) = 4 b. Problem1(13) c. Problem1(x), where x is a nonnegative integer
30
Problem1(13) int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n-2); } Problem1(13) = 1 + Problem1(11) = 1 + 1 + Problem1(9) = 1 + 1 + 1 + Problem1(7) = 1 + 1 + 1 + 1 + Problem1(5) = 1 + 1 + 1 + 1 + 1 + Problem1(3) = 1 + 1 + 1 + 1 + 1 + 1 + Problem1(1) = 1 + 1 + 1 + 1 + 1 + 1 + 0 = 6
31
Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) = 4 b. Problem1(13) = 6 c. Problem1(x), where x is a nonnegative integer
32
Okay, let's see how well you understand recursion: int Problem1(int n) { if (n <= 1) return 0; else return 1 + Problem1(n - 2); } What value is returned from the calls a. Problem1(8) = 4 b. Problem1(13) = 6 c. Problem1(x), where x is a nonnegative integer = x/2
33
Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) }
34
Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) } What value is returned from the calls a. Problem2(12, 8) = Problem2(4, 8) = Problem2(4, 4) = 4
35
Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) } What value is returned from the calls b. Problem2(18, 24) = Problem2(6, 18) = Problem2(6, 12) = Problem2(6, 6) = 6
36
Greatest Common Divisor int Problem2(int m, int n) { if (m == n) return m; else if (m < n) return Problem2(n - m, m); else return Problem2(m - n, n) } What value is returned from the calls c. Problem2(x, y), where x and y are positive integers, returns the greatest common divisor of x and y
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.