< previous page page_989 next page >

Page 989
    y = temp;
}
By default, C++ passes the two parameters by value. That is, copies of alpha's and beta's values are sent to the function. The local contents of x and y are exchanged within the function, but the actual parameters alpha and beta remain unchanged. To correct this situation, we have two options. The first is to send the addresses of alpha and beta explicitly by using the address-of operator (&):
Swap(&alpha, &beta) ;
The function must then declare the formal parameters to be pointer variables:
void Swap( float* px, float* py )
{
    float temp = *px;

    *px = *py;
    *py = temp;
}
This approach is necessary in the C language, which has pointer variables but not reference variables.
The other option is to use reference variables to eliminate the need for explicit dereferencing:
void Swap( float& x, float& y )
{
    float temp = x;

    x = y;
    y = temp;
}
In this case, the function call does not require the address-of operator (&) for the actual parameters:
Swap(alpha, beta);
The compiler implicitly generates code to pass the addresses, not the contents, of alpha and beta. This method of passing nonarray parameters by ref-

 
< previous page page_989 next page >