Recursion Output Input

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Programming with Recursion
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursion.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
M180: Data Structures & Algorithms in Java
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Sequential & Object oriented Programming
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Chapter 8 Recursion Modified.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Program Development and Design Using C++, Third Edition
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursion Powerful Tool
Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Recursion Version 1.0.
to understand recursion you must understand recursion
Topic: Recursion – Part 2
Topic 6 Recursion.
Recursion what is it? how to build recursive algorithms
Introduction to Recursion
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion DRILL: Please take out your notes on Recursion
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
To understand recursion, you have to understand recursion!
Recursion, Tail Recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
to understand recursion you must understand recursion
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Functions Inputs Output
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursive Definitions
Lecture 17 Recursion part 1 Richard Gesick.
Fundamentals of Programming
Module 1-10: Recursion.
Lecture 12 Recursion part 1 CSE /26/2018.
Chapter 18 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
Recursive Thinking.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Lecture 6 - Recursion.
Presentation transcript:

Recursion Output Input Recursion is a programming concept whereby a function invokes itself. Recursion is typically used to solve problems that are decomposable into subproblems that are just like the original problem, but a step closer to being solved.

Recursion w/out a Base Case void foo(string str) { printf(“%s\n”, str); foo(str); } Here’s a recursive function called foo. What happens when this function is called? foo will print a string and call itself, which will print another string and call itself again, which will print another string and call itself yet again... and on and on infinitely. However, every time a function is called, some space is set aside in the stack called a frame and every function call results in a new frame being created. So, if foo calls itself infinitely, we’ll eventually run out of stack memory and try to access memory that doesn’t belong to us, in which case our program will fail with a segmentation fault. To avoid this situation, we need to make sure there’s always a way to break out of recursive calls -- we’ll call this the base case. When the base condition is met, the function should return without making a recursive call. Let’s make sure we have a base case in the next example :)

Factorial n! = n * (n - 1) * (n - 2) * … * 1 For our next example, let’s think about the factorial operation. The factorial operation is a perfect candidate for recursion because it is a problem that can easily be broken up into similar smaller problems! 5! = 5 * 4 * 3 * 2 * 1 So if we want to calculate the factorial of 5, we can think of it as multiplying 5 by the factorial of 4: 5! = 5 * 4! Similarly, to calculate the factorial of 4, we multiply 4 by the factorial of 3: 4! = 4 * 3! And so on... Let’s implement a factorial function in C!

unsigned int factorial(unsigned int n) { if (n <= 1) return 1; } else return n * factorial(n - 1); This recursive function calculates the factorial of its integer argument, and unlike the last function we saw, this one has a base case! Think through what happens when you pass this function an argument of 3: factorial(3) returns 3 * factorial(2) factorial(2) returns 2 * factorial(1) factorial(1) is the base case and returns 1

factorial(3) = 3 * factorial(2) 1 Once the base case is reached, all of the factorial() calls will return in succession to give us an answer of 6. Let’s see what’s going on in the stack as this recursive function executes!

heap factorial(3) main() stack Let’s zoom in on the stack as this function executes: main() calls factorial(3) Every function call results in a new frame being created. These frames are arranged on top of each other in the stack, with the frame for the most recently called function (the active frame) at the top. factorial(3) main() stack

heap factorial(2) factorial(3) main() stack main() calls factorial(3) factorial(3) calls factorial(2) factorial(2) is now the active frame. factorial(3) main() stack

heap factorial(1) factorial(2) factorial(3) main() stack main() calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) is now the active frame. factorial(3) main() stack

heap 1 factorial(2) factorial(3) main() stack main() calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) returns 1 to factorial(2) factorial(1) returns 1 to factorial(2). Its frame is destroyed, and the one for the function below (in this case factorial(2)) becomes active again. factorial(3) main() stack

heap 2 factorial(3) main() stack main() calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) returns 1 to factorial(2) factorial(2) multiplies 1 * 2 and returns 2 to factorial(3) factorial(2) returns 2 to factorial(2). Its frame is destroyed, and the one for the function below (in this case factorial(3)) becomes active again. factorial(3) main() stack

heap 6 main() stack main() calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) returns 1 to factorial(2) factorial(2) multiplies 1 * 2 and returns 2 to factorial(3) factorial(3) multiplies 2 * 3 and returns 6 to main() factorial(3) returns 6 to main(). Its frame is destroyed, and the one for the function below (in this case main()) becomes active again. Nice! Our function calculated the correct answer! 3! = 3 * 2 * 1 = 6 6 main() stack