Lecture 5 Functions Pass by Value and Pass by Reference ...
Transcript of Lecture 5 Functions Pass by Value and Pass by Reference ...
![Page 1: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/1.jpg)
Lecture 5 – Functions
Pass by Value and Pass by Reference
Recursion
![Page 2: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/2.jpg)
Differences between Value Parameters and
Reference Parameters
Recursion
![Page 3: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/3.jpg)
![Page 4: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/4.jpg)
![Page 5: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/5.jpg)
MAIN PROGRAM MEMORY
25
4000
age
If you pass only a copy of 25 to a
function, it is called “pass-by-value” and the function will not be able to
change the contents of age. It is still
25 when you return.
![Page 6: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/6.jpg)
MAIN PROGRAM MEMORY
BUT, if you pass 4000, the address of
age to a function, it is called “pass-
by-reference” and the function will be
able to change the contents of age. It
could be 23 or 90 when you return.
25
4000
age
![Page 7: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/7.jpg)
Value Parameter Reference Parameter
The value (25) of the
argument
is passed
to the function when
it is called.
The memory address (4000)
of the argument
is passed
to the function when
it is called.
25
4000
age
Argument
in Calling Block
![Page 8: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/8.jpg)
![Page 9: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/9.jpg)
reference parameters should be used
when you want your function to give a
value to, or change the value of, a
variable from the calling block.
Return more than one variables.
![Page 10: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/10.jpg)
CALLING
BLOCK FUNCTION
CALLED
“incoming”
value of
argument
![Page 11: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/11.jpg)
CALLING
BLOCK FUNCTION
CALLED
“incoming”
original value of
argument
“outgoing”
changed value of
argument
OR,
![Page 12: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/12.jpg)
CALLING
BLOCK FUNCTION
CALLED
argument
has no value yet
when call occurs
“outgoing”
new value of
argument
![Page 13: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/13.jpg)
13
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
![Page 14: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/14.jpg)
14
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
![Page 15: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/15.jpg)
15
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
3 5
![Page 16: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/16.jpg)
16
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
badSwap ( a, b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables. */
void badSwap ( int a, int b )
{
int temp;
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
}
Output: 3 5
5 3
3 5
![Page 17: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/17.jpg)
17
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables. */
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
![Page 18: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/18.jpg)
18
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
![Page 19: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/19.jpg)
19
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
5 3
![Page 20: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/20.jpg)
20
int main()
{
int a = 3, b = 5;
printf("%d %d\n", a, b);
Swap ( &a, &b );
printf("%d %d\n", a, b);
return 0;
}
/* Swap the values of two variables.
*/
void Swap ( int *a, int *b )
{
int temp;
temp = *a;
*a = *b;
*b = temp;
printf("%d %d\n", *a, *b);
}
Output: 3 5
5 3
5 3
![Page 21: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/21.jpg)
We want to find 2 real roots for a quadratic
equation with coefficients a,b,c. Write a
prototype for a void function named
GetRoots( ) with 5 parameters. The first 3
parameters are type float. The last 2 are
reference parameters of type float.
![Page 22: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/22.jpg)
#include <stdio.h>
#include <math.h> void GetRoots(float, float, float, float*, float*);
void main ( )
{
float a, b, c, first, second;
printf(“ Enter a b c : “); scanf(“%f %f %f”, &a , &b, &c ); GetRoots(a, b, c, &first, &second);
printf(“ Roots are %.2f %.2f \n “ , first , second );
}
// function GetRoots goes here
……..
![Page 23: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/23.jpg)
Function Definition
![Page 24: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/24.jpg)
In C programming, a single array element or an
entire array can be passed to a function.
Also, both one-dimensional and multi-
dimensional array can be passed to function as
argument.
![Page 25: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/25.jpg)
#include <stdio.h>
void display(int a)
{ printf("%d",a);}
int main()
{int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;}
Output : 4
![Page 26: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/26.jpg)
While passing arrays to the argument, the name
of the array is passed as an argument (i.e.,
starting address of memory area is passed as
argument).
![Page 27: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/27.jpg)
Write a C program to pass an array containing
age of 6 persons to a function. This function
should find average age and display the
average age in main function.
![Page 28: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/28.jpg)
#include <stdio.h> float average(float a[]); int main() { float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18}; avg=average(c);/* Only name of array is passed as argument. */ printf("Average age=%.2f",avg); return 0; } float average(float a[]) {int i;float avg, sum=0.0; for(i=0;i<6;i++) {sum+=a[i];} avg =(sum/6); return avg; }
![Page 29: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/29.jpg)
To pass two-dimensional array to a function as
an argument, starting address of memory area
reserved is passed as in one dimensional array.
![Page 30: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/30.jpg)
#include <stdio.h>
void Function(int c[2][2]);
int main()
{int c[2][2],i,j;
printf("Enter 4 numbers:\n");
for(i=0;i<2;++i)for(j=0;j<2;++j)
{scanf("%d",&c[i][j]);}
Function(c); /*passing multi-dimensional array to function */
return 0;
}
void Function(int c[2][2])
{/* Instead to above line, void Function(int c[][2]){ is also valid */
int i,j;
printf("Displaying:\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
printf("%d\n",c[i][j]);
}
![Page 31: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/31.jpg)
![Page 32: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/32.jpg)
What is recursion?
When one function calls ITSELF directly or
indirectly.
A function is a recursive if a statement in the
body of the function calls the function that
contains it.
![Page 33: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/33.jpg)
Why learn recursion?
New mode of thinking.
Powerful programming tool.
Divide-and-conquer paradigm.
Many computations are naturally self- referential
![Page 34: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/34.jpg)
A recursive definition is made up of two parts. ◦ Base case that tells us directly what the answer is.
◦ Recursive case that defines the answer in terms of the answer to a subproblem.
For example, in factorial: ◦ Base case is factorial(0)=1.
◦ Recursive case is factorial(n) = n * factorial(n-1).
![Page 35: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/35.jpg)
Recursive Function Multiply
int multiply ( int m, int n)
{
int ans;
If (n==1)
ans=m;
else
ans= m + multiply (m, n-1);
return (ans);
}
![Page 36: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/36.jpg)
m is 6
n is 3
3 == 1 is false
ans is 6 + multiply (6,2)
return (ans)
m is 6
n is 2
2 == 1 is false
ans is 6 + multiply (6,1)
return (ans)
m is 6
n is 1
1 == 1 is true
ans is 6
return (ans)
6
12
18
multiply (6,3) int multiply ( int m, int n) { int ans; If (n==1) ans=m; else ans= m + multiply (m, n-
1); return (ans); }
![Page 37: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/37.jpg)
When a function calls itself, new local variables
are allocated storage on the stack, and the
function code is executed with these new
variables from the beginning .
A recursive call does not make a new copy of
the function. Only the arguments are new.
![Page 38: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/38.jpg)
#include <stdio.h>
int sum(int n)
{ if(n==0)
return n;
else return n+sum(n-1);
}
int main()
{ int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
![Page 39: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/39.jpg)
#include <stdio.h>
int sum(int n);
int main()
{ int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n)
{ if(n==0)
return n;
else return n+sum(n-1);
}
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
![Page 40: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/40.jpg)
Functions are very often defined recursively. The classic example is the factorial function. ◦ factorial(0) = 1 ◦ factorial(n) = n * factorial(n-1) [for n>0]
Let's compute factorial(3). ◦ factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 * factorial(0) = 3 * 2 * 1 * 1 = 6
![Page 41: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/41.jpg)
int factorial(int n)
{
if (n == 0) return 1 ;
else return n * factorial(n-1) ;
}
![Page 42: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/42.jpg)
Base Cases: fib(0) = 0, fib(1) = 1
Recursive Case (two recursive calls): fib(n) = fib(n-1) + fib(n-2) [for n>1]
fib(4)= fib(3) + fib(2)
= fib(2) + fib(1) + fib(1) + fib(0)
= fib(1) + fib (0) + 1 + 1 + 0
= 1 +0 + 1 + 1 + 0
= 3
![Page 43: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/43.jpg)
int fib(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
else return (fib(n-1) + fib(n-2));
}
43
![Page 44: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/44.jpg)
F(X) = 2 *X + F ( X – 2) ;
F(0) = F(1) = 0;
![Page 45: Lecture 5 Functions Pass by Value and Pass by Reference ...](https://reader034.fdocuments.in/reader034/viewer/2022042822/62696cc5d4bc46466c54db76/html5/thumbnails/45.jpg)