1 / 42

Functions-Recall

Functions-Recall. Parameter passing &amp; return. void main() { int x=10, y=5; printf (“M1: x = %d, y = %d<br>”, x, y); interchange (x, y); printf (“M2: x = %d, y = %d<br>”, x, y); } void interchange (int x, int y) { int temp; printf (“F1: x = %d, y = %d<br>”, x, y);

aretha
Télécharger la présentation

Functions-Recall

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions-Recall

  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); } Output M1: x = 10, y = 5 F1: x = 10, y = 5 F2: x = 5, y = 10 M2: x = 10, y = 5

  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. Local Variables Return Value Return Addr • Pop activation record, whenever the function returns • Activation record looks like:

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

  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); } int fact (int n) { ……… return (result); } 3 times 3 times LV2, RV2, RA2 LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1 Before call Call ncr Call fact fact returns ncr returns

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

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

  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); } int fact (int n) { ……… return (result); } 3 times 3 times LV2, RV2, RA2 LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1 Before call Call ncr Call fact fact returns ncr returns

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

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

  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); } int fact (int n) { ……… return (result); } 3 times 3 times LV2, RV2, RA2 LV1, RV1, RA1 LV1, RV1, RA1 LV1, RV1, RA1 Before call Call ncr Call fact fact returns ncr returns

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

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

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

  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. fact(4) Factorial Execution long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

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

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

  27. 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)); Factorial Execution long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); }

  28. 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)); Factorial Execution long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } if (1 = = 1) return (1);

  29. 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 Factorial Execution long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } if (1 = = 1) return (1);

  30. 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 Factorial Execution 2 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } if (1 = = 1) return (1);

  31. 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 Factorial Execution 2 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } if (1 = = 1) return (1);

  32. 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 Factorial Execution 6 2 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } if (1 = = 1) return (1);

  33. fact(4) 24 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 Factorial Execution 6 2 long int fact (int n) { if (n = = 1) return (1); else return (n * fact(n-1)); } if (1 = = 1) return (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. Local Variables Return Value Return Addr • 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:

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

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

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

  39. Fibonacci Numbers 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] }

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

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

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

More Related