Parameter passing mechanism in C plus plus

Parameter passing mechanism in C++ :  Unlike most other programming languages, C++ offers a diversity of argument passing mechanisms: pass by value, pass by address, and pass by reference. Additional non-standard argument-passing mechanisms are also supported by certain compilers, such as passing arguments on a register.

Pass by value

Pass by Value is the default argument passing mechanism of both C and C++. When a caller passes by value an argument to a function (known as the callee), the latter gets a copy of the original argument. This copy remains in scope until the function returns and is destroyed immediately afterwards. Consequently, a function that takes value-argument cannot change them, because the changes will apply only to local copies, not the actual caller’s variables.
For example

void negate(int n)  //buggy version
{
n=-n; //affects a local copy of n
}
void func()
{
int m=10;
negate(m);  //doesn’t change the value of m
cout<<m<<endl; // display :10
}

When negate() is called, C++ creates a fresh copy of m on its stack. negate() then modifies this private copy, which is destroyed immediately when it returns. The original variable m remains intact, though. Consequently, the cout expression displays 10 rather than -10. If you want the callee to modify its arguments, you must override the default passing mechanism.

Pass by address

Passing the argument’s address to the callee is called as Pass by Address. A call by address required the formal variable to be considered as pointer and thus the indirection operator has to be used with them. When a caller passes by address an argument to a function (known as the callee), the latter gets the address of the original argument. Hence the change made to the argument in the called function is also visible to the caller. For e.g.,

void negate(int * n)
{
*n=-*n;
}
Similarly, the caller must also be adjusted:
void func()
{
Int m = 10;
Negate (&m);  // pass m’s sddress<
cout<<m<<endl;  // display: – -10 as changes made are visiable
}

Pass by Reference

A call by address requires the formal variable to be considered as pointer and thus the indirection operator has to be used with them. A reference on the other hand, is another name, for a previously defined variable. In other words, after a reference is defined for a particular variable, using its original name as well as the reference can refer to the variable. Hence, a reference is nothing but an alias.
Thus, not passing a copy (call by value) saves time and is efficient. It does not have to create a temporary variable. And, not passing the address (call by address) eliminates the use of pointers in the called functions. But, at the same time, the changes made to a formal variable, using its reference or alias can be reflected back in the calling function.
Passing by reference can be illustrated by the following example:

void negate( int & n)
{
n=-n;    //modifies caller’s argument
}
void func()
{
int m=10;
negate(m); //pass by reference
cout<<m<<endl;  //display: -10 as changes made are visible
}

Leave a Reply

Your email address will not be published.