Sunday, February 22, 2015

Swapping Via Pass-by-Reference and Pass-by-Pointer

There is a function swap in this code, which we use to swap two numbers. If we define the function as swap(x, y) and call it as swap(a, b), the swapping done in the function will not reflect inside the main function. Why? Because it is pass-by-value method. While we call the function, only the copies of the arguments are passed to the function. Changes made in these copies are local to the function. So we should send the addresses of the real arguments (variables), so that the function can handle the original ones. There are two methods in C++ to accomplish this: 1) pass-by-reference and 2) pass-by-pointer.
In pass-by-reference method, we define the function as swap(&x, &y) and call it as swap(a, b). We can handle those variables as normal variables inside the function. All we had done is adding and ampersand(&) sign in front of the variables in the function header. Things like sending the memory addresses will be done by the compiler automatically. That's why pass-by-reference is known as implicit method.
In pass-by-pointer method, we define the function as swap(*x, *y) and call it as swap(&a, &b). We should handle those variables as pointer variables inside the function. i.e., x = y will become *x = *y. Here, things like sending the memory addresses and decoding them is done by the programmer. That is why pass-by-pointer is known as explicit method.

1. Code -- Pass-by-Reference

#include <iostream>
using namespace std;

void swap(int &x, int &y)
{
    int tmp;

    tmp  = y;
    y   = x;
    x   = tmp;
}

int main()
{
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << endl;
    cout << "Before swapping: a = " << a
         << " and b = " << b << endl;
    swap(a, b);
    cout << "After swapping: a = " << a
         << " and b = " << b << endl;

    return 0;
}

2. Code -- Pass-by-Pointer

#include <iostream>
using namespace std;

void swap(int *x, int *y)
{
    int tmp;

    tmp  = *y;
    *y   = *x;
    *x   = tmp;
}

int main()
{
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << endl;
    cout << "Before swapping: a = " << a
         << " and b = " << b << endl;
    swap(&a, &b);
    cout << "After swapping: a = " << a
         << " and b = " << b << endl;

    return 0;
}

Output of 1 and 2

Enter two numbers: 10 20

Before swapping: a = 10 and b = 20
After swapping: a = 20 and b = 10

3. Code -- Pass-by-Value (which won't work)

#include <iostream>
using namespace std;

void swap(int x, int y)
{
    int tmp;

    tmp  = y;
    y   = x;
    x   = tmp;
}

int main()
{
    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << endl;
    cout << "Before swapping: a = " << a
         << " and b = " << b << endl;
    swap(a, b);
    cout << "After swapping: a = " << a
         << " and b = " << b << endl;

    return 0;
}

Output

Enter two numbers: 10 20

Before swapping: a = 10 and b = 20
After swapping: a = 10 and b = 20

NOTE: Turbo C++ users please read this (click here).

No comments:

Post a Comment