c++

C++ Passing Array to a Function

In C , whenever we pass an array as argument to the function , we always pass name of the array.

Similarly in C++, we can pass arrays as an argument to the function. And, also we can return arrays from a function.

Syntax for passing arrays to a function :-

return_type function_name(data_type array_name[array_size])
{
    // code
}

Example :

int total(int marks[20])
{
    // code
}

Above example shows how to pass an int type array named passed to the function total().

Total size of an array is 20 bytes.


Example : Passing One-dimensional Array to a Function

A Program to display marks of 10 students.

#include <iostream>

using namespace std;

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

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

int main() {
    // declaring and initializing array
    int marks[10] = {88, 76, 90, 61, 69, 76, 89, 46, 87, 76};

    display(marks);

    return 0;
}

Output:

Displaying marks:
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69
Student 6: 76
Student 7: 89
Student 8: 46
Student 9: 87
Student 10: 76
Note:

In C , while passing an array to the doesn’t saves memory. But, in C++,it saves memory and time.


Passing more than 1 Array to a Function

Same as one dimensional array we can pass multidimensional array in C++ .

Let us understand with an example,

Example: Passing Multidimensional Array to a Function

#include <iostream>

using namespace std;

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

int main() {
    //initialization of 2D arrays
    int num[5][4] = { {3, 4},
                      {9, 5},
                      {7, 1},
                      {7, 3},
                      {5, 8},
                    };
    
    // Passing an argument in 2D array
    display(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

In the above program, we have defined a function named display(). The function takes a two dimensional array, int n[][2] as its argument and prints the elements of the array.

While calling the functions, we only pass the name of the two dimensional array as the function argument display(num).

Note: It is not mandatory to specify the number of rows in the array. However, the number of columns should always be specified. That is why in int n[][2] the place of rows is empty but column is specified clearly.

We can also pass arrays with more than 2 dimensions as a function argument.


Returning an Array From a Function

We can also return an array from the function. However, the actual array is not returned. Instead the address of the first element of the array is returned with the help of pointers.

Special Points:-

  1. Whenever we pass an array as an argument to a function it is always “PASS BY REFERENCE” .
  2. Arrays can never become formal argument even if we use the syntax of an array and declare a formal argument , still the compiler will automatically change it to pointer.
  3. This means if an array is passed as actual argument it will be received by pointer either IMPLICTLY EXPLICITLY
  4. Since we have a pointer pointing to the base address of the array , we can not only access the array data but we also can change it.
  5. Since address is passed as argument , we have to receive it in pointer declared.