Passing Array to a Function in C++

Passing Array to a Function in C++ 

Passing Array to a Function in C++ Programming








Arrays can be passed to a function as an argument. Consider this example to pass a one-dimensional array to a function:

Example 1: Passing One-dimensional Array to a Function

C++ Program to display marks of 5 students by passing one-dimensional array to a function.
  1. #include <iostream>
  2. using namespace std;
  3. void display(int marks[5]);
  4. int main()
  5. {
  6. int marks[5] = {88, 76, 90, 61, 69};
  7. display(marks);
  8. return 0;
  9. }
  10. void display(int m[5])
  11. {
  12. cout << "Displaying marks: "<< endl;
  13. for (int i = 0; i < 5; ++i)
  14. {
  15. cout << "Student "<< i + 1 <<": "<< m[i] << endl;
  16. }
  17. }
Output
Displaying marks: 
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69
When an array is passed as an argument to a function, only the name of an array is used as argument.
display(marks);
Also notice the difference while passing array as an argument rather than a variable.
void display(int m[5]);
The argument marks in the above code represents the memory address of first element of array marks[5].
And the formal argument int m[5] in function declaration converts to int* m;. This pointer points to the same address pointed by the array marks.
That's the reason, although the function is manipulated in the user-defined function with different array name m[5], the original array marks is manipulated.
C++ handles passing an array to a function in this way to save memory and time.

Passing Multidimensional Array to a Function

Multidimensional array can be passed in similar way as one-dimensional array. Consider this example to pass two dimensional array to a function:

Example 2: Passing Multidimensional Array to a Function

C++ Program to display the elements of two dimensional array by passing it to a function.
  1. #include <iostream>
  2. using namespace std;
  3. void display(int n[3][2]);
  4. int main()
  5. {
  6. int num[3][2] = {
  7. {3, 4},
  8. {9, 5},
  9. {7, 1}
  10. };
  11. display(num);
  12. return 0;
  13. }
  14. void display(int n[3][2])
  15. {
  16. cout << "Displaying Values: " << endl;
  17. for(int i = 0; i < 3; ++i)
  18. {
  19. for(int j = 0; j < 2; ++j)
  20. {
  21. cout << n[i][j] << " ";
  22. }
  23. }
  24. }
Output
Displaying Values: 
3 4 9 5 7 1 
In the above program, the multi-dimensional array num is passed to the function display().
Inside, display() function, the array n (num) is traversed using a nested for loop.
The program uses 2 for loops to iterate over the elements inside a 2-dimensional array. If it were a 3-dimensional array, you should use 3 for loops.
Finally, all elements are printed onto the screen.
Note: Multidimensional array with dimension more than 2 can be passed in similar way as two dimensional array.

   

Comments

Popular posts from this blog

C++ Templates ?

Function Overriding in C++ [With Example]

C++ Program to Print Number Entered by User