This preview shows pages 1–11. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS111: Slides for Prof. Steinberg s lecture 1 Lecture 22 CS111: Intro to Computer CS111: Intro to Computer Science Science Lecture 22 Recursion CS111: Slides for Prof. Steinberg s lecture 2 Lecture 22 No class Thanksgiving week No class Thanksgiving week There will be no class Monday because it is a Rutgers Wednesday There will be no class Thursday because it is Thanksgiving CS111: Slides for Prof. Steinberg s lecture 3 Lecture 22 Recursion Recursion Recursion is a way of looking at a problem EG problem: print pattern like * * * * * * * * * * CS111: Slides for Prof. Steinberg s lecture 4 Lecture 22 Recursion Recursion Nonrecursive view * * * * * * * * * * A size 4 triangle is four lines, lengths 1, 2, 3, 4 CS111: Slides for Prof. Steinberg s lecture 5 Lecture 22 Recursion Recursion Recursive view * * * * * * * * * * A size 4 triangle is A size 3 triangle, followed by A line of length 4 CS111: Slides for Prof. Steinberg s lecture 6 Lecture 22 Recursive Definitions in Math Recursive Definitions in Math Factorial n! = n * (n1)! 1! = 1 e.g., 3! = 3*2! = 3 * (2 * 1!) = 3 * (2 * 1) = 6 Definition looks circular, but is not Two parts: recursive case base case CS111: Slides for Prof. Steinberg s lecture 7 Lecture 22 Seeing recursion look at a problem as if it were pregnant: inside it is a small version of the same problem What is N! ? What is (n1)! ? CS111: Slides for Prof. Steinberg s lecture 8 Lecture 22 Method Call Method Call What happens when a method calls itself? The same things that happen when it calls any other method: Evaluate arguments in the call Set up new invocation record with parameters Start at beginning of called procedure, go to end Continue the calling procedure, right after the call See CallExample.java CS111: Slides for Prof. Steinberg s lecture 9 Lecture 22 Note about Note about CallByExample CallByExample Things done before recursive call are done in order (done shallow to deep) Things done after the recursive call are done in reverse order (done deep to shallow) CS111: Slides for Prof. Steinberg s lecture 10 Lecture 22 How Do NonRecursive calls How Do NonRecursive calls Work?...
View
Full
Document
This document was uploaded on 11/01/2011 for the course 198 111 at Rutgers.
 Fall '10
 STEINBERG

Click to edit the document details