Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Passing Static Array as an Argument of the Function | Function Arguments Specification
C++ Functions
course content

Зміст курсу

C++ Functions

C++ Functions

1. Introduction
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics

Passing Static Array as an Argument of the Function

In C++, a static array is an array whose size is determined at compile time and remains constant throughout the program's execution.

Like values with simple data types, we can pass arrays as function arguments!

Pass 1-dimensional array as an argument

To pass a 1-dimensional array as an argument of the function, we have to use [] brackets after the variable name inside the function signature:

cpp

main

Pass a 2-dimensional array as an argument

Passing a 2-dimensional array is very similar to passing a 1-dimensional array: we have to use the [][] after the variable name.

But there is one important difference: in C++, you cannot directly pass a 2D array using the syntax datatype arrayName[][] as a function argument.

When you pass a 2D array to a function, you need to specify the size of at least one array dimension inside the [][] brackets. This is because C++ requires knowing the size of the dimension to calculate memory offsets when accessing elements in the array properly.

cpp

main

Since we need to specify at least one of the array dimensions (it doesn’t matter which), the signature of this function can also look like this:

Specify only number of rows

or

Specify both number of rows and columns

When we call the function inside the main() block, we use the name of an array as an argument without any additional operators.

Все було зрозуміло?

Секція 2. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt