Download presentation
Presentation is loading. Please wait.
1
Computer Science 2
2
Learning Objectives Be able to dry run a program that has functions.
Understand how recursion works and be able to write a program that uses recursion. See #2
3
program sample; {****************************************} procedure tale; begin Write('Twas a dark and stormy night, '); writeln( 'and the captain said to his crew, '); write(‘ "Gather round, and I''ll tell ye a tale.“ '); writeln('So the crew gathered round, '); writeln(' and the captain said: '); readln; tale; end; {***************************************} end.
4
Recursion quotes. "In order to understand recursion, one must first understand recursion." Definition Recursion If you still don't get it, See: "Recursion".
5
Recursion Recursion: When a subroutine calls itself.
When designing a recursive function it is imperative to include the following four elements: a well-defined BASE CASE, a RECURSIVE CALL within the body of the function, a guaranteed SMALLER PROBLEM as a result of the recursive call, and a guarantee that the BASE CASE IS REACHABLE
6
When To Use Recursion • When a problem can be divided into steps.
• The result of one step can be used in a previous step. • There is scenario when you can stop sub-dividing the problem into steps and return to previous steps. • All of the results together solve the problem.
7
When To Consider Alternatives To Recursion
• When a loop will solve the problem just as well • Types of recursion: • Tail recursion — A recursive call is the last statement in the recursive module. — This form of recursion can easily be replaced with a loop. • Non-tail recursion — A statement which is not a recursive call to the module comprises the last statement in the recursive module. —This form of recursion is very difficult to replace with a loop.
8
Drawbacks Of Recursion
Function/procedure calls can be costly • Uses up memory • Uses up time
9
Benefits Of Using Recursion
• Simpler solution that’s more elegant (for some problems) • Easier to visualize solutions (for some people and certain classes of problems – typically require either: non-tail recursion to be implemented or some form of “backtracking”)
10
Common Pitfalls When Using Recursion
•These three pitfalls can result in a segmentation fault (using up too much memory) occurring • No base case • No progress towards the base case • Using up too many resources (e.g., variable declarations) for each function call
11
Reading a program with Recursion
program Confusion; function a(b:integer):integer; begin if b<= 1 then a:=b else a:= b + a(b-1); end; writeln(a(6)); readln; end.
12
Applying Recursion Powers ab Base Case Smaller Problem Recursive Call
an = a * a(n-1) Recursive Call power = base * power(exponent - 1)
13
Example Program Recursion;
function pow(base,exponent:integer):integer; begin if exponent>=1 then pow:= base*pow(base,exponent-1) else pow:=1; end; a:=3; b:=4; writeln(pow(a,b)); end. Recursive call Smaller Problem Base Case
14
Dry run program sumthing; function sum (no : integer): integer; begin
if (no = 1) then sum := 1 else sum:= no - sum (no - 1); end; var total : integer; total := sum(5); writeln(total); end. Dry run
15
Program Factorial using recursion Input: 10 names into an array
Input: A positive integer Output: It’s factorial: Example: Input 6 Output: 6 ! = 6*5*4*3*2*1 = 720 Input: 10 names into an array Output: The names in reverse order recursively, in the subroutine Finding the Greatest Common Factor using the Euclidean Algorithm If b goes into a, then the GCF(a,b) is b If b does not go into a, then GCF(a,b) = GCF(b, a MOD b) Example: GCF(28,8) since 28 MOD 8 = 4 GCF(8,4), since 4 goes into 8, GCF(8,4) = 4. GCF(28,8) = 4 Other examples:
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.