< previous page page_609 next page >

Page 609
In the formal parameter list, the declaration of arr does not include a size within the brackets. If you include a size, the compiler ignores it. The compiler only wants to know that it is a float array, not a float array of any particular size. Therefore, you must include a second parameter-the number of array elements-in order for this function to work correctly.
The calling code can invoke the ZeroOut function for a float array of any size. The following code fragment makes function calls to zero out two arrays of different sizes. Notice how an array parameter is declared in a function prototype.
void ZeroOut( float[], int );    // Function prototype
  .
  .
  .

int main()
{
    float velocity[30];
    float refractionAngle[9000];
      .
      .
      .
    ZeroOut(velocity, 30);
    ZeroOut(refractionAngle, 9000);
      .
      .
      .
}
With simple variables, pass-by-value prevents a function from modifying the caller's actual parameter. You cannot pass arrays by value in C++, but you can still prevent the function from modifying the caller's array. To do so, you use the reserved word const in the declaration of the formal parameter. Below is a function that copies one int array into another. The first parameter-the destination array-is expected to be modified, but the second array is not.
void Copy( /* out */         int destination[],
           /* in */    const int source[],
           /* in */          int size          )
{
    int i;

    for (i = 0; i < size; i++)

            // Invariant (prior to test):
            //     For all k, where 0 <= k <= i-l,
            //        destination[k] == source[k]
            //  && 0 <= i <= size

 
< previous page page_609 next page >