Returns control from a function to its caller.
If return
has an operand, the operand is converted to the function's return type, and the converted value is returned to the caller.
int f() {
return 42;
}
int x = f(); // x is 42
int g() {
return 3.14;
}
int y = g(); // y is 3
If return
does not have an operand, the function must have void
return type. As a special case, a void
-returning function can also return an expression if the expression has type void
.
void f(int x) {
if (x < 0) return;
std::cout << sqrt(x);
}
int g() { return 42; }
void h() {
return f(); // calls f, then returns
return g(); // ill-formed
}
When main
returns, std::exit
is implicitly called with the return value, and the value is thus returned to the execution environment. (However, returning from main
destroys automatic local variables, while calling std::exit
directly does not.)
int main(int argc, char** argv) {
if (argc < 2) {
std::cout << "Missing argument\n";
return EXIT_FAILURE; // equivalent to: exit(EXIT_FAILURE);
}
}