Download presentation
Presentation is loading. Please wait.
Published byLeslie McKenzie Modified over 9 years ago
1
Functions-Recall 1
2
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
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
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
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
6 Pop activation record, whenever the function returns Activation record looks like: Return Addr Return Value Local Variables
7
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
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
9
Push activation record 9 Return Addr Return Value a, b (Local x Variables) main
10
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
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
12
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
13
Pop activation record 13 Return Addr Return Value Local Variables Return Addr Return Value Local Variables main nCr
14
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
15
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
16
Pop activation record 16 Return Addr Return Value Local x Variables Return Addr Return Value p Local Variables main Return nCr
17
Pop activation record 17 Return Addr Return Value a, b (Local x Variables) main
18
18 Recursion
19
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
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
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
22 Factorial long int fact (int n) { if (n == 1) return (1); else return (n * fact(n-1)); }
23
23 Factorial Execution long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }
24
24 Factorial Execution fact(4) long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }
25
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
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
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
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
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
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
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
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)); 1 2 6 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }
33
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)); 1 2 6 24 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }
34
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
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
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
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
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 = 3221224528 &val = 3221224516 F: data = 3, &data = 3221224480 &val = 3221224468 F: data = 2, &data = 3221224432 &val = 3221224420 F: data = 1, &data = 3221224384 &val = 3221224372 M: x= 4, y = 24 Output
39
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
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
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) 1 11 1 1 1 1 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
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) 1 11 1 1 1 1 1 1 2 2 2 1111 1 3 3 5 8 1 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
43 Mergesort
44
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
45 Merging Two Sorted Arrays 34789 257 2 34789 257 23 34789 257 234 34789 257 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.
46
46 2345 34789 257 23457 34789 257 234577 34789 257 2345778 34789 257
47
47 Merge Code 23457789 34789 257 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
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); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.