CKTIS / C++
return — ending a function and returning a result
The return statement ends the current function call and transfers control to its caller. It can also initialize a returned value or reference. It is not equivalent to break: inside a loop, it exits the entire function.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
return;
return expression;
return { initializer-list };return transfers control from the current function to its caller. The operand in return expression; or return { initializer-list }; initializes the returned reference or result object according to the function's return type; this does not necessarily involve copying. Unlike break, return inside a loop exits the entire function.
return; is permitted in a void function, constructor, or destructor. A void function can also use return expression; if the expression has type void. A value-returning function requires a compatible operand, not a bare return;.
The result is initialized before local objects are destroyed. When scopes are exited, constructed automatic objects are destroyed in reverse construction order. Do not return a pointer or reference to a local automatic object: once that object is destroyed, the result dangles.
Reaching the end of an ordinary void function, constructor, or destructor acts like return;. In an ordinary value-returning function it causes undefined behavior; main is an exception, where it is equivalent to return 0;. Coroutines use co_return, not return.
This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 publication; later changes must not automatically be applied to that mode. A repository compiler flag does not attest production runtime behavior.
Examples
#include <iostream>
int clampNonnegative(int value) {
if (value < 0) {
return 0;
}
return value;
}
int main() {
std::cout << clampNonnegative(-3) << ' '
<< clampNonnegative(7) << '\n';
return 0;
}
Result
0 7
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.