POINTERS AND REFERENCE AS AN ARGUMENTS

Sometimes the function can't be implemented using the pass by value approach. For example, let's try to write the function which will change value of two variables. Now let's se a function which will replace the value of two variables:
#include "stdafx.h"
#include "iostream"
using namespace std;

void switch1(int first, int second) {
       int third = first;
       first = second;
       second = third;
}


int _tmain(int argc, _TCHAR* argv[])
{
       int a, b;
       cout << "Enter a value of variable a: " << endl;
       cin >> a;
       cout << "Enter a value of variable b:" << endl;
       cin >> b;
       cout << "The value of variable a before the change is:" << a << endl;
       cout << "The value of variable b before the change is:" << b << endl;
       switch1(a, b);
       cout << "The value of variable a after the change is: " << a << endl;
       cout << "The value of variable b after the change is:" << b << endl;
       system("pause");
       return 0;
}
Althoguh the structure of function switch1 looks correct during the function call inside of the function values of varaiables first and second will be swiched. After the variable exits the function that switch wont have any effect because the functions handels with copies and not with originals. In the next picture you can see the result.

As you can see the function didn't do the switch of values of variables a and b. So in general the algorithm is useless. But there is a way we can solve this. Let's put pointers instead of variables in arguments of the function. We can do this by putting * before variable first and before variable second respectively. So the code should look like this. 
#include "stdafx.h"
#include "iostream"
using namespace std;

void switch1(int *first, int *second) {
       int third = *first;
       *first = *second;
       *second = third;
}
int _tmain(int argc, _TCHAR* argv[])
{
       int a, b;
       cout << "Enter a value of variable a: " << endl;
       cin >> a;
       cout << "Enter a value of variable b:" << endl;
       cin >> b;
       cout << "The value of variable a before the change is:" << a << endl;
       cout << "The value of variable b before the change is:" << b << endl;
       switch1(&a, &b);
       cout << "The value of variable a after the change is: " << a << endl;
       cout << "The value of variable b after the change is:" << b << endl;
       system("pause");
       return 0;
}
If you have the pointers inside of function arguemnts when the function is called your syntax of function calling should look like this.
       switch1(&a, &b);
The result of the program is shown in next picture. 

As we can see from the previous picture the result is correct because we have successfully switched the values of variables a and b.
The pointers are passed by value which means that formal argument first and second will contain copies of pointers. Although this copies still points on same locations on which the real arguments were pointing. The function is written in a way that process pointers (because inside of brackets you can see the syntax of pointers which is * before the variables a and b which actually means that switch will take place on locations where arguments first and second points. After the function is finished arguments first and second dies but that’s not important because the change has taken place and made a change on variables a and b in the code under the function main() or tmain().
The c++ program offers the more elegant solution and that solution is called pass by reference. Instead of passing by the pointer we can pass by references on outer objects. The procedure is shown in following code.
#include "stdafx.h"
#include "iostream"
using namespace std;

void switch1(int &first, int &second){
       int third = first;
       first = second;
       second = third;
}

int _tmain(int argc, _TCHAR* argv[])
{
       int a, b;
       cout << "Enter a value of variable a: " << endl;
       cin >> a;
       cout << "Enter a value of variable b:" << endl;
       cin >> b;
       cout << "The value of variable a before the change is:" << a << endl;
       cout << "The value of variable b before the change is:" << b << endl;
       switch1(a, b);
       cout << "The value of variable a after the change is: " << a << endl;
       cout << "The value of variable b after the change is:" << b << endl;
       system("pause");
       return 0;
}
The function is called without using any special signs like we used in previous example. The result is shown in the next figure.

As you can see the algorithm is functional because it switched the values of variables a and b.

Important: In function call which have pointers and references as arguments the type of pointers and references which are transferred must match the type of arguments that are placed in declaration of function because for them there aren't any conversions. 
Share on Google Plus