functions-recall
DESCRIPTION
Functions-Recall. 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); - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/1.jpg)
Functions-Recall
1
![Page 2: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/2.jpg)
2
Parameter passing & returnvoid 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
![Page 3: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/3.jpg)
3
Parameter passing & returnvoid 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
![Page 4: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/4.jpg)
4
Parameter passing & returnint 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
![Page 5: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/5.jpg)
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
![Page 6: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/6.jpg)
6
Pop activation record, whenever the function returns
Activation record looks like:
Return Addr
Return Value
Local Variables
![Page 7: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/7.jpg)
7
void main(){ …….. x = gcd (a, b); ……..}
int gcd (int x, int y){ …….. …….. return (result);}
Return Addr
Return Value
Local Variables
Before call After call After return
ST
AC
K
Activation record
![Page 8: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/8.jpg)
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 call Call fact ncr returns
int fact (int n){ ……… return (result);}
3 times
LV1, RV1, RA1
fact returns
LV1, RV1, RA1
LV2, RV2, RA2
Call ncr
3 times
![Page 9: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/9.jpg)
Push activation record
9
Return Addr
Return Value
a, b (Local x Variables)
main
![Page 10: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/10.jpg)
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
![Page 11: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/11.jpg)
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 call Call fact ncr returns
int fact (int n){ ……… return (result);}
3 times
LV1, RV1, RA1
fact returns
LV1, RV1, RA1
LV2, RV2, RA2
Call ncr
3 times
![Page 12: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/12.jpg)
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
![Page 13: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/13.jpg)
Pop activation record
13
Return Addr
Return Value
Local Variables
Return Addr
Return Value
Local Variables
main
nCr
![Page 14: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/14.jpg)
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 call Call fact ncr returns
int fact (int n){ ……… return (result);}
3 times
LV1, RV1, RA1
fact returns
LV1, RV1, RA1
LV2, RV2, RA2
Call ncr
3 times
![Page 15: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/15.jpg)
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
![Page 16: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/16.jpg)
Pop activation record
16
Return Addr
Return Value
Local xVariables
Return Addr
Return Value p
Local Variables
main
Return
nCr
![Page 17: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/17.jpg)
Pop activation record
17
Return Addr
Return Value
a, b (Local x Variables)
main
![Page 18: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/18.jpg)
18
Recursion
![Page 19: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/19.jpg)
19
Recursion
A process by which a function calls itself repeatedlyEither directly.
X calls XOr 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)
![Page 20: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/20.jpg)
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 dataThe problem statement must include a
stopping condition
fact(n) = 1, if n = 0 = n * fact(n-1), if n > 0
Stopping condition
Recursive definition
![Page 21: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/21.jpg)
21
Examples:
Factorial:fact(0) = 1fact(n) = n * fact(n-1), if n > 0
Fibonacci series (1,1,2,3,5,8,13,21,….)fib (0) = 1fib (1) = 1fib (n) = fib (n-1) + fib (n-2), if n > 1
![Page 22: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/22.jpg)
22
Factorial
long int fact (int n)
{
if (n == 1)
return (1);
else
return (n * fact(n-1));
}
![Page 23: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/23.jpg)
23
Factorial Execution
long int fact (int n){ if (n = = 1) return (1); else return (n * fact(n-1));}
![Page 24: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/24.jpg)
24
Factorial Execution fact(4)
long int fact (int n){ if (n = = 1) return (1); else return (n * fact(n-1));}
![Page 25: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/25.jpg)
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));}
![Page 26: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/26.jpg)
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));}
![Page 27: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/27.jpg)
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));}
![Page 28: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/28.jpg)
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));}
![Page 29: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/29.jpg)
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));}
![Page 30: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/30.jpg)
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));}
![Page 31: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/31.jpg)
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));}
![Page 32: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/32.jpg)
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));}
![Page 33: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/33.jpg)
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));}
![Page 34: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/34.jpg)
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 itselfSeveral function calls going on, with none of the
function calls returning back Activation records are pushed onto the stack continuously Large stack space required
![Page 35: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/35.jpg)
35
Activation records keep popping off, when the termination condition of recursion is reached
We shall illustrate the process by an example of computing factorialActivation record looks like:
Return Addr
Return Value
Local Variables
![Page 36: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/36.jpg)
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) );
}
![Page 37: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/37.jpg)
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
![Page 38: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/38.jpg)
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
![Page 39: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/39.jpg)
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
![Page 40: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/40.jpg)
40
fib (5)
fib (3) fib (4)
fib (1)
fib (2)fib (1) fib (2)
fib (0)
fib (3)
fib (1)
fib (1) fib (2)
fib (0)
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) ;}
![Page 41: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/41.jpg)
41
fib (5)
fib (3) fib (4)
fib (1)
fib (2)fib (1) fib (2)
fib (0)
fib (3)
fib (1)
fib (1) fib (2)
fib (0)
fib (0) fib (1)
1 1 1 1 1
1
11
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
![Page 42: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/42.jpg)
42
fib (5)
fib (3) fib (4)
fib (1)
fib (2)fib (1) fib (2)
fib (0)
fib (3)
fib (1)
fib (1) fib (2)
fib (0)
fib (0) fib (1)
1 1 1 1 1
1
11
1 2 2
211111
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) ;}
![Page 43: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/43.jpg)
43
Mergesort
![Page 44: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/44.jpg)
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)
![Page 45: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/45.jpg)
45
Merging Two Sorted Arrays
3 4 7 8 9
2 5 7
2
3 4 7 8 9
2 5 7
2 3
3 4 7 8 9
2 5 7
2 3 4
3 4 7 8 9
2 5 7
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.
![Page 46: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/46.jpg)
46
2 3 4 5
3 4 7 8 9
2 5 7
2 3 4 5 7
3 4 7 8 9
2 5 7
2 3 4 5 7 7
3 4 7 8 9
2 5 7
2 3 4 5 7 7 8
3 4 7 8 9
2 5 7
![Page 47: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/47.jpg)
47
Merge Code
2 3 4 5 7 7 8 9
3 4 7 8 9
2 5 7
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++];
}
![Page 48: Functions-Recall](https://reader036.vdocument.in/reader036/viewer/2022062517/56813b28550346895da3e9c4/html5/thumbnails/48.jpg)
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);
}