C++ Passing Array to Function

In this tutorial, we will learn about how to pass an array in C++ with the help of examples.


In C++ when you pass an array, it is always passed with the help of “Call by Reference” and never “Call by Value”. This means that any changes made to the array in the function will reflect back in the original array. There are mainly 3 ways to pass an array to a function which are as follows :

Method #1:

void myFunction(int *param) { … }

Method #2:

void myFunction(int param[]) { … }

Method #3:

void myFunction(int param[5]) { … }

Example

The following code will show how an array is passed and how the values get changed.

#include <iostream>
using namespace std;
void change(int m[5]) {
    m[0]=100;
}
int main() {
    // Declare and initialize an array
    int marks[5] = {88, 76, 90, 61, 69};
    // call change function to change a value
    change(marks);

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

Output

Student 1: 100
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69

Here you can see that the marks of the student were set to 88 initially but the function changed it to 100 and it got reflected back to the main function variable which proves that arrays are always by default passed by reference.

You can also send the size of an array so that you can print the array in the function. One point is that you do not need to return the array as it is always Call by reference.

How to make changes but not let them reflect?

If you want to pass an array and do any operation on it without changing the original array. You can create a local copy of the array in the function and perform your operation on it. The following code will show how you can pass an array and create a copy of it.

Example

#include <iostream>
using namespace std;
void change(int m[5]) {
    int copyarr[5];
    for(int i=0;i<5;i++){
        copyarr[i]=m[i];
    }
    copyarr[0]=100;  //Making changes to the copy array will not
                    // affect the original array.
}

int main() {
    // Declare and initialize an array
    int marks[5] = {88, 76, 90, 61, 69};
    // call change function to change a value
    change(marks);

    // display array elements 

    for (int i = 0; i < 5; ++i) {
        cout << "Student " << i + 1 << ": " << marks[i] << endl;
    }
    return 0;

}

Output

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

As we can see that the value does not affect the original array. If you want to pass more than a 1D array, you need to specify the successive dimensions of the array of the formal parameter and it cannot be left blank. For example :

int arrayfunc( int arr[][] ) {…} // This will throw an error
int arrayfunc( int arr[][5] ) {…} // This will not throw an error

You can also pass the length of the array to the function which will save you the time needed to find the length of the array.

Please get connected & share!

Categories

Advertisement