Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:

Slides:



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

Recursion.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Binary search example postponed to end of lecture.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
Some Advanced Features of Procedures. Recursion Recursive Calls –A procedure can call itself (Self Recursion) –A can call B, B calls C, etc, Z calls A.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
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.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Analysis of Algorithms Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Sorting. 2 Sorting Data Items Consider a set of data items  Each item may have more than one field Example: a student record with name, roll no, CGPA,…
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
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.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
Recursion Function calling itself
Recursion.
Programming and Data Structures
Recursion CENG 707.
Recursion CENG 707.
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.
Chapter 17 Recursion.
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 17 Recursion.
Recursion Data Structures.
7.Recursion Recursion is the name given for expression anything in terms of itself. Recursive function is a function which calls itself until a particular.
Chapter 17 Recursion.
Module 1-10: Recursion.
Sorting.
Java Programming: Chapter 9: Recursion Second Edition
Last Class We Covered Recursion Stacks Parts of a recursive function:
Chapter 17 Recursion.
Presentation transcript:

Functions-Recall 1

2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2: x = %d, y = %d\n”, x, y); } void interchange (int x, int y) { int temp; printf (“F1: x = %d, y = %d\n”, x, y); temp= x; x = y; y = temp; printf (“F2: x = %d, y = %d\n”, x, y); } Output

3 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2: x = %d, y = %d\n”, x, y); } void interchange (int x, int y) { int temp; printf (“F1: x = %d, y = %d\n”, x, y); temp= x; x = y; y = temp; printf (“F2: x = %d, y = %d\n”, x, y); } M1: x = 10, y = 5 F1: x = 10, y = 5 F2: x = 5, y = 10 M2: x = 10, y = 5 Output

4 Parameter passing & return int x=11,y=6; void interchange (int a, int b); int main() { { int x=6,y=11; interchange (x,y); } printf("x=%d y=%d",x,y); } void interchange (int x, int y) { int temp; temp=x; x=y; y=temp; } Homework

5 How are function calls implemented? The following applies in general, the implementation details of function call  The system maintains a stack in memory Stack is a last-in first-out structure Two operations on stack, push and pop  Whenever there is a function call, the activation record gets pushed into the stack Activation record consists of the return address in the calling program, the return value from the function, and the local variables inside the function

6  Pop activation record, whenever the function returns  Activation record looks like: Return Addr Return Value Local Variables

7 void main() { …….. x = gcd (a, b); …….. } int gcd (int x, int y) { …….. return (result); } Return Addr Return Value Local Variables Before callAfter call After return STACK Activation record

8 void main() { …….. x = ncr (a, b); …….. } int ncr (int n, int r) { x=fact(n); y=fact(r); z=fact(n-r); p=x/(y*z) return (p); } LV1, RV1, RA1 Before callCall factncr returns int fact (int n) { ……… return (result); } 3 times LV1, RV1, RA1 fact returns LV1, RV1, RA1 LV2, RV2, RA2 Call ncr 3 times

Push activation record 9 Return Addr Return Value a, b (Local x Variables) main

Push activation record 10 Return Addr Return Value a, b (Local x Variables) Return Addr Return Value n, r, p (Local Variables) main nCr Parameter passing

11 void main() { …….. x = ncr (a, b); …….. } int ncr (int n, int r) { x=fact(n); y=fact(r); z=fact(n-r); p=x/(y*z) return (p); } LV1, RV1, RA1 Before callCall factncr returns int fact (int n) { ……… return (result); } 3 times LV1, RV1, RA1 fact returns LV1, RV1, RA1 LV2, RV2, RA2 Call ncr 3 times

Push activation record 12 Return Addr Return Value a, b (Local x Variables) Return Addr Return Value n, r (Local Variables) Return Addr Return Value n, result (Local Variables) result x main nCr Parameter passing Return fact

Pop activation record 13 Return Addr Return Value Local Variables Return Addr Return Value Local Variables main nCr

14 void main() { …….. x = ncr (a, b); …….. } int ncr (int n, int r) { x=fact(n); y=fact(r); z=fact(n-r); p=x/(y*z) return (p); } LV1, RV1, RA1 Before callCall factncr returns int fact (int n) { ……… return (result); } 3 times LV1, RV1, RA1 fact returns LV1, RV1, RA1 LV2, RV2, RA2 Call ncr 3 times

Push activation record 15 Return Addr Return Value Local Variables Return Addr Return Value Local Variables Return Addr Return Value Local Variables result y main fact nCr

Pop activation record 16 Return Addr Return Value Local x Variables Return Addr Return Value p Local Variables main Return nCr

Pop activation record 17 Return Addr Return Value a, b (Local x Variables) main

18 Recursion

19 Recursion 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 Used for repetitive computations in which each action is stated in terms of a previous result fact(n) = n * fact (n-1)

20 Contd. For a problem to be written in recursive form, two conditions are to be satisfied:  It should be possible to express the problem in recursive form Solution of the problem in terms of solution of the same problem on smaller sized data  The problem statement must include a stopping condition fact(n) = 1, if n = 0 = n * fact(n-1), if n > 0 Stopping condition Recursive definition

21 Examples:  Factorial: fact(0) = 1 fact(n) = n * fact(n-1), if n > 0  Fibonacci series (1,1,2,3,5,8,13,21,….) fib (0) = 1 fib (1) = 1 fib (n) = fib (n-1) + fib (n-2), if n > 1

