Check The Programming Section
Here we will learn, how to pass a 1-d array as argument to a function. In general, we have used arrays to build a communication between multiple functions. In this case, a function can be a calling or called function. Various ways we can build this relationship, consdier the image displayed below:
Various ways passing one-dimenstional arrays to function
As the figure suggest that, we can pass an entire array to the function or one of the three other ways can be used to pass an element of the array to a function.
Passing individual items of the array to a function, means that passing values of each items to the function. To do that, the index of individual items with subscript operator will be used. The required syntax is
function-name(array[index]);
An example to display the elements of the array using a function (edit and try):
#include<iostream>
using namespace std;
void display(int num){
cout<<num<<" ";
}
int main(){
int arr[]={3,4,5,67,8,9,10,23,45};
int size = sizeof arr/sizeof arr[0];
for(int i = 0; i<size; i++){
display(arr[i]);
}
}
In the above code, the individual items of arr array is passed to display() function. The statement display(arr[i]); inside the for loop pass the elements by value and for that index of each items are used. Note that since at a time only one element is being passed, this element is collected in an ordinary integer variable num in the function display().
Like values, we can also pass the address of individual item to the function display(), to print one item at a time. The required syntax to pass the address of individual array item to a function is
function-name(&array[index]);
The required code to display the values of array items, by passing address of each item is (edit and try)
#include<iostream>
using namespace std;
void display(int *num){
cout<<*num<<" ";
}
int main(){
int arr[]={3,4,5,67,8,9,10,23,45};
int size = sizeof arr/sizeof arr[0];
for(int i = 0; i<size; i++){
display(&arr[i]);
}
}
Hence, the variable num in which the address is collected in function display(), declared as pointer int *num. The address of operator & is preceded before each item of the array, while calling the function display(&arr[i]);
As C++ gives us mechanisms to declare a variable as l-value reference (alias to a variable), which refers to the same memory block of a variable, we can use this to pass each data items of the array as an alias to the function formal argument. The requied syntax for function prototype of the display() function is
void display(int &);
The required code to display the values of array items, by passing values as reference of each item is (edit and try)
#include<iostream>
using namespace std;
void display(int &num){
cout<<num<<" ";
}
int main(){
int arr[]={3,4,5,67,8,9,10,23,45};
int size = sizeof arr/sizeof arr[0];
for(int i = 0; i<size; i++){
display(arr[i]);
}
}
The temporary variable num in the display function declared as l-value referenec as int &num to each data item of the array. Note that since at a time only one element is being passed, this element is collected in a l-value reference variable num in the function display().
As we have discussed, array name represents the base address of the array, we can use array name to pass the entire array to the function. The address of rest of the elements in the array can be calculated using the array name and the index value of the array element. The syntax to pass the entire array, while calling the function is
function-name(name-of-the-array);
The required syntax for the function prototype is
return-type function-name(array-type []);
Few points we need to remember while passing an entire array to a function as argument. These are as follows:
An array name without index is required only, to pass the entire array as a function argument. Here display() is the function and arr is the name of the array. So the syntax to call the display() function is
display(arr);
Along with array name, we also needs to pass the size of the array from the calling function as argument to the called function. The complete syntax is
display(arr, size);
size must be calculated insdie the calling function using the statement int size = sizeof arr/sizeof arr[0];
The second point is important, because the statement int size = sizeof arr/sizeof arr[0]; (where arr is an array as argument of the function) does not return the complete size of the array, if we calculates the size of the array inside the called function. Instead, it simply divides the size of a pointer (which is 8 in 64-bit machine architecture) by the size of an integer (4) and returns 2. This problem is know as array decay problem. So, the complete function prototype for display() function is
void display(int arr[], int size);
The required code to display all the items of an array by passing the entire array is (edit and try)
#include<iostream>
using namespace std;
void display(int num[], int size){
for(int i=0;i<size;i++){
cout<<num[i]<<" ";
}
}
int main(){
int arr[]={3,4,5,67,8,9,10,23,45};
int size = sizeof arr/sizeof arr[0];
display(arr, size);
}
The most important change to the above code is, the for loop is now insdie the display() function. Earlier, it was inside the main() function, while we pass one item at a time and display() gets called equal to the size of the array times insdie the loop. Now we have passed an entire array, so we need a loop to access the item of the array inside display().