184k views
3 votes
Write a program in C++ to swap two variables usingfunctions?

User Linuxatico
by
5.5k points

1 Answer

3 votes

Answer:

#include <iostream>

using namespace std;

void swap(int& m, int& n) /* passing by reference so that changes will be made in the original values.*/

{

int t=m;

m=n;

n=t;

}

int main()

{

int val1,val2;

cout<<"Enter the values"<<endl;

cin>>val1>>val2;

cout<<"Values before swap "<<val1<<" "<<val2<<endl;

swap(val1,val2); //calling the function swap..

cout<<"Values after swap "<<val1<<" "<<val2<<endl;

return 0;

}

Step-by-step explanation:

Created a function swap with 2 arguments m and n passed by reference.

User Jakobk
by
5.3k points