Pass Arrays as Function Parameters
You can also pass arrays to a function:
Example
void myFunction(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}
}
int main() {
int
myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}
Note: Example Explained The function (
myFunction) takes an array as its parameter (int myNumbers[5]), and loops through the array elements with theforloop. When the function is called insidemain(), we pass along themyNumbersarray, which outputs the array elements. Note that when you call the function, you only need to use the name of the array when passing it as an argumentmyFunction(myNumbers). However, the full declaration of the array is needed in the function parameter (int myNumbers[5]).