The function return value is the result after function execution, and the type can be basic type, custom type or void. The return type is specified in the function declaration, void means no value is returned. The meaning of the return value is determined by the function implementation, for example, it can represent a result, function status, or data pointer. In practical cases, the triangleArea function returns the triangle area or throws an exception when input is illegal. The main function processes the return value and displays the result or error message.
The inside story of C function return value: List of types and meanings
In C programming, the function return value is the value after the function is executed the result of. It can be a basic data type (such as int, float), a custom data type (such as struct, class), or void.
The return value type of a function defines the type of value returned by the function. Specify the return value type in the function declaration:
int myFunction();
The above code declares a function named myFunction
whose return value type is int, which means that the function will return an int value .
void
is a special return value type, which means that the function does not return any value. When using void
as the return value type, the function declaration should look like this:
void myFunction();
The meaning of the function return value depends on the implementation of the function itself. It may represent the result of an operation, the state of a function, or a pointer to data.
For example, the following function returns the sum of two numbers:
int add(int a, int b) { return a + b; }
And the following function returns a Boolean value indicating the success (true) or failure (false) of the function:
bool isSuccessful() { // ... 函数体 ... return true; }
Calculate the area of a triangle:
#include <iostream> #include <cmath> using namespace std; double triangleArea(double base, double height) { if (base <= 0 || height <= 0) { throw invalid_argument("Base and height must be positive"); } return 0.5 * base * height; } int main() { double base, height; cout << "Enter the base of the triangle: "; cin >> base; cout << "Enter the height of the triangle: "; cin >> height; try { double area = triangleArea(base, height); cout << "The area of the triangle is: " << area << endl; } catch (invalid_argument& e) { cout << e.what() << endl; } return 0; }
The return value meaning of this example:
triangleArea
The function returns the area of the triangle. If any of the parameters passed (base, height) are non-positive numbers, an invalid_argument
exception is thrown. In the main
function, if the calculation is successful, the return value of triangleArea
is stored in the area
variable and printed out. main
function catches the invalid_argument
exception and displays an error message. The above is the detailed content of The inside story of C++ function return values: an overview of types and meanings. For more information, please follow other related articles on the PHP Chinese website!