Next: Strings in C++
Up: Arrays
Previous: Example Program: Test of
In passing an array as a parameter to a function it is passed as a reference parameter. What is actually passed is the address of its first element. Since arrays are passed by reference this means that if the function changes the value of an element in an array that is a parameter of the function then the corresponding actual array of the call will have that element changed.
Though an array is passed as a reference parameter an &
is not used to
denote a reference parameter. However it must be indicated to the
compiler that this parameter is an array by appending []
to the formal
parameter name. Thus to declare an array of real values as a parameter
requires the parameter to be specified as follows:
..., float A[],...
This is the same as a normal array declaration but the size of the
array is not specified. This is illustrated in the following example
which returns the average value of the first n
elements in a real
array.
float meanarray(int n, // IN no of elements float A[]) // IN array parameter // This function returns the average value of // the first n elements in the array A which // is assumed to have >= n elements. { float sum = 0.0; // local variable to // accumulate sum int i; // local loop control for (i = 0; i < n; i++) sum += A[i]; return sum/n; } // end meanarray
Download program.
If when this function was called the value given for the parameter
n
was greater than the number of elements in the actual array
replacing the parameter A
then an incorrect result would be
returned.
The function meanarray
could be used as follows:
const int NE = 100; float average, data[NE]; int i, m; cout << "Enter no of data items (no more than " << NE << "): "; cin >> m; for (i = 0; i < m; i++) cin >> data[i]; average = meanarray(m, data);
An array can also be an output parameter, consider the following
example in which the function addarray
adds two arrays together
to produce a third array whose elements are the sum of the
corresponding elements in the original two arrays.
void addarray(int size, // IN size of arrays const float A[], // IN input array const float B[], // IN input array float C[]) // OUT result array // Takes two arrays of the same size as input // parameters and outputs an array whose elements // are the sum of the corresponding elements in // the two input arrays. { int i; // local control variable for (i = 0; i < size; i++) C[i] = A[i] + B[i]; } // End of addarray
Download program.The function
addarray
could be used as follows:float one[50], two[50], three[50]; . . addarray(20, one, two, three);
Note that the parameter size could have been replaced with any value
up to the size that was declared for the arrays that were used as
actual parameters. In the example above the value of 20 was used
which means that only the first 20 elements of the array three
are set.
Also note that the input parameters A
and B
have been
declared in the function head as being of type const float
.
Since they are input parameters they should not be changed by the function
and declaring them as constant arrays prevents the function from
changing them.