call by reference in c++
/*In call by reference we pass (&)address as argument or parameter in definition of function and while calling the function we pass only variables of the original value.
*/
#include<iostream>
using namespace std;
void swap(int &,int &);
int main(){
int a,b;
cout<<"Enter A and B "<<endl;
cin>>a>>b;
cout<<"Before swapping ";
cout<<" A "<<a<<" B "<<b;
swap(a,b);
cout<<" After swapping ";
cout<<" A "<<a<<" B "<<b;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
output-
Enter A and B
9
5
Before swapping A 9 B 5 After swapping A 5 B 9
*/
#include<iostream>
using namespace std;
void swap(int &,int &);
int main(){
int a,b;
cout<<"Enter A and B "<<endl;
cin>>a>>b;
cout<<"Before swapping ";
cout<<" A "<<a<<" B "<<b;
swap(a,b);
cout<<" After swapping ";
cout<<" A "<<a<<" B "<<b;
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
output-
Enter A and B
9
5
Before swapping A 9 B 5 After swapping A 5 B 9
Comments
Post a Comment