In C++ Passing an Array to a Function, an entire array cannot be provided as an argument to a function. You can, however, supply the array’s name to pass a pointer to an array without an index.
- When we send an array to a function in C, such as fun(), it is always handled as a pointer ().
- With the help of examples, we will learn how to send a single-dimensional and multidimensional array as a function parameter in C++.
- Arrays can be passed as arguments to functions in C++. We can also return arrays from functions.
- A whole array cannot be passed as an argument to a function in C++. You can, however, send a pointer to an array without giving an index by specifying the array’s name.
- If you want to give a single-dimension array as an argument to a function, you must declare the function formal parameter in one of three ways,
- All three declaration procedures produce comparable results because they all tell the compiler that an integer reference will be received.
Make sure you understand C++ Arrays and C++ Functions before learning about providing arrays as function arguments.
Contents
The syntax for Passing Arrays as Function Parameters
The following is the syntax for sending an array to a function:
returnType functionName(dataType arrayName[arraySize]) { // code }
Let’s look at an example.
int total(int marks[5]) { // code }
In this case, we gave an int-type array called marks to the method total (). The array has a size of 5.
Example 1: Passing One-dimensional Array to a Function
// 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 display(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 display function // pass array as argument display(marks); return 0; }
Output
Displaying marks:
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69
Here,
When we call a function with an array as a parameter, we just utilize the array’s name.
display(marks);
The memory location of the first element of array marks is represented by the argument markings[5].
Take note of the display() function’s parameter.
void display(int m[5])
In this case, we use the whole array declaration in the function parameter, including the square braces [].
Int m[5] is converted to int* m; by the function parameter. This corresponds to the address indicated by the array markings. This means that when we change m[5] in the function body, we are actually changing the array marks.
To conserve memory and time, C++ handles supplying an array to a function in this manner.
A Multidimensional Array Is Passed to a Function
Multidimensional arrays can also be passed as an argument to the function. As an example,
Example 2: Passing Multidimensional Array to a Function
// C++ Program to display the elements of two // dimensional array by passing it to a function #include <iostream> using namespace std; // define a function // pass a 2d array as a parameter void display(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 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 preceding program, we defined a function called display (). The function accepts a two-dimensional array, int n[][2,] as an input and outputs the array’s elements.
We merely supply the name of the two-dimensional array as the function argument display when invoking the function (num).
Note: It is not required to indicate the number of rows in the array. The number of columns, however, should always be stated. That’s why we used int n[][2].
As a function argument, we can also pass arrays with more than two dimensions.
C++ Returning an Array From a Function
The function can also return an array. The actual array, however, is not returned. Instead, pointers are used to return the address of the array’s first element.
In the next lessons, we will learn about returning arrays from a function.
You may like:
C++ Programming Default Arguments (Parameters) with Example