TRANSFERRING ARGUMENTS BY VALUE

The usual way of transferring the arguments by value is transferring data values, as we've done before in example where we've calculated the value of factorial or square. However, it's very important to notice that during this function call the formal arguments and value which is transferred are not mutually connected. Formal argument is alive just inside the function and the value of this argument is lost after the argument leaves the function. If we change the value of the argument the change will not affect the object which we've entered during the function call. Let's take a closer look on particular example:

#include "stdafx.h"
#include "iostream"
using namespace std;

//FUNCTION DEFINITION AND DECLARATION

int addhundared(float i){
       i += 100;
       return i;
}

int _tmain(int argc, _TCHAR* argv[])
{
       int n = 1;
       addhundared(n);
       cout << "Starting value:" << n << endl;
       n = addhundared(n);
       cout << "Ending value:" << n << endl;
       system("pause");
       return 0;
}

This simple function addhundared() in its definition increase the value of the argument it handles only with local variable which is initialized on value of real argument during the function call. Simply speaking a function made a copy of an argument and always works with it. As the argument exits the function the copy is destroyed because it is defined only inside the function body. For that reason the variable n will still have its original value as before the function call. This pass of value to a function is usually called pass by value.
The increased value is returned as a result of the function after the second function call i.e. joining the return value to a variable n, variable n will be increased.
IMPORTANT: When arguments of a function are passed by value then their value in code doesn't change.

If real arguments and formal arguments in declaration of different types then during initialization of formal argument on real argument are applied the usual rules of conversion. As a real argument of function we can use some expression. In that cases the expression is calculated before the function call. 
Share on Google Plus