Зміст курсу
C++ Functions
C++ Functions
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:
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.
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.
Все було зрозуміло?