Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Returning Values With Simple Data Types | Function Return Values Specification
course content

Зміст курсу

C++ Functions

Returning Values With Simple Data TypesReturning Values With Simple Data Types

In C++, functions can return values of simple data types such as integers, floating-point numbers, and characters. To specify the return type of a function, you indicate the data type before the function name in the function signature.

When the function is executed, it can compute a value, which is then returned using the return statement. This type of return value was used in the code examples before:

cpp

main.cpp

The addNumbers() function is declared to return an integer value by using the int specifier before the function name. It calculates the sum of a and b and returns the result as an int.

Note

Ensure that the variable where you intend to store the returned value inside the main() block matches the data type of the corresponding return value.

Please note that the function's return value can be specified only within the function signature. Even if you try to return a value of a different type using the return statement, it will be automatically cast to the data type declared in the function signature:

cpp

main.cpp

We observe that the sum calculated within the function is of double type, but the function's return type is declared as int. Consequently, the final return value was explicitly converted to an int data type, resulting in 8 instead of 8.6.

Note

Note that we can return only one value from a function using a simple datatype specifier. To return multiple values, we should use arrays or custom structures (classes).

What is the return type specifier in a C++ function signature used for?

Виберіть правильну відповідь

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

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

Зміст курсу

C++ Functions

Returning Values With Simple Data TypesReturning Values With Simple Data Types

In C++, functions can return values of simple data types such as integers, floating-point numbers, and characters. To specify the return type of a function, you indicate the data type before the function name in the function signature.

When the function is executed, it can compute a value, which is then returned using the return statement. This type of return value was used in the code examples before:

cpp

main.cpp

The addNumbers() function is declared to return an integer value by using the int specifier before the function name. It calculates the sum of a and b and returns the result as an int.

Note

Ensure that the variable where you intend to store the returned value inside the main() block matches the data type of the corresponding return value.

Please note that the function's return value can be specified only within the function signature. Even if you try to return a value of a different type using the return statement, it will be automatically cast to the data type declared in the function signature:

cpp

main.cpp

We observe that the sum calculated within the function is of double type, but the function's return type is declared as int. Consequently, the final return value was explicitly converted to an int data type, resulting in 8 instead of 8.6.

Note

Note that we can return only one value from a function using a simple datatype specifier. To return multiple values, we should use arrays or custom structures (classes).

What is the return type specifier in a C++ function signature used for?

Виберіть правильну відповідь

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

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