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
display(marks);
In above case, the argument marks represent the memory address of the first element
of array marks[10]
.
display()
function.
void display(int x[10])
In the above case, we have to use the full declaration of the array in the function parameter, including the square braces [].
int x[10]
converts to int* x;
.
This indicates to the same address shown by the array marks. This means that when
we run x[10]
in the function body, we are actually manipulating the original array
marks.
In C , while passing an array to the doesn’t saves memory. But, in C++,it saves memory and time.
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.
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:-