in swap function, we are sending variables simply and it looks it is being passed by reference how. Is it following a pass by reference by default?
Perameter passing
Yes. The swap function follows a pass by reference as the swapped values are reflected in the main program.
but how it is implementing this functionality…it is not same with the normal function?
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a=5, b=10;
cout<<a<<" “<<b<<endl;
swap(a,b);
cout<<a<<” "<<b<<endl;
}
Output of this code will be -
5 10
10 5
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.