Swapping of Two Numbers in C++ Using Functions | Call by Reference & Call by Value
belongs to collection: examples of Function In C++ Programming
All Answers
need an explanation for this answer? contact us directly to get an explanation for this answer
Swapping of Two Numbers in C++ Using Call by Reference | Functions
#include<iostream>
using namespace std;
void swap(int *x ,int *y );
/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/
int main()
{
int a,b;
cout<<"Enter Two Numbers To Swap: ";
cin>>a>>b;
swap(&a,&b);
cout<<"\nAfter Swapping Two Numbers: ";
cout<<a<<" "<<b<<" \n";
return 0;
}
void swap(int *x,int *y)
{
int z;
z=*x;
/*Copying the first variable Address to the tempriory variable*/
*x=*y;
/*Copying the second variable Address to the first variable*/
*y=z;
/*Copying the tempriory variable Address to the second variable*/
}
Output:
Enter Two Numbers To Swap: 256 512
After Swapping Two Numbers: 512 256
need an explanation for this answer? contact us directly to get an explanation for this answertotal answers (2)

C++ programming
Program Output of Swapping of Two Numbers Using Call by Value
Output:
Enter the Two Numbers to Swap in C++: 512 256
After Swapping of Two Numbers: 256 512
need an explanation for this answer? contact us directly to get an explanation for this answer