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

Passing Static Array as an Argument of the FunctionPassing 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.

An array in C++ is a collection of elements of the same data type!

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.cpp

Code Description
  • The processArray() function takes a 1-dimensional static array as its first argument.
  • We use the int arr[] constriction to declare that we will pass an array that consists of integer values as an argument of the function.
  • The array size is passed separately to ensure the we know the array's size inside the function during processing (because of the function's local scope, it won't "see" the variables declared in the main() function).
  • When we call the function inside the main() block, we use the name of an array without any additional operators as an argument of the processArray() function.
  • 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.cpp

    Code Description
    In this example, int matrix[][3] specifies that the function processMatrix() accepts a 2D array where the number of columns is fixed at 3. The number of rows is not specified because C++ can deduce it from the array passed to the function (but it is still passed as another argument, rows, to let us iterate this array).

    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
    course content

    Зміст курсу

    C++ Functions

    Passing Static Array as an Argument of the FunctionPassing 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.

    An array in C++ is a collection of elements of the same data type!

    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.cpp

    Code Description
  • The processArray() function takes a 1-dimensional static array as its first argument.
  • We use the int arr[] constriction to declare that we will pass an array that consists of integer values as an argument of the function.
  • The array size is passed separately to ensure the we know the array's size inside the function during processing (because of the function's local scope, it won't "see" the variables declared in the main() function).
  • When we call the function inside the main() block, we use the name of an array without any additional operators as an argument of the processArray() function.
  • 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.cpp

    Code Description
    In this example, int matrix[][3] specifies that the function processMatrix() accepts a 2D array where the number of columns is fixed at 3. The number of rows is not specified because C++ can deduce it from the array passed to the function (but it is still passed as another argument, rows, to let us iterate this array).

    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
    some-alt