Example of C++ code to Pass an Array to a Function


If you are not familiar with functions, you may find it helpful to review the web page entitled Functions and Parameter Passing. If you are not familiar with arrays, you may find it helpful to review the web page entitled Array Declaration and Manipulation.

In C++, arrays are passed by reference, meaning that the child function does not receive a copy of the entire array with all of its elements, but rather receives a means to access the elements within the original array in the parent function. This is accomplished internally using a pointer to the first element of the array. A second parameter indicating the size of the array is also typically passed, so that the function will know how many elements the array contains.

The C++ source code below demonstrates a variety of ways in which the actual and formal parameter lists can be specified for the passage of arrays. The two child functions both accomplish the same result, which is to display the five elements in the passed array. The only difference between the two functions is the manner in which they represent the received pointer in their formal parameter lists.

The first function, "displayArrayUsingArrayNotation", uses a notation for the received array pointer that is similar to a typical array declaration notation and allows the use of subscripts within the function to access its elements. A function containing a formal parameter list using this notation is typically called using an actual parameter list that refers to the address of the array's first element by typing only the identifier of the array (without brackets or subscript). An example of this can be seen in the first calling statement to function displayArrayUsingArrayNotation in the main function below.

The second function "displayArrayUsingPointerNotation" uses a notation for the received array pointer that a typical pointer declaration, expecting the use of indirection within the function to access the array's elements. A function containing a formal parameter list using pointer notation is typically called using an actual parameter list that refers to the address of the array's first element by using the & (address of) operator. An example of this can be seen in the first calling statement to function displayArrayUsingPointerNotation in the main function below.

Note that either notation can be used in the actual parameter list of the calling statement, regardless of the notation used in the formal parameter list. This is illustrated by the final two calls in the main function below to the two child functions, in which the opposite notation is used in the actual parameter list from the one used in the child function's formal parameter list.


/* Examples of different notations for passing arrays.
   This program declares and initializes a small integer array
   and then passes it into two functions which both display the array
   but each function uses a different notation for the parameters.
*/

#include <iostream>
using namespace std;

#define  SIZE  5   // Size of the array

void displayArrayUsingArrayNotation (int A[], int SZ)
// Function receives array A by reference and its size SZ by value. The counter C
// is used to count passes and as a subscript to point at elements.
{
 int C; // Loop counter
 for (C=0; C<SZ; C++)
     cout << A[C] << endl;
}

void displayArrayUsingPointerNotation (int *Ap, int SZ)
// Function receives pointer Ap to an array and its size SZ, both by value,
// and uses indirection to access the array elements in main. The counter C
// is used to count passes, but not as a subscript to point at elements.
{
 int C; // Loop counter
 for (C=0; C<SZ; C++)
 {
     cout << *Ap << endl;
     Ap++;  // Increment the pointer to point at the next element
 }
}

int main ()
{

 /* Array Declaration - An array of known integers */
 int N[]= {1,2,3,4,5};

 // Call a function defined array using array notation
 // which passes the array by reference using just its identifier
 // and the SIZE by value
 displayArrayUsingdisplayArrayUsingArrayNotation (N, SIZE);

 cout << endl; // Skip a line between the output steps
 
 // Call a function defined using pointer notation by passing
 // the address of the first element of the array by value
 // and the SIZE by value.
 // Note that the array is still consider to be passed by reference.
 displayArrayUsingPointerNotation (&N[0], SIZE);

 cout << endl; // Skip a line between the output steps

 // Call a function defined array using array notation by passing
 // the address of the first element of the array by value
 // and the SIZE by value.
 // Note that the array is still consider to be passed by reference.
 displayArrayUsingdisplayArrayUsingArrayNotation (&N[0], SIZE);

 cout << endl; // Skip a line between the output steps
 
 // Call a function defined using pointer notation 
 // which passes the array by reference using just its identifier
 // and the SIZE by value
 displayArrayUsingPointerNotation (N, SIZE);

 return 0;
}
PATH: Instructional Server> COP 2000>