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