22 Factorial long int fact (int n) { if (n == 1) return (1); else return (n * fact(n-1)); }

23 Factorial Execution long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

24 Factorial Execution fact(4) long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

25 Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

26 Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

27 Factorial Execution fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

28 Factorial Execution if (1 = = 1) return (1); fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

29 Factorial Execution if (1 = = 1) return (1); fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); 1 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

30 Factorial Execution if (1 = = 1) return (1); fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); 1 2 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

31 Factorial Execution if (1 = = 1) return (1); fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); 1 2 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

32 Factorial Execution if (1 = = 1) return (1); fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

33 Factorial Execution if (1 = = 1) return (1); fact(4) if (4 = = 1) return (1); else return (4 * fact(3)); if (3 = = 1) return (1); else return (3 * fact(2)); if (2 = = 1) return (1); else return (2 * fact(1)); long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

34 What happens for recursive calls? What we have seen ….  Activation record gets pushed into the stack when a function call is made  Activation record is popped off the stack when the function returns In recursion, a function calls itself  Several function calls going on, with none of the function calls returning back Activation records are pushed onto the stack continuously Large stack space required

35  Activation records keep popping off, when the termination condition of recursion is reached We shall illustrate the process by an example of computing factorial  Activation record looks like: Return Addr Return Value Local Variables

36 Example:: main() calls fact(3) int fact (n) int n; { if (n = = 0) return (1); else return (n * fact(n-1)); } void main() { int n; n = 3; printf (“%d \n”, fact(n) ); }

37 TRACE OF THE STACK DURING EXECUTION fact returns to main RA.. main - n = 3 RA.. main - n = 3 RA.. fact - n = 2 RA.. main - n = 3 RA.. fact - n = 2 RA.. fact - n = 1 RA.. main - n = 3 RA.. fact - n = 2 RA.. fact - n = 1 RA.. fact 1 n = 0 RA.. main - n = 3 RA.. fact - n = 2 RA.. fact 1*1 = 1 n = 1 RA.. main - n = 3 RA.. fact 2*1 = 2 n = 2 RA.. main 3*2 = 6 n = 3 main calls fact

38 Look at the variable addresses (a slightly different program) ! void main() { int x,y; scanf("%d",&x); y = fact(x); printf ("M: x= %d, y = %d\n", x,y); } int fact(int data) { int val = 1; printf("F: data = %d, &data = %u \n &val = %u\n", data, &data, &val); if (data>1) val = data*fact(data-1); return val; } 4 F: data = 4, &data = &val = F: data = 3, &data = &val = F: data = 2, &data = &val = F: data = 1, &data = &val = M: x= 4, y = 24 Output

39 Fibonacci recurrence: fib(n) = 1 if n = 0 or 1; = fib(n – 2) + fib(n – 1) otherwise; int fib (int n){ if (n == 0 or n == 1) return 1; [BASE] return fib(n-2) + fib(n-1) ; [Recursive] } Fibonacci Numbers

40 fib (5) fib (3)fib (4) fib (1) fib (2)fib (1)fib (2) fib (0) fib (3) fib (1) fib (2) fib (0) fib (1) Fibonacci recurrence: fib(n) = 1 if n = 0 or 1; = fib(n – 2) + fib(n – 1) otherwise; int fib (int n){ if (n == 0 || n == 1) return 1; return fib(n-2) + fib(n-1) ; }

41 fib (5) fib (3)fib (4) fib (1) fib (2)fib (1)fib (2) fib (0) fib (3) fib (1) fib (2) fib (0) fib (1) Fibonacci recurrence: fib(n) = 1 if n = 0 or 1; = fib(n – 2) + fib(n – 1) otherwise; int fib (int n){ if (n == 0 || n == 1) return 1; return fib(n-2) + fib(n-1) ; } fib.c

42 fib (5) fib (3)fib (4) fib (1) fib (2)fib (1)fib (2) fib (0) fib (3) fib (1) fib (2) fib (0) fib (1) Fibonacci recurrence: fib(n) = 1 if n = 0 or 1; = fib(n – 2) + fib(n – 1) otherwise; int fib (int n){ if (n==0 || n==1) return 1; return fib(n-2) + fib(n-1) ; }

43 Mergesort

44 Basic Idea Divide the array into two halves Sort the two sub-arrays Merge the two sorted sub-arrays into a single sorted array Step 2 (sorting the sub-arrays) is done recursively (divide in two, sort, merge) until the array has a single element (base condition of recursion)

45 Merging Two Sorted Arrays Problem : Two sorted arrays A and B are given. We are required to produce a final sorted array C which contains all elements of A and B.

47 Merge Code void merge (int A[], int B[], int C[], int m,int n) { int i=0,j=0,k=0; while (i<m && j<n) { if (A[i] < B[j]) C[k++] = A[i++]; else C[k++] = B[j++]; } while (i<m) C[k++] = A[i++]; while (j<n) C[k++] = B[j++]; }

48 Merge Sort: Sorting an array recursively void mergesort (int A[], int n) { int i, j, B[max]; if (n <= 1) return; i = n/2; mergesort(A, i); mergesort(A+i, n-i); merge(A, A+i, B, i, n-i); for (j=0; j<n; j++) A[j] = B[j]; free(B); }