In call by reference mechanism, instead of passing values to the function being called, reference/pointers to the original variables are passed.
Example: Program interchange values of two variables by call by reference mechanism.
#include<iostream.h>
#include<conio.h>
void swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
cout<<"Swapped Values are a is--->"<<*x<<"And b="<<*y;
int main( )
{
int a = 7, b = 4;
cout<<"Original Value are a = "<<a<<"And b ="<<b;
swap(&a, &b);
cout<<"The values after swap are a = "<<a<<"and b = ""<<b;
}
Output:-
Original Values are a = 7 and b = 4
Swapped values are a = 4 and b = 7
The values after swap are a = 4 and b = 7
Example: Program interchange values of two variables by call by reference mechanism.
#include<iostream.h>
#include<conio.h>
void swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
cout<<"Swapped Values are a is--->"<<*x<<"And b="<<*y;
int main( )
{
int a = 7, b = 4;
cout<<"Original Value are a = "<<a<<"And b ="<<b;
swap(&a, &b);
cout<<"The values after swap are a = "<<a<<"and b = ""<<b;
}
Output:-
Original Values are a = 7 and b = 4
Swapped values are a = 4 and b = 7
The values after swap are a = 4 and b = 7
0 Comments:
Post a Comment