C++ Pass Array to Function
C++ Pass Array to Function
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
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;
}
Try it Yourself »
Example Explained
The function ( myFunction ) takes an array as its parameter ( int myNumbers[5] ), and
loops through the array elements with the for loop.
https://www.w3schools.com/cpp/cpp_function_array.asp 1/4
1/26/24, 10:10 PM C++ Pass Array to Function
When the function is called inside main() , we pass along the myNumbers array, which
Tutorials Exercises
outputs the array elements.
Services Sign Up Log in
COLOR PICKER
https://www.w3schools.com/cpp/cpp_function_array.asp 2/4
1/26/24, 10:10 PM C++ Pass Array to Function
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
https://www.w3schools.com/cpp/cpp_function_array.asp 3/4
1/26/24, 10:10 PM C++ Pass Array to Function
HTML
CSS JAVASCRIPT
SQL
FORUM PYTHON
ABOUT JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/cpp/cpp_function_array.asp 4/4