1/26/24, 10:10 PM C++ Pass Array to Function
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
C++ Pass Array to a Function
❮ Previous Next ❯
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;
}
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
HTML CSS when
Note that JAVASCRIPT SQL
you call the function, PYTHON JAVA
you only need to usePHP HOW
the name ofTO W3.CSS
the array C
when passing it as an argument myFunction(myNumbers) . However, the full
declaration of the array is needed in the function parameter ( int myNumbers[5] ).
❮ Previous Log in to track progress Next ❯
COLOR PICKER
https://www.w3schools.com/cpp/cpp_function_array.asp 2/4
1/26/24, 10:10 PM C++ Pass Array to Function
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
SPACES UPGRADE AD-FREE
NEWSLETTER GET CERTIFIED
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
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
https://www.w3schools.com/cpp/cpp_function_array.asp 3/4
1/26/24, 10:10 PM C++ Pass Array to Function
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT
SQL
FORUM PYTHON
ABOUT JAVA PHP HOW TO W3.CSS C
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by
W3.CSS.
https://www.w3schools.com/cpp/cpp_function_array.asp 4/4