return

A return statement ends the processing of the current function and returns control to the caller of the function.

A return statement in a function is optional. The compiler issues a warning if a return statement is not found in a function declared with a return type. If the end of a function is reached without encountering a return statement, control is passed to the caller as if a return statement without an expression were encountered. A function can contain multiple return statements.

Value of a return Expression and Function Value
If an expression is present on a return statement, the value of the expression is returned to the caller. If the data type of the expression is different from the function return type, conversion of the return value takes place as if the value of the expression were assigned to an object with the same function return type.

If an expression is not present on a return statement, the value of the return statement is undefined. If an expression is not given on a return statement in a function declared with a nonvoid return type, a warning message is issued, and the result of calling the function is unpredictable.

You cannot use a return statement with an expression when the function is declared as returning type void.

If a function returns a class object with constructors, a temporary class object might be constructed. The temporary object is not in the scope of the function returning the temporary object but is local to the caller of the function.

When a function returns, all temporary local variables are destroyed. If local class objects with destructors exist, destructors are called.



Expression Statement
Temporary Objects
Examples Using the return Statement