Functions 331
p rin tf ("Sum %d,r, * s ) ;
return 0;
}
*sum( )
I
int x,y,z,k;
printf("n Enter three values
scanf ("% d %d %d,&x,&y&z);
k & y + z ;
return (&k);
}
Explanation The function sum () is declared as a pointer function, that is, the function declared
as a pointer always returns a reference. The reference returned by the function sum () is assigned
to pointer *s. The pointer *s prints the sum.
10.6 CALL BY VALUE AND REFERENCE
There are two ways by which we can pass arguments to the function.
1) Call by value
2) Call by reference
1) Call by value: In this type values of the actual argument are passed to the formal argument
and operation is done on the formal arguments. Any change in the formal argument made, does
not affect the actual arguments because formal arguments are a photocopy of the actual argument.
Hence, when a function is called by call by value method, it does not affect the actual contents of
the actual arguments. Changes made in the formal arguments are local to the block of the called
function. Once control returns back to the calling function, the changes made vanish. The
following example illustrates the use of call by value.
10.15 Write a program to send values by call by value.
#in clude<8tdio. h>
#include<conio.h>
mainO
{
int x ,y,c h a n g e (in t,in t);
c lra c r ();
printf("n Enter Values of X & Y
scanf("%d %d",&x,&y);
change(x,y);
print! ("n In mainO X=%d Y=%d",x,y);
return 0;
I
change(int a, int b)
332 Programming and Data Structures
{
int k;
k=a;
a=b;
b=k;
p r in tf (" n In changeQ X=%d Y=%d",a,b);
I
OUTPUT;
Enter Values of X & Y: 5 4
Explanation In the above program we are passing values of actual arguments 'x' and 'y' to the
function change (). The formal arguments 'a' and V of function change () receive these va lu e s ()
functions. The values are interchanged, that is, the value of 'a' is assigned to 'b' and vice versa and
printed. When the control returns back to the main (), the changes made in function change ()
vanish. In the main () the values of V and 'y' are printed as they read from the keyboard. In call
by value method, the formal argument acts as a photocopy of the actual argument. Hence, the
changes made are temporary.
1. Call by reference: In this type instead of passing values, addresses (references) are passed. The
function operates on addresses rather than values. Here the formal arguments are pointers to the
actual argument. In this type formal arguments point to the actual argument. Hence changes made
in the axgument are permanent. The following example illustrates call by reference.
10.16 Write a program to send a value by reference to the user-defined function.
#include<stdio.h>
#include<conio.h>
main ()
{
in t x ,y ,c h ang e(in t*, in t* );
c lr s c r ();
printf ("n Enter Values of X & Y :");
scanf ("%d %d",&x/&y);
chattge(&x,&y);
printf ("n In mainO X=%d Y=%d",x,y);
return 0;
}
changetfnt *a, int *b)
{
int *k;
*k=*ti;
*a=*b;
*b=*k;
printf ( In changeO X=%d Y-%d",*a,*b);
i
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.54.136