call by address in c++
/*In call by address we pass pointer(*) in the argument in the difinition and while calling the function we pass address of the variable*/
#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<<endl;
swap(&a,&b);//here we are passing address of a and b thats why we are using & operator with a and b.
cout<<"After swapping ";
cout<<" A "<<a<<" B "<<b;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
#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<<endl;
swap(&a,&b);//here we are passing address of a and b thats why we are using & operator with a and b.
cout<<"After swapping ";
cout<<" A "<<a<<" B "<<b;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
output-
Enter A and B
9
6
Before swapping A 9 B 6
After swapping A 6 B 9
Comments
Post a Comment