8.call by Reference
8.call by Reference
Call by reference
• Instead of passing value, the address of the argument will be passed.
• Any changes to the formal argument will affect the actual argument.
Example
#include <stdio.h>
#include<conio.h>
int main()
{
int x,y;
int swap(int*,int*);
clrscr();
printf("\nEnter value of x:");
scanf("%d",&x);
printf("\nEnter value of y:");
scanf("%d",&y);
swap(&x,&y);
printf("\n\nValues in the Main() : x=%d,y=%d",x,y);
getch();
}
int swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
printf("\nValues in the User Defined Function : x=%d,y=%d",*a,*b);
return;
}
Output
Enter value of x:5
Enter value of y:6
Values in the User Defined Function : x=6,y=5
Values in the Main() : x=6,y=5
SNS COLLEGE OF TECHNOLOGY, COIMBATORE –35
(An Autonomous Institution)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING