Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.

Slides:



Advertisements
Similar presentations
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Advertisements

 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
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.
Prof. S.M. Lee Department of Computer Science. Answer:
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
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.
1 Storage Classes, Scope, and Recursion Lecture 6.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
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.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
IB Computer Science Unit 5 – Advanced Topics Recursion.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
 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.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
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.
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
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
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Functions Course conducted by: Md.Raihan ul Masood
5.13 Recursion Recursive functions Functions that call themselves
RECURSION.
C Functions -Continue…-.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
JavaScript: Functions Part II
Chapter 6 - Functions Outline 5.1 Introduction
Functions Recursion CSCI 230
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Dale Roberts, Lecturer IUPUI
Programming Fundamentals Lecture #7 Functions
Recursion.
Presentation transcript:

Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts Recursion Recursive functions A function can invoke any other function; “ Can a function invoke itself?” Functions that call themselves either directly or indirectly (through another function) is called a recursive function. If a function is called with a simple case, the function actually knows how to solve the simplest case(s) - “base case” and simply returns a result. If a function is called with a complex case - “recursive case”, it invokes itself again with simpler actual parameters. 1.must resembles original problem but slightly simplified 2.function will call itself (recursion step or recursive call) to solve slightly simplified problem 3.process continues until the last recursive call is with the base case 4.that call returns the result of the base case 5.return to previous calling functions 6.finally reaches main.c. Eventually base case gets solved Gets plugged in, works its way up and solves whole problem

Dale Roberts Recursion (cont.) Example: Compute x y for x > 0 & y > 0 x y = x * x * x * … * x base case: x 1 = x  x y = x 1 * ( x * x * … * x) = x 1 * ( x 1 * ( x * x * … * x )) = x 1 * ( x 1 * ( x 1 * ( x * x * … * x ))) = x 1 * x y-1 ex. x 4 = x 1 * ( x 1 * ( x 1 * ( x 1 ))) Fundamental rules of recursion 1.) Base cases 2.) Making progress through recursion 3.) Design rule: assuming all recursive call work (details hidden) 4.) Always specify the base case; otherwise, indefinite recursive will occur and cause “stack-overflow” error. x1x1 x1x1 x1x1 x1x1 * * * x1x1 x3x3 x2x2 x1x1 x2x2 x3x3 x4x4 x4x * * * x1x1 x2x y times

Dale Roberts Recursion: #include int x_pow_y(int, int); main() { printf(“enter x and y: \n”); scanf(“%d, %d”, x, y); z = x_pow_y(x,y); printf(“z: %d\n”, z); } x_pow_y(int a, int b) { if (b==1) return a; else return (a * x_pow_y(a, b-1)) } Recursion vs. Iteration Iteration: x_pow_y = 1; for (i = y; i >=1; i--) x_pow_y*=x; If x = 5, y = 4 x_pow_y = 1, x = 5, y = 4, i = 4; 1.) x_pow_y = 5*1 = 5, i = 3; 2.) x_pow_y = 5*5 = 25, i = 2; 3.) x_pow_y = 25*5 = 125, i = 1; 4.) x_pow_y = 125*5 = 625, i = 0; … x=5; y=4; x_pow_y(5,4); … is (4==1)?  no … is (3==1)?  no … is (2==1)?  no … is (1==1)?  yes  return 5 x_pow_y(5,4)x_pow_y(5,3)x_pow_y(5,2)x_pow_y(5,1) Base Case main

Dale Roberts Example Using Recursion: Factorial Example: factorials: 5! = 5 * 4 * 3 * 2 * 1 Notice that 5! = 5 * 4! 4! = 4 * 3! … Can compute factorials recursively Solve base case ( 1! = 0! = 1 ) then plug in 2! = 2 * 1! = 2 * 1 = 2; 3! = 3 * 2! = 3 * 2 = 6; long factorial(int n) { if (n <= 1) return 1; else return n * factorial(n-1); }

Dale Roberts Example Using Recursion: The Fibonacci Series Example: Fibonacci series : F k = F k-1 + F k-2, F 0 = 1, F 1 = 1 ex: 0, 1, 1, 2, 3, 5, 8… Each number is the sum of the previous two Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) Set of recursive calls to function fibonacci Code for the fibonacci function long fibonacci(long n) { if (n <= 1) if (n <= 1) return n; return n;else; return fibonacci (n-1) + fibonacci (n-2); } f( 3 ) f( 1 ) f( 2 ) f( 1 )f( 0 )return 1 return 0 return + +

Dale Roberts 1/* Fig. 5.15: fig05_15.c 2 Recursive fibonacci function */ 3#include 4 5long fibonacci( long ); 6 7int main() 8{8{ 9 long result, number; printf( "Enter an integer: " ); 12 scanf( "%ld", &number ); 13 result = fibonacci( number ); 14 printf( "Fibonacci( %ld ) = %ld\n", number, result ); 15 return 0; 16} 17 18/* Recursive definition of function fibonacci */ 19long fibonacci( long n ) 20{ 21 if ( n == 0 || n == 1 ) 22 return n; 23 else 24 return fibonacci( n - 1 ) + fibonacci( n - 2 ); 25} Enter an integer: 0 Fibonacci(0) = 0 Enter an integer: 1 Fibonacci(1) = 1 Enter an integer: 2 Fibonacci(2) = 1 Enter an integer: 3 Fibonacci(3) = 2 Enter an integer: 4 Fibonacci(4) = 3 Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 10 Fibonacci(10) = 55 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = Enter an integer: 35 Fibonacci(35) = Function prototype 2. Declare variables 3. Input an integer 4. Call function fibonacci 5. Output results. 6. Define fibonacci recursively

Dale Roberts Recursion vs. Iteration Both are based on the control structures Repetition (Iteration): explicitly uses repetition (loop). Selection (Recursion): implicitly use repetition by successive function calls Both involve termination Iteration: loop condition fails Recursion: base case recognized Both can lead infinite loops Loop termination is not met Base case is not reached Balance Choice between performance (iteration) and good software engineering (recursion)

Dale Roberts Recursion vs. Iteration Any problem that can be solved recursively can also be solved iteratively with a stack. A recursion is chosen in preference over iteration while the recursive approach more naturally mirrors the problem and results in a program that easier to understand and debug. Recursion has an overhead of repeated function calls which is expensive in terms of processor time and memory usage. Another reason to choose recursion is that an iterative solution may not apparent. If used properly a recursive approach can lead to smaller code size and elegant program (at the case of performance penalty.)

Dale Roberts Acknowledgements