C++ Passing Array to a Function
In this tutorial, we will learn how to pass a single-dimensional and multidimensional array as a function parameter in C++ with the help of examples.
Passing Array to a Function
- we can pass arrays as an argument to a function just like we pass variables as argument. And, also we can return arrays from a function.
- In order to pass array to the function we just need to mention the array name during function call.
- Before you learn about passing arrays as a function argument, make sure you know about C++ Arrays and C++ Functions.
Syntax
The syntax for passing an array to a function is:
returnType functionName(dataType arrayName[arraySize]) {
// statement
}
Let's see an example,
int total(int num[2]) {
// statement
}
Here, we have passed an int
type array named num to the function total()
. The size of the array is 2.
Declaration Function with array as a Parameter
There are two possible ways to do so, one by using call by value and other by using call by reference.
- We can either have an array as a parameter.
- Or, we can have a pointer in the parameter list, to hold the base address of our array.
int sum(int arr[]);
int sum(int* ptr);
we will study the second way in details later when we will study pointers.
Passing array as parameter to Function
Now let's see a few examples where we will pass a single array element as argument to a function, a one dimensional array to a function and a multidimensional array to a funcion.
Example 1: Passing single Array to a Function
Let's write a very simple program, where we will declare and define an array of intergers in our main() function and pass one of the array element to a function, which will just print th evalue of the element.
// C++ Program to display single array element
#include <iostream>
using namespace std;
void arr(int a ) {
// display array elements
cout << "Numbers " << a <<endl;
}
int main() {
// declare and initialize an array
int myArr[] = {1, 2, 3};
arr(myArr[1]); // pass array element myArr[1] only.
return 0;
}
Output
Numbers : 2
Example 2: Passing 1D Array to a Function
// C++ Program to display numbers of 4 students
#include <iostream>
using namespace std;
// declare function to display Roll Number & take a 1d array as parameter
void display(int a[4]) {
cout << "Display Roll Numbers: " << endl;
// display array elements
for (int i = 0; i < 4; ++i) {
cout << "Student " << i + 1 << ": " << a[i] << endl;
}
}
int main() {
// declare and initialize an array
int rollNum[4] = {4, 1, 3, 2};
display(rollNum); // call display function pass array as argument
return 0;
}
Output
Display Roll Numbers: Student 1: 4 Student 2: 1 Student 3: 3 Student 4: 2
Working of above program
- When we call a function by passing an array as the argument, only the name of the array is used.
Here, the argument rollNum represent the memory address of the first element of array rollNum[4].display(rollNum);
- However, notice the parameter of the
display()
function.
Here, we use the full declaration of the array in the function parameter, including the square bracesvoid display(int a[4])
[]
. - The function parameter
int a[4]
converts toint* a;
. This points to the same address pointed by the array rollNum. This means that when we manipulate a[4] in the function body, we are actually manipulating the original array rollNum.
C++ handles passing an array to a function in this way to save memory and time.
Passing Multidimensional Array to a Function
We can also pass Multidimensional arrays as an argument to the function. For example,
Example 3: Program to Store value entered by user in 3D array
// C++ Program to display the elements of 2D 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 a[][2]) {
cout << "The Elements are: " << endl;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
cout << "arr[" << i << "][" << j << "]: " << a[i][j] << endl;
}
}
}
int main() {
// initialize 2d array
int arr[2][2] = { {1, 2}, {3, 4} };
// call the function & pass a 2d array as an argument
display(arr);
return 0;
}
Output
The Elements are: arr[0][0]: 1 arr[0][1]: 2 arr[1][0]: 3 arr[1][1]: 4
Working of above program
- In the above program, we have defined a function named
display()
. The function takes a two dimensional array,int a[][2]
as its argument and prints the elements of the array. - While calling the function, we only pass the name of the two dimensional array as the function argument
display(arr)
. - Note: It is not mandatory to specify the number of rows in the array. However,
the number of columns should always be specified. This is why we have used
int a[][2]
.
We can also pass arrays with more than 2 dimensions as a function argument.
C++ Returning an Array From a Function
- We don't return an array from functions, rather we return a pointer holding the base address of the array to be returned.
- But we must, make sure that the array exists after the function ends i.e. the array is not local to the 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.
We will learn about returning arrays from a function in the later tutorials.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Array & Function in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ String
.