Functions Recursion CSCI 230

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.
 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.
 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.
© 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 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.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
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.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
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.
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
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
 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.
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.
Functions Course conducted by: Md.Raihan ul Masood
Chapter 15 Recursion.
Introduction to Recursion
5.13 Recursion Recursive functions Functions that call themselves
RECURSION.
C Functions -Continue…-.
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Chapter 15 Recursion.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
6.11 Function Call Stack and Activation Records
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Functions Declarations CSCI 230
MSIS 655 Advanced Business Applications Programming
Important Notes Remaining Schedule: recall the 2 bad weather days (Jan 8th, Jan 17th) we will need to make up. In providing the 2 weeks needed for the.
JavaScript: Functions Part II
Chapter 6 - Functions Outline 5.1 Introduction
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Pointers Call-by-Reference CSCI 230
Lesson #6 Modular Programming and Functions.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Recursive Algorithms 1 Building a Ruler: drawRuler()
Dale Roberts, Lecturer IUPUI
Programming Fundamentals Lecture #7 Functions
Recursion.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Functions Recursion CSCI 230 Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Functions Recursion Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu

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

Recursion (cont.) Fundamental rules of recursion Example: Compute x y for x > 0 & y > 0 x y = x * x * x * … * x base case: x1 = x  x y = x1 * ( x * x * … * x) = x1 * ( x1 * ( x * x * … * x )) = x1 * ( x1 * ( x1 * ( x * x * … * x ))) = x1 * x y-1 ex. x 4 = x1 * ( x1 * ( x1 * ( x1 ))) 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. x1 * x3 x2 x4 5 51 53 52 125 625 54 25 y times

Recursion vs. Iteration #include <stdio.h> 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)) 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)? is (2==1)? 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 5 25 125 625

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); }  

Example Using Recursion: The Fibonacci Series Example: Fibonacci series: Fk = Fk-1 + Fk-2, F0 = 1, F1 = 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) return n; else; return fibonacci(n-1) + fibonacci(n-2); }   f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +

4. Call function fibonacci 5. Output results. 1 /* Fig. 5.15: fig05_15.c 2 Recursive fibonacci function */ 3 #include <stdio.h> 4 5 long fibonacci( long ); 6 7 int main() 8 { 9 long result, number; 10 11 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 */ 19 long 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) = 832040 Enter an integer: 35 Fibonacci(35) = 9227465 1. Function prototype 2. Declare variables 3. Input an integer 4. Call function fibonacci 5. Output results. 6. Define fibonacci recursively

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)

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.)

Acknowledgements