C++ Passing Array to a Function

In C++ programming, an array can be passed to a function as an argument. We can pass an array of any dimension to a function as argument.

  • Passing an array to a function uses pass by reference, which means any change in array data inside function's body will reflect globally.
  • When an array is passed as an argument to a function, only the name of an array is passed. Name of the array represent the address of the first element of array.
    For Example :
       int arrayName[10];
       getSum(arrayName) is equivalent to getSum(&arrayName[0]);
    

Passing Single Element of an Array to Function

We can pass one element of an array to a function like passing any other basic data type variable.

C program to pass single element of an array to function
#include <iostream>
using namespace std;
 
int getSquare(int num){
    return num*num;
}
 
int main(){
   int array[5]={1, 2, 3, 4, 5};
   int counter;
   for(counter = 0; counter < 5; counter++){
       // Passing individual array elements to function
       cout << "Square of " << array[counter] << " is " << getSquare(array[counter]);
       cout << endl;
   }

   return 0;
} 

Output
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25

Passing One-dimensional Array to a Function

We can pass a one dimensional array to a function by passing the base address(address of first element of an array) of the array. We can either pass the name of the array (which is equivalent to base address) or pass the address of first element of array like &array[0]. Similarly, we can pass multi dimensional array also as formal parameters.

Different ways of declaring function which takes an array as parameter
  • Function argument as a pointer
    int testFunction(int *array){
    /* Function body */
    } 
    
  • By specifying size of an array in function parameters
    int testFunction(int array[10]){
    /* Function body */
    }
    
    the formal argument int array[10] in function declaration converts to int* array. This pointer points to the base address of array.
  • By passing unbounded array in function parameters.
    int testFunction(int array[]){
    /* Function body */
    }
    

C++ Program to Pass One Dimensional Array as Parameter to a Function

#include <iostream>
using namespace std;
 
// This function takes integer pointer as argument
void printArrayOne(int *array, int size){
    int i;
    for(i=0; i<size; i++){
        cout << array[i] << " ";
    }
    cout << endl;
}
 
// This function takes sized array as argument
void printArrayTwo(int array[5], int size){
    int i;
    for(i=0; i<size; i++){
        cout << array[i] << " ";
    }
    cout << endl;
}
 
// This function takes unbounded array as argument
void printArrayThree(int array[], int size){
    int i;
    for(i=0; i<size; i++){
        cout << array[i] << " ";
    }
    cout << endl;
}
 
int main(){
   int score[5]={1, 2, 3, 4, 5};
   // Passing name of array 
   printArrayOne(score, 5);
   printArrayTwo(score, 5);
   // passing address of first array element 
   printArrayThree(&score[0], 5);
    
   return 0;
} 

Output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Passing Multidimensional Array to a Function

Multidimensional array with dimension 2 or more can be passed in similar way as one dimensional array.

#include <iostream>
using namespace std;
 
// This function takes sized array as argument
void printArray(int array[3][3], int rows, int cols){
    int i, j;
    for(i=0; i<rows; i++){
        for(j=0; j<cols; j++){
            cout << array[i][j] << " ";
        }
        cout << endl;
    }
}
 
int main(){
   int matrix[3][3]={{1, 2, 3},{4, 5, 6},{7, 8, 9}};
   // Passing name of array 
   printArray(matrix, 3, 3);
    
   return 0;
} 

Output
1 2 3
4 5 6
7 8 9