Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Returning Arrays from the Function | Function Return Values Specification
course content

Зміст курсу

C++ Functions

Returning Arrays from the FunctionReturning Arrays from the Function

Just like simple data types, arrays can be returned as function results.

However, there is a crucial limitation: only dynamic arrays can be returned from functions. This restriction arises from the local scope of functions; static arrays created within a function are only accessible within that function.

If you attempt to return a locally created array, you'll encounter a limitation due to the function's scope (you will try to return an address of non-existing variables that will lead to an error).

Returning a dynamic array

Why can we return dynamic arrays?
Unlike local variables, which are deallocated when a function exits, dynamic memory allocated using new[] allows data to survive beyond the scope of the function where it was created. As a result, we can access variables created inside the function using their addresses.
To return a dynamic array, we have to use the following type specifiers in the function signature:

  • dataType* for 1D array.
  • dataType** for 2D array.

Returning 1D array

cpp

main.cpp

The function dynamically allocates memory for an integer array of the specified size. It initializes the array elements with values from 1 to the size of the array. The function returns a pointer on the first element of the dynamically allocated integer array that can now be used in the main() block.

Returning 2D array

cpp

main.cpp

The principle is the same as returning a 1D array: the only difference is that we have to return a pointer at the array of pointers using the int** type specifier to return the 2D array.

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

Секція 3. Розділ 2
course content

Зміст курсу

C++ Functions

Returning Arrays from the FunctionReturning Arrays from the Function

Just like simple data types, arrays can be returned as function results.

However, there is a crucial limitation: only dynamic arrays can be returned from functions. This restriction arises from the local scope of functions; static arrays created within a function are only accessible within that function.

If you attempt to return a locally created array, you'll encounter a limitation due to the function's scope (you will try to return an address of non-existing variables that will lead to an error).

Returning a dynamic array

Why can we return dynamic arrays?
Unlike local variables, which are deallocated when a function exits, dynamic memory allocated using new[] allows data to survive beyond the scope of the function where it was created. As a result, we can access variables created inside the function using their addresses.
To return a dynamic array, we have to use the following type specifiers in the function signature:

  • dataType* for 1D array.
  • dataType** for 2D array.

Returning 1D array

cpp

main.cpp

The function dynamically allocates memory for an integer array of the specified size. It initializes the array elements with values from 1 to the size of the array. The function returns a pointer on the first element of the dynamically allocated integer array that can now be used in the main() block.

Returning 2D array

cpp

main.cpp

The principle is the same as returning a 1D array: the only difference is that we have to return a pointer at the array of pointers using the int** type specifier to return the 2D array.

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

Секція 3. Розділ 2
some-alt