`return` — C++ Keyword
`return` — C++ Keyword
The return keyword in C++: exits the current function, optionally returning a value.
`return` — C++ Keyword
The return keyword in C++: exits the current function, optionally returning a value.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
returnExits the current function. An expression may follow return to provide the function's return value; void functions use bare return (or fall off the end).
return; // void function
return expression; // returns a value
return { initializer-list }; // aggregate/brace init
#include <print>
int add(int a, int b) {
return a + b; // returns int
}
bool is_positive(int n) {
if (n > 0) return true;
return false;
}
std::pair<int, int> divmod(int a, int b) {
return {a / b, a % b}; // brace-init pair
}
int main() {
std::println("{}", add(3, 4)); // 7
std::println("{}", is_positive(-1)); // false
auto [q, r] = divmod(17, 5);
std::println("{} {}", q, r); // 3 2
}
main is equivalent to return 0;.void function is undefined behavior.returnint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}