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
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:
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
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:-
-
Whenever we pass an array as an argument to a function it is always “PASS BY
REFERENCE” .
-
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.
-
This means if an array is passed as actual argument it will be received by pointer
either IMPLICTLY EXPLICITLY
-
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.
-
Since address is passed as argument , we have to receive it in pointer declared.