Passing 1-D Array to Functions

Before we get into passing arrays, let's review the format for using functions.

#include <iostream>
using namespace std;

// function prototypes go here
// e.g. int getMax( int [], int );

int main()
{
    //  variables declared here

    //  function calls interspersed with other code
    //  e.g. max = getMax(array, 10);

return 0;
} // end main

// Function header
// function_return_type   function_name(parameter_list)
// e.g. int getMax(int ary[], int len)
{
   // code for function
   // e.g. max = ary[i];

} // end function

Now, let's take a closer look at how those arrays are passed. In C++, arrays are not passed by value to functions, they are passed by reference. Because of this, you do not have to use the & reference character. You simply pass the base address of an array to a function. To do this, just supply the name of the array.

For example, suppose you had made the following declarations:


const int size=10;
int ary[size];

Further suppose you wanted to pass this array to a function called getMax which expected the array reference as a parameter. The call to that function would be:

getMax(ary, size);

Notice that only the array name ary appears in the parameter list; it is not followed by any subscripts at all.

Example:

This program adds two integer arrays and displays the arrays.
// Program Arraypractice.cpp will add two arrays and store the sum
// in the third array.  Print them all out to the screen.

#include <iostream>
using namespace std;

void add_arrays(int [], int [], int [], int);

int main ()
{
     const int MAX_ARRAY = 5;

     int  a[MAX_ARRAY];
     int  b[MAX_ARRAY];
     int  c[MAX_ARRAY];
     int  index;

        // Ask users to enter values for array a[].
        for (index = 0; index < MAX_ARRAY; index++)
        {

                cout << "Please input 5 values for a array: ";
                cin >>a[index];
        }

       // Ask users to enters value for array b[].
       for (index = 0; index < MAX_ARRAY; index++)
        {
                cout << "Please input 5 values for b arrays: ";
                cin >>b[index];
        }

       // Store the sum of array a[] and array b[] to array c[].
       // for (index = 0; index < MAX_ARRAY; index++)
       // {

       //    c[index] = a[index]+ b[index];
       // }

	// call for the function add_arrays() to add two arrays
	add_arrays( a, b, c,  MAX_ARRAY);

	//To separate the output from other stuff
        cout << endl;

       // Add code to print out each of the arrays
       for (index = 0; index < MAX_ARRAY; index++)
       {
        cout << "a[" << index << "] = "  << a[index] << endl;
        cout << "b[" << index << "] = "  << b[index] << endl;
        cout << "c[" << index << "] = "  << c[index] << endl;
        cout << endl;
       }

   return 0;

}

// a function adds two arrays
void add_arrays(int x[], int y[], int z[], int len)
{
	for ( int i = 0; i < len; i++)
	{
	   z[i] = x[i] + y[i];
	}

	return;
}


Copyright: Department of Computer Science, University of Regina.