English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn to pass arrays to C ++in the function. You will learn how to pass one-dimensional and multi-dimensional arrays.
arraycan be passed as a parameterFunction. It is also possible to return an array from the function. Consider the following example, passing a one-dimensional array to a function:
C ++The program displays the scores by passing a one-dimensional array to the function.5scores of students.
#include <iostream> using namespace std; void display(int marks[5]); int main() { int marks[5]= {88, 76, 90, 61, 69}); display(marks); return 0; } void display(int m[5]) { cout << "Display scores: "<< endl; for (int i = 0; i < 5; ++i) { cout << "Student " << i + 1 <<": "<< m[i] << endl; } }
Output result
Display scores: Student 1: 88 Student 2: 76 Student 3: 90 Student 4: 61 Student 5: 69
When an array is passed as a parameter to a function, only the array name is used as a parameter.
display(marks);
It should also be noted the difference between passing an array as a parameter and passing a variable.
void display(int m[5]);
The parameter marks in the above code represents the array marks[5] in the function declaration.
The memory address of the first element in the form parameter int m [5] converted to int * m; The pointer points to the same address pointed to by the array marks.
This is the reason, although the function is used with a different array name m[5perform operations, but the original array is still being operated on in marks.
C ++This way of handling passing arrays to functions saves memory and time.
Multidimensional ArraysMulti-dimensional arrays can be passed in a manner similar to one-dimensional arrays. Consider the following example, passing a two-dimensional array to a function:
C ++The program displays the array by passing the elements of the two-dimensional array to the function.
#include <iostream> using namespace std; void display(int n[3][2]); int main() { int num[3][2]= { {3, 4}, {9, 5}, {7, 1} }); display(num); return 0; } void display(int n[3][2]) { cout << "Display value: " << endl; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 2; ++j) { cout << n[i][j] << " "; } } }
Output result
Display value: 3 4 9 5 7 1
In the above program, the multi-dimensional array num is passed to the function display().
In the display() function, use nested for loops to traverse the array n (num).
This program uses2timesfor loopTraverse the elements in the two-dimensional array. If it is a three-dimensional array, then you should use3 for loop.
Finally, all elements are printed to the screen.
Note: dimensions greater than2Multi-dimensional arrays can be passed in a manner similar to two-dimensional arrays.
C++ It is not allowed to return a complete array as a function parameter. However, you can return a pointer to an array by specifying the array name without an index.
If you want to return a one-dimensional array from a function, you must declare a function that returns a pointer, as follows:
int * myFunction() { . . . }
Additionally, C++ It is not supported to return the address of a local variable outside the function unless the local variable is defined as static variable.
Now, let's look at the following function, which will generate 10 a random number, and use an array to return them, as follows:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; // Function to generate and return random numbers int * getRandom() { static int r[8]; // Set seed srand((unsigned)time(NULL)); for (int i = 0; i < 8; ++i) { r[i] = rand(); cout << r[i] << endl; } return r; } // the main function to call the above-defined function int main () { // a pointer to an integer int *p; p = getRandom(); for ( int i = 0; i < 8; i++ ) { cout << \*(p + << i << ") : "; cout << *(p + i) << endl; } return 0; }
When the above code is compiled and executed, it will produce the following results:
30737 23110 21765 14820 8295 12330 28395 191 *(p + 0) : ; 30737 *(p + 1) : ; 23110 *(p + 2) : ; 21765 *(p + 3) : ; 14820 *(p + 4) : ; 8295 *(p + 5) : ; 12330 *(p + 6) : ; 28395 *(p + 7) : ; 191