ReturnReturn

The return statement terminates the execution of a function and returns a value of a predefined type.

If the type is specified incorrectly, the function will behave unpredictably:

cpp

main.cpp

That is, before creating a function, the type of data that it returns must be specified. Also, in C++, there are special void functions. Functions of this data type are allowed to return nothing:

cpp

main.cpp

or return "nothing":

cpp

main.cpp

Note

Usually, void-type functions simply display static text or work with pointers (spoilers).

A lot of returns

There can be multiple returns inside functions, and each one will only fire under certain conditions.

cpp

main.cpp

If there are two returns, the second return function will be ignored:

cpp

main.cpp

question-icon

What will the function return?

Select the correct answer

Everything was clear?

Section 5. Chapter 3