Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

7 7 Quiz Function call stack Passing values to functions Recursion.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
8 8 Function call stack Passing values to functions 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
計算機程式 第五單元 Function II 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,
 Pearson Education, Inc. All rights reserved Recursion.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Chapter 8 Recursion Modified.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
 2005 Pearson Education, Inc. All rights reserved Recursion.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 18 Recursion & Pointers in Java.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Java How to Program, 10/e Late Objects Version © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
RECURSION.
Recursion CSC 202.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
15 Recursion.
6.11 Function Call Stack and Activation Records
MSIS 655 Advanced Business Applications Programming
Lecture 17 Recursion part 1 Richard Gesick.
JavaScript: Functions Part II
Functions Recursion CSCI 230
Recursion Taken from notes by Dr. Neil Moore
Lecture 12 Recursion part 1 CSE /26/2018.
Fundaments of Game Design
Programming Fundamentals Lecture #7 Functions
Recursion.
Presentation transcript:

Functions and an Introduction to Recursion

 Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)  Recursion ◦ Base case(s)  The simplest case(s), which the function knows how to handle ◦ For all other cases, the function typically divides the problem into two conceptual pieces  A piece that the function knows how to do  A piece that it does not know how to do  Slightly simpler or smaller version of the original problem

 Consider the following function.  How to compute f(4) ?  Discuss: ◦ What does this function actually compute? ◦ What if f(1) =1 is not defined?

◦ Recursive call (also called the recursion step)  The function launches (calls) a fresh copy of itself to work on the smaller problem  Can result in many more recursive calls, as the function keeps dividing each new problem into two conceptual pieces  This sequence of smaller and smaller problems must eventually converge on the base case  Otherwise the recursion will continue forever

 Although we don’t know the steps of how to solve f(n) directly, we find a way to break the size of problem into smaller pieces and solve them individually.

6  Factorial ◦ The factorial of a nonnegative integer n, written n! (and pronounced “ n factorial ” ), is the product  n · (n – 1) · (n – 2) · … · 1 ◦ Recursive definition of the factorial function  n! = n · (n – 1)!  Example  5! = 5 · 4 · 3 · 2 · 1 5! = 5 · ( 4 · 3 · 2 · 1) 5! = 5 · ( 4! )

7

First call to factorial function

Base cases simply return 1 Recursive call to factorial function with a slightly smaller problem

 Either omitting the base case, or writing the recursion step incorrectly so that it does not converge on the base case, causes “ infinite ” recursion, eventually exhausting memory. This is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution.

factorial(5) number Return value Return address main() factorial(4) factorial(3) factorial(2) factorial(1) Function Call Stack  Discuss: ◦ What if there is no base case?

 The Fibonacci series ◦ 0, 1, 1, 2, 3, 5, 8, 13, 21, … ◦ Begins with 0 and 1 ◦ Each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers ◦ can be defined recursively as follows:  fibonacci(0) = 0  fibonacci(1) = 1  fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)

Recursive calls to fibonacci function Base cases

16  Caution about recursive programs ◦ Each level of recursion in function fibonacci has a doubling effect on the number of function calls  i.e., the number of recursive calls that are required to calculate the nth Fibonacci number is on the order of 2 n  20th Fibonacci number would require on the order of 2 20 or about a million calls  30th Fibonacci number would require on the order of 2 30 or about a billion calls. ◦ Exponential complexity  Can humble even the world ’ s most powerful computers

17  Avoid Fibonacci-style recursive programs that result in an exponential “ explosion ” of calls.

18  Both are based on a control statement ◦ Iteration – repetition structure ◦ Recursion – selection structure  Both involve repetition ◦ Iteration – explicitly uses repetition structure ◦ Recursion – repeated function calls  Both involve a termination test ◦ Iteration – loop-termination test ◦ Recursion – base case

19  Both gradually approach termination ◦ Iteration modifies counter until loop-termination test fails ◦ Recursion produces progressively simpler versions of problem  Both can occur infinitely ◦ Iteration – if loop-continuation condition never fails ◦ Recursion – if recursion step does not simplify the problem

Iterative approach to finding a factorial

 Negatives of recursion ◦ Overhead of repeated function calls  Can be expensive in both processor time and memory space ◦ Each recursive call causes another copy of the function (actually only the function ’ s variables) to be created  Can consume considerable memory  Iteration ◦ Normally occurs within a function ◦ Overhead of repeated function calls and extra memory assignment is omitted

 Any problem that can be solved recursively can also be solved iteratively (nonrecursively).  A recursive approach is normally chosen in preference to an iterative approach when the recursive approach more naturally results in a program that is easier to understand and debug. ◦ Another reason to choose a recursive solution is that an iterative solution is not apparent.

 Performance Tip 6.9 ◦ Avoid using recursion in performance situations. Recursive calls take time and consume additional memory.  Common Programming Error 6.26 ◦ Accidentally having a nonrecursive function call itself, either directly or indirectly (through another function), is a logic error.