In this tutorial, we will explore how to use arrays as parameters when calling functions in C++. We will look at examples of both one-dimensional and multi-dimensional arrays.
In C++, we can pass an array of data as an argument to a function, and the function can also return an array of data as a result.
The syntax for Passing Arrays as Function Parameters
The following is the general syntax of passing an array to a function:
Return_Type Function_Name (Type Array-Name [Size-Of-Array]){
//statements
}
For example,
int sumOfArray(int arr[5]) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
return sum;
}
The above example code takes in an array of size 5
as an argument and computes the sum of all elements in the array. It does this by iterating through each element in the array and adding it to a sum variable
, which is then returned at the end of the program.
Example 1: A C++ Program To Pass a One-Dimensional Array To Function
#include <iostream> using namespace std; //Function declaration void showArray(int arr[], int size); int main(){ int size; cout << "Enter The Number of Students: "; cin >> size; int myArray[size]; cout << "\nEnter The Student Marks: " << endl; for (int i = 0; i < size; i++) { cout << "Student " << i+1 << " Marks: "; cin >> myArray[i]; } cout << "\nDisplaying The Student Marks: " << endl; showArray(myArray, size); return 0; } //Function definition void showArray(int arr[], int size){ for(int i=0; i<size; i++){ cout<<"Student " << i+1 << " marks: " << arr[i]<<endl; } }
OUTPUT:
Enter The Number of Students: 4
Enter The Student Marks:
Student 1 Marks: 49
Student 2 Marks: 76
Student 3 Marks: 39
Student 4 Marks: 46
Displaying The Student Marks:
Student 1 marks: 49
Student 2 marks: 76
Student 3 marks: 39
Student 4 marks: 46
The program above demonstrates how to pass an array to a function in C++. The program begins by declaring a function, showArray()
, which takes two parameters – an array, arr[]
and the size of the array, size.
Next, in the main()
function, the size of the array is taken as input from the user. Then, the elements of the array are taken as input from the user, one by one and stored in the array, myArray[]
.
The showArray()
function is then called with the array, myArray[]
and its size as parameters. Inside the showArray()
function, a for loop is used to iterate over the elements of the array and display them one by one.
Finally, the main()
function is ended, and the program is terminated.
Example 2: A C++ Program For Passing a Two-Dimensional Array To a Function
#include <iostream> using namespace std; //Function Declaration void display(int [][3]); int main() { //Declaring a Two-Dimensional Array int arr[2][3] = { {1, 2, 3}, {4, 5, 6} }; //Displaying elements of two-dimensional array display(arr); return 0; } //Function Definition void display(int arr[][3]) { cout << "Displaying The Values Of The Array" <<endl; cout << endl; for(int i = 0; i < 2; ++i) { for(int j = 0; j < 3; ++j) { cout << arr[i][j] << " "; } cout << endl; } }
OUTPUT:
Displaying The Values Of The Array
1 2 3
4 5 6
Step 1: Declare a two-dimensional array.
In the main()
function, we have declared a two-dimensional array of size 2x3
and initialized it with some values.
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Step 2: Call the function display()
and pass the two-dimensional array as a parameter.
In the main()
function, we have called the function display()
and passed the two-dimensional array arr
as the parameter.
display(arr);
Step 3: Write the function display()
and pass the two-dimensional array as a parameter.
In this program, we have written the function display()
and passed the two-dimensional array arr
as the parameter.
void display(int arr[][3])
Step 4: Display the contents of the two-dimensional array.
In the display()
function, we have used two for loops to iterate over the array and display its contents.
for(int i = 0; i < 2; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
Step 5: End the program.
At the end of the program, we have returned 0 from the main()
function, which terminates the program.
Returning an Array From a Function in C++
C++ does not allow for returning an entire array as a function return value. However, it is possible to return a pointer to an array by specifying the array’s name without an index.
For example, the following function, called myFunction
, returns an array of 10 integers:
int myFunction()
{
int myArray[10];
// Initialize myArray with values
// . . .
return myArray;
}
The function myFunction
can be called from within another function, as follows:
int main()
{
// Call myFunction
int *myArray = new int[10];
myArray = myFunction();
// Do something with myArray
// . . .
delete [] myArray;
return 0;
}
In this example, the myFunction
returns an array of 10
integers which is stored in the pointer myArray
. The pointer is then used to access the array elements, which can be manipulated in any way. Finally, the memory allocated to the array is freed using the delete []
operator.
Example 3: A C++ Program To Return an Array From Another Function Using Pointer
#include <iostream> using namespace std; //function that returns an array from another function using pointer int *getArray() { //Create an array static int array[3]; //Get values for the array elements from the user cout << "Enter three elements of the array : \n"; for (int i = 0; i < 3; i++) { cin >> array[i]; } //Return the array return array; } //main function int main() { int *p; //calling the function and store the returned value in pointer p = getArray(); //displaying the values of the array cout << "The values of the array are: "; for (int i = 0; i < 3; i++) { cout << *(p + i) << " "; } return 0; }
OUTPUT:
Enter three elements of the array :
5
2
3
The values of the array are: 5 2 3
In the above program, first, the getArray()
function is declared. This function does not take any arguments but returns an integer pointer.
In the getArray()
function, an array of size 3
is declared. This array is initialized with values from the user. The user is asked to enter three elements of the array.
Once the user enters the values, the array is returned using a return statement.
In the main function, a pointer p
is declared. This pointer stores the returned value from the getArray()
function.
The getArray()
function is then called, and the returned value is stored in the pointer p
.
Finally, the values of the array are displayed by looping through the elements of the array and printing the value stored at each index.