C++ Pass Array to a Function

Welcome to The Coding College! In this tutorial, we will explore how to pass arrays to functions in C++, enabling efficient data processing and manipulation of collections of data.

How to Pass Arrays to Functions

In C++, when you pass an array to a function:

  1. The array is passed by reference (its memory address is passed).
  2. The function operates on the original array.
  3. You must specify the type of the array elements, but you don’t need to specify the array’s size in the parameter.

Syntax

return_type function_name(data_type array_name[], int size) {
    // Function body
}
  • data_type: Type of the elements in the array (e.g., int, float).
  • array_name[]: Array passed to the function.
  • size: Typically, the size of the array is also passed to avoid out-of-bound errors.

Example: Passing an Array to a Function

Example: Print Array Elements

#include <iostream>
using namespace std;

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    int numbers[] = {10, 20, 30, 40, 50};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    printArray(numbers, size); // Passing the array and its size

    return 0;
}

Output:

10 20 30 40 50  

Why Pass the Size of the Array?

Arrays in C++ don’t carry their size information when passed to a function. Therefore, it is a common practice to pass the size explicitly.

Modify an Array in a Function

Since arrays are passed by reference, modifications to the array inside the function affect the original array.

Example: Double Each Element

#include <iostream>
using namespace std;

void doubleElements(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        arr[i] *= 2; // Modify the array elements
    }
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    doubleElements(numbers, size);

    cout << "Doubled array: ";
    for (int i = 0; i < size; i++) {
        cout << numbers[i] << " ";
    }
    cout << endl;

    return 0;
}

Output:

Doubled array: 2 4 6 8 10  

Multi-Dimensional Arrays

To pass a multi-dimensional array, you need to specify all dimensions except the first in the function parameter.

Example: Print a 2D Array

#include <iostream>
using namespace std;

void print2DArray(int arr[][3], int rows) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 3; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    print2DArray(matrix, 2);

    return 0;
}

Output:

1 2 3  
4 5 6  

Using std::array or std::vector

Passing arrays using standard library containers like std::array or std::vector is often more manageable and safer than raw arrays.

Example with std::array

#include <iostream>
#include <array>
using namespace std;

void printArray(array<int, 5> arr) {
    for (int val : arr) {
        cout << val << " ";
    }
    cout << endl;
}

int main() {
    array<int, 5> numbers = {10, 20, 30, 40, 50};
    printArray(numbers);

    return 0;
}

Output:

10 20 30 40 50  

Example with std::vector

#include <iostream>
#include <vector>
using namespace std;

void printVector(vector<int> vec) {
    for (int val : vec) {
        cout << val << " ";
    }
    cout << endl;
}

int main() {
    vector<int> numbers = {5, 10, 15, 20, 25};
    printVector(numbers);

    return 0;
}

Output:

5 10 15 20 25  

Best Practices

  • Pass Size Explicitly: Always pass the size of the array to prevent out-of-bound errors.
  • Use const for Read-Only Arrays: If the function doesn’t modify the array, declare it as const.
void printArray(const int arr[], int size);
  • Prefer STL Containers: Use std::array or std::vector for better type safety and built-in size management.

Explore More on The Coding College

Arrays are a fundamental data structure in C++, and passing them to functions unlocks their full potential. To dive deeper into topics like dynamic arrays, pointers, and STL algorithms, visit The Coding College.

Leave a Comment