C++ Swap Function

Swap function is used to interchange two values in variables with each other.

For example variable x contains 10 and y contains 20, after swap, x contains 20 and y contains 10. C++ has a inbuild swap function i.e, swap(x,y).

Without using this inbuild function we can swap two values in C++ many ways as discussed below.

1. Swap By Value

The call by value method pass arguments to a function which actually copy the actual value of an argument into the formal parameter of the function. In this case, the argument have no effect if any changes happen to the parameter inside the function. By default, C++ uses call by value to pass arguments.

#include “iostream”
#include <conio.h>
using namespace std;
//call by value
void swap (int a, int b)
{
  int t;
  t = a;
  a = b;
  b = t;
}

int main()
{
 int x = 4,y = 7;
 cout << "Before Swap:" << endl;
 cout << "x=" << x << "\ty=" << y << endl;
 swap (x, y);
 cout << "After Swap:" << endl;
 cout << "x=" << x << "\ty=" << y;
 getch();
}

2. Swap By Pointer Reference

The call by pointer reference method pass arguments to a function which copy the address of an argument into the formal parameter of the function. The pointer is used inside the function to access the address of actual argument used in the call. Hence, the changes made to the parameter affect the passed argument.

#include "iostream"
#include <conio.h>

using namespace std;
//reference by pointer
void swap (int *a, int *b) 
{
 int t;
 t = *a;
 *a = *b;
 *b = t;
}

int main()
{
 int x = 4, y = 7;
 cout << "Before Swap:" << endl;
 cout << "x=" << x << "\ty=" << y <<endl;
 swap (&x, &y);
 cout << "After Swap:" << endl;
 cout << "x=" << x << "\ty=" <<y;
 getch();
}

3. Swap By Reference Address

Calling a value by directly passing the address of an actual argument is called by address. So, pointer is not needed here to indicate the location of the argument. But, the changes made to the parameter affect the passed argument.

#include "iostream"
#include <conio.h>
using namespace std;

//reference by address
void swap (int &a, int &b)
{
     int t;
     t = a;
     a = b;
     b = t;
}

int main()
{
    int x = 4, y = 7;
    cout << "Before Swap:" << endl;
    cout << "x=" << x << "\ty=" << y << endl;
       swap (x, y);
    cout << "After Swap:" << endl;
    cout << "x=" << x << "\ty=" << y;
    getch();
}

4. Swap By Function Template

You can see, each argument of function required particular data type of the values so if you have to write separate function for the values of different data types.

Here is a solution, Function templates allow you to write a single function definition that works with many different data types, instead of having to write a separate function for each data type used.

#include "iostream"
#include <conio.h>
using namespace std;
template <class T>

//Function Template
T swap (T a, T b)                 
{
    T temp = a;
    a = b;
    b = temp;
    return a,b;
}

int main()
{
    int x = 4,y = 7;
    cout << "Before Swap:" << endl;
    cout << "x=" << x << "\ty=" << y << endl;
       swap (&x, &y);
    cout << "After Swap:" << endl;
    cout << "x=" << x << "\ty=" << y;
    getch();
}

Leave a Reply