Argument Passing

Introduction

Each parameter is created anew on each call to the function. The value used to initialize a parameter is the corresponding argument passed in the call.

Parameters vs Arguments

  • A function parameter is a variable declared in the prototype of declaration of a function.
  • An argument is the value that is passed to the function in place of a parameter.

Passing arguments by value

  • By default, arguments in C++ are passed by value.
void foo(int y)
{
    using namespace std;
    cout << "y = " << y << endl;
 
    y = 6;
 
    cout << "y = " << y << endl;
} // y is destroyed here
 
int main()
{
    using namespace std;
    int x = 5;
    cout << "x = " << x << endl;
 
    foo(x);
 
    cout << "x = " << x << endl;
    return 0;
}

Passing arguments by reference

  • In pass by reference we declare the function parameters as references.

Example 1: The value of x is changed by the function.

void foo(int &y) // y is now a reference
{
    using namespace std;
    cout << "y = " << y << endl;
    y = 6;
    cout << "y = " << y << endl;
} // y is destroyed here
 
int main()
{
    int x = 5;
    cout << "x = " << x << endl;
    foo(x);
    cout << "x = " << x << endl;
    return 0;
}

Example 2

void AddOne(int &y)
{
    y++;
}
 
int main()
{
    int x = 5;
 
    cout << "x = " << x << endl;
    AddOne(x);
    cout << "x = " << x << endl;
 
    return 0;
}

Passing arguments by address

  • Involves passing the address of the argument variable rather than the argument variable itself.
  • Because the argument is an address, the function parameter must be a pointer. The function can then dereference the pointer to access or change the value being pointed to.
void foo(int *pValue)
{
    *pValue = 6;
}
 
int main()
{
    int nValue = 5;
 
    cout << "nValue = " << nValue << endl;
    foo(&nValue);
    cout << "nValue = " << nValue << endl;
    return 0;
}
  • Pass by address is typically used with dynamically allocated variables and arrays.
  • It is a good idea to ensure parameters passed by address are not null pointers before dereferencing them.
void PrintArray(int *pnArray, int nLength)
{
    // if user passed in a null pointer for pnArray, bail out early!
    if (!pnArray)
        return;
 
    for (int iii=0; iii < nLength; iii++)
        cout << pnArray[iii] << endl;
}

Nonreference Parameters

Parameters that are plain, nonreference types are initialized by copying the corresponding argument. When a parameter is initialized with a copy, the function has no access to the actual arguments of the call. It cannot change the arguments.

Example 1

     // return the greatest common divisor
     int gcd(int v1, int v2)
     {
         while (v2) 
           {
             int temp = v2;
             v2 = v1 % v2;
             v1 = temp;
           }
         return v1;
     }

Inside the body of the while, we change the values of both v1 and v2. However, these changes are made to the local parameters and are not reflected in the arguments used to call gcd. Thus, when we call

gcd(i, j)

the values i and j are unaffected by the assignments performed inside gcd.

Pointer Parameters

A parameter can be a pointer in which case the argument pointer is copied. As with any nonreference type parameter, changes made to the parameter are made to the local copy. If the function assigns a new pointer value to the parameter, the calling pointer value is unchanged.

Example 2

void reset(int *ip)
     {
         *ip = 0; // changes the value of the object to which ip points
         ip = 0;   // changes only the local value of ip; the argument is unchanged
     }

After a call to reset, the argument is unchanged but the object to which the argument points will be 0:

     int i = 42;
     int *p = &i;
     cout << "i: " << *p << '\n';   // prints i: 42
     reset(p);                      // changes *p but not p
     cout << "i: " << *p << endl;   // ok: prints i: 0

If we want to prevent changes to the value to which the pointer points, then the parameter should be defined as a pointer to const.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.