Pass entire array to a function in C++ is possible? here we will discuss this with following,
we can’t pass an entire to function although you can pass the address of array’s first element using by array-name(without subscript operator) in the actual parameter.
don’t confuse with Passing Array Elements to a Function and Passing Array to a Function, Both are quite different.
In the passing array elements to function, the value will be passed and function will be called multiple time until array element ends while passing an array to function only first element’ address will be passed and inside the function, other elements will be accessed using loop means function has a single call.
let’s explore it,
and we already mentioned in Array in C++ that array name always refers the base address of its first element, so you can say we only pass the base address of its first element rather than all elements address to a function where inside function body we use the loop to access all other elements just like in normal array.
Passing array to a function in the Single dimensional array
In this method, we pass an array to a function. see below syntax-
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
get_element(arr);
In the syntax given above, inside the loop, all the elements of the array are stored in array variable arr[i] and then a function has been called outside the loop, which will be passed the base address of its first element.
how to pass an array into a function?
Firstly in the function declaration, as arguments declare an array,
void get_array(int[]);
whereas in calling function, the function can be called only by array name ( means, without [] operator), such as-
get_array(array-name);
But in Function Definition, arr[] name will be declared with subscript operator []
void get_array(int arr[])
{
..........;
}
Here is an example,
#include<iostream>
using namespace std;
void get_element(int[]);// function declaration
int main()
{
int arr[5];
cout<<"Enter the five Element in the Array\n";
for(int i=0;i<5;i++)
{
cout<<" arr["<<i<<"]=";
cin>>arr[i]; //store element into arr[i]
}
<strong> get_element(arr);</strong> //passing first element address
}
// function definition
void get_element(int arr[])
{
cout<<"Element display using function";
for(int i=0;i<5;i++)
{
cout<<"\n arr["<<i<<"]="<<arr[i];
}
}
OUTPUT
Enter the Element in the Array
arr[0]=3
arr[1]=5
arr[2]=2
arr[3]=4
arr[4]=8
Element display using function
arr[0]=3
arr[1]=5
arr[2]=2
arr[3]=4
arr[4]=8
Explanation
In the program, input is taken from the user which will be stored in the array-variable arr[].such as,
arr[5] = 3 5 4 2 8
First, inside the loop, we have stored the element from the user in the array-variable arr[i]. Then out of the loop, this array is passed to function void get_array(arr). such as,
for(.....)
{
....
cin>>arr[i];
}
get_array(arr);
Remember, here we only arr name to pass the base address of its first element into function get_array(arr)
Inside the function body we use loop to access other elements.
But what, if Array-Elements are already declare in the Program
Then,
void main()
{
int arr[5]={3,4,2,1,4};
get_element(arr);
}
and other statements will be same as above.
Note here, if the array size is given by the user, then only one extra parameter will be added to the function, then
void get_array(int[],int); // in function declaration
void get_array(arr,size); // in function calling
void get_array(int arr[],int size){ } // in function definition
Passing an Array to a function in multi dimensional array
Its syntax is slightly different from the syntax of the above program,
Firstly in Function declaration, as arguments declare an array,
void get_array(int[2][3]);
whereas in calling function, the function can be called only by array name ( means, without [] operator) same as in Single dimensional array, such as-
get_array(array-name);
But in Function Definition, But in the formal parameter arr will be declared with its array-size,
void get_array(int arr[2][3]) { ..........; }
Here is the program,
#include<iostream>
using namespace std;
void get_array(int[2][3]);// function declaration
void main()
{
clrscr();
cout<<"\nEnter Five Element in the Array\n";
for(int i=0;i<2;i++)
{
for(int j=0; j<3;j++)
{
cout<<"arr["<<i<<"]["<<j<<"] = ";
cin>>arr[i][j];
}
}
get_array(arr);//base address will be passed
return 0;
}
// function definition
void get_array(int arr[2][3])+
{
cout<<"\nElement display using function\n";
for(int i=0;i<2;i++)
{
for(int j=0; j<3;j++)
{
cout<<"arr["<<i<<"]["<<j<<"] = "<<arr[i][j]<<endl;
}
}
}
OUTPUT
Enter six Element in the Array
arr[0][0] = 2
arr[0][1] = 1
arr[0][2] = 4
arr[1][0] = 5
arr[1][1] = 7
arr[1][2] = 6
Element display using function
arr[0][0] = 2
arr[0][1] = 1
arr[0][2] = 4
arr[1][0] = 5
arr[1][1] = 7
arr[1][2] = 6
Explanation
In the program, input is taken from the user which will be stored in the array-variable arr[i][j].such as,
arr[2][3] = 2 1 4 5 7 6
First, inside the loop, we have stored the elements from the user in the array-variable arr[i][j]. Then outside of the loop, the entire array is passed to the function get_array(arr). such as,
for(.....)
{
....
cin>>arr[i][j];
}
get_array(arr);
Remember Here, we have only used the name of the array-variable arr to pass the array elements and as we know, array name always refers to the base address of its first element, so we can say that here we pass the base address of the first element to the function void get_array(arr[][]).
Where in function void get_array(arr[][] we use loop to access other elements of Array.
But In Two Dimensional Array Elements Are Already Declare In The Program
Then,
int main()
{
int arr[2][3]={
{3,4,3},
{3,2,4}
};
get_array(arr);
return 0;
}
and other statements will be same as before.
Note: when we pass an array to a function then Only base address will be passed of array’s first element while Passing Array Elements to a function C++ we pass value not address it is also the main difference between them.
Related exercise: