bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/C++/C++ Functions
C++•C++ Functions

C++ Pass Array to a Function

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind C++ Pass Array to a Function?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ myFunction(int myNumbers[5]) {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The function ( myFunction ) takes an array as its parameter ( int myNumbers[5] ), and loops through the array elements with the for loop.
You can also pass arrays to a function:
Pass Arrays as Function Parameters

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;
}

The function ( myFunction ) takes an array as its parameter ( int myNumbers[5] ), and loops through the array elements with the for loop.

When the function is called inside main() , we pass along the myNumbers array, 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 argument myFunction(myNumbers) . However, the full declaration of the array is needed in the function parameter ( int myNumbers[5] ).

Previous

C++ The Return Keyword

Next

C++ Pass Structures to a Function