Saturday 31 August 2013

Call by reference | C Programming Language Tutorial



Call by reference :

In this type instead of passing values, addresses are passed. Function operates on addresses rather than values. Here the formal arguments are pointers to the actual arguments. In this type , formal arguments point to the actual argument. Hence changes made in the arguments are permanent.
Ex: A program to send a value by reference to the user defined function.
main ( )
{
int x,y ,change (int *, int *);
clrscr();
printf (“/n Enter values of x& y” );
scanf (“%d %d “, & x, & y);
change (&x, &y);
printf ( “/n in main ( ) x =%d % d y = ./. d “, x,y);
return 0;
}
change (int * a, int * b)
{
int * k;
*k = *a;
*a = * b;
*b = * K;
printf (“ /n in chage ( ) x =% d y=%d”, *a, *b);
}
Enter values of x & y 5 4
In change ( ) x = 4 y = 5

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved