Arrays as Function Arguments

The only way to pass an array to a function is to pass a pointer to it. In case of mono-dimensional arrays there are three syntactic forms available to pass the pointer to the array. Either you can specify the array length:
int multiply(int intArray[5])
{/* body */}
or you can leave the array length unspecified:
int highest(int intArray[])
{/* body */}
or, finally, you can pass an actual pointer:
int add(int *intArray)
{/* body */}
The final result is the same in all three cases: the function receives a pointer to the array and no bound-checking is performed on the array actually passed. So one could pass a fifteen element array to a function expecting a five-element one, and no exception would be raised.

Example


#include <iostream>
using namespace std;

int multiply(int intArray[5])
{
    int returnValue = 1;
    for(int i = 0; i < 5; i++)
           returnValue = returnValue * intArray[i];

     return returnValue;
}

int highest(int intArray[])
{
    int highest = intArray[0];
    for(int i = 1; i < 5; i++)
           if(highest < intArray[i])
                highest = intArray[i];

    return highest;
}

int add(int *intArray)
{
    int returnValue = 0;
    for(int i = 0; i < 5; i++)
           returnValue = returnValue + intArray[i];

    return returnValue;
}

int main()
{   
     int arrayOfInt[5];

     for(int i = 0; i < 5; i++)
           arrayOfInt[i] = i;


     for(int i = 0; i < 5; i++)
           cout << "The element " << i << "\'s content is " << arrayOfInt[i] << endl;

     cout << endl;

     cout << "multiply(arrayOfInt):  " << multiply(arrayOfInt) << endl;

     cout << "highest(arrayOfInt):  " << highest(arrayOfInt) << endl;

     cout << "add(arrayOfInt):  " << add(arrayOfInt) << endl;
}

Output

The element 0's content is 0
The element 1's content is 1
The element 2's content is 2
The element 3's content is 3
The element 4's content is 4

multiply(arrayOfInt):  0
highest(arrayOfInt):  4
add(arrayOfInt):  10