Interchange Value Using Call By Value / Reference


Program to interchange value using call by value or call by reference in C++









#include<iostream.h>
#include<conio.h>
void swapV(int a,int b)
{
          int c;
          c=a;
          a=b;
          b=c;
}

void swapR(int &a,int &b)
{
          int c;
          c=a;
          a=b;
          b=c;
}

void main()
{
          clrscr();
          int x,y;
          cout<<"\nEnter two numbers :: ";
          cin>>x>>y;
          cout<<"\n\nOriginal Values ::\nx="<<x<<"\ty="<<y;

          swapV(x,y);
          cout<<"\n\nAfter Interchanging using Call by Value Method ::\nx="<<x<<"\ty="<<y;

          swapR(x,y);
          cout<<"\n\nAfter Interchanging using Call by Reference Method ::\nx="<<x<<"\ty="<<y;
          getch();
}


OUTPUT

Enter Two Numbers :: 2  3

Original Values ::
x=2 y=3

After Interchanging Using Call By Value Method ::
x=2 y=3

After Interchanging Using Call By Reference Method ::
x=3 y=2


-----

Firoz Memon

Please view my other blogs:

          C++ Codes 4 Beginners

          Java Tips

          Java 4 Beginners

Previous Post Next Post