C program to implement "Call By Refrence"
C program for "Call By Reference"
The address of memory location A and B are passed to the function swap and the pointers *x and *y accept those values.
When, the value of pointers are changed, the value in the pointed memory location also changes correspondingly.
Hence, changes made to
*x
and *y are reflected in A and B in the main function.
This technique is known as Call by Reference in C programming.

Happy Coding :)
#include <stdio.h>
#include <conio.h>
void swapref(int*,int*); //Declaration of the swap value by refrence function
void main()
{
int a,b; //Initialization of variable
clrscr();
printf("\n\nSwaping value using Call By Reference\n");
//Fetch values of A & B from the user
printf("Enter the first value A: ");
scanf("%d",&a);
printf("Enter the second value B: ");
scanf("%d",&b);
printf("\nValue inside the main function:\n");
printf("A= %d B= %d",a,b);
//Calling the refrence function
swapref(&a,&b);
getch();
}
void swapref(int *x,int *y)
{
int temp; //Declare temp variable to store data temporary
//Swapping the values
temp=*x;
*x=*y;
*y=temp;
printf("\nValue inside swap function:\n");
printf("A= %d B= %d",*x,*y); //Getting the output on the screen
}
Happy Coding :)
Comments
Post a Comment