How to Pass an Array to a Function in C++

 Pass an Array to a Function

This tutorial will demonstrate using examples how to pass a single-dimensional and multidimensional array to a function parameter in C++. Pass an Array to a Function

In C++, functions accept arguments in the form of arrays. Additionally, functions can return arrays.

Make sure you are familiar with C++ Arrays and C++ Functions before learning about passing arrays as function arguments.


Passing Arrays as Function Parameters: Syntax

The following syntax is used to pass an array to a function:

returnType functionName(dataType arrayName[arraySize]) {
    // code
}

Let’s see an Example

int code(int geektocode[10]) {
    // code
}

Here, we have passed an int type array named geektocode to the function code(). The size of the array is 10.


Example 1: Providing a Function with a One-Dimensional Array

// C++ Program to display marks of 5 students

#include <iostream>
using namespace std;

// declare function to display marks
// take a 1d array as parameter
void show(int m[5]) {
    cout << "Displaying marks: " << endl;

    // display array elements    
    for (int i = 0; i < 5; ++i) {
        cout << "Student " << i + 1 << ": " << m[i] << endl;
    }
}

int main() {

    // declare and initialize an array
    int marks[5] = {88, 76, 90, 61, 69};
    
    // call show function
    // pass array as argument
    show(marks);

    return 0;
}

Output:

Displaying marks: 
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69

In order to conserve memory and time, C++ handles passing an array to a function in this manner.

Multidimensional Array Passing to a Function

Multidimensional arrays are another type of argument we can pass to the function. For instance,

// C++ Program to display the elements of two
// dimensional array by passing it to a function
//geektocode.com
#include <iostream>
using namespace std;

// define a function 
// pass a 2d array as a parameter
void show(int n[][2]) {
    cout << "Displaying Values: " << endl;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 2; ++j) {
            cout << "num[" << i << "][" << j << "]: " << n[i][j] << endl;
        }
    }
}

int main() {

    // initialize 2d array
    int num[3][2] = {
        {3, 4},
        {9, 5},
        {7, 1}
    };

    // call the function
    // pass a 2d array as an argument
    show(num);

    return 0;
}

Output:

Displaying Values: 
num[0][0]: 3
num[0][1]: 4
num[1][0]: 9
num[1][1]: 5
num[2][0]: 7
num[2][1]: 1

Reminder: Specifying the array’s row count is not required. The number of columns should, even so, always be stated. For this reason, we used int n[][2].

As a function argument, we can also pass arrays with more than two dimensions.

C++ Using a Function to Return an Array

The function also has the option of returning an array. The actual array, however, is not given back. Instead, pointers are used to return the address of the array’s first element.

In upcoming tutorials, we will learn about functions that return arrays.

Leave a Reply

Your email address will not be published. Required fields are marked *