Subscribe Us

Swap the variables using call by value, call by Address and call by reference (Code Block)

Ques:
Swap the variables using call by value, call by Address and call
by reference (Code Block)


#include <iostream>

using namespace std;
void swap1(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"\nAfter Swapping :"<<a<<"\t"<<b;
}
void swap2(int *c, int *d)
{
int temp;
temp=*c;
*c=*d;
*d=temp;
}
void swap3(int &e, int &f)
{
int temp;
temp=e;
e=f;
f=temp;
}
int main()
{
int a,b,c,d,e,f;
cout<<"\nEnter the numbers to swap using Call by value:\n";
cin>>a;
cin>>b;
cout << "\n\nBefore Swapping :"<<a<<"\t"<<b;
swap1(a,b);
cout<<"\n\nEnter the numbers to swap using Call by Reference:\n";
cin>>c;
cin>>d;
cout << "\n\nBefore Swapping:"<<c<<"\t"<<d;
swap2(&c,&d);
cout << "\n After Swapping:"<<c<<"\t"<<d;
cout<<"\n\nEnter the numbers to swap using Call by Address:\n"
cin>>e;
cin>>f;
cout << "\n\nBefore Swapping:"<<e<<"\t"<<f;
swap3(e,f);
cout << "\n After Swapping:"<<e<<"\t"<<f;
return 0;

}

Post a Comment

0 Comments