Skip to content

Search topics & cppreference

Search all study topics, concept maps, and 6,587 C++ and C reference pages.

C++ keyword: return

Keywords
  • C++17
  • C++20
  • C++23
1 min readView on cppreference
NotebookLM

Usage

  • return statement: as the declaration of the statement

Example

example.cpp
#include <cstdlib>
#include <iostream>
 
[[nodiscard]] constexpr auto clamp(int value, int min, int max) noexcept
{
    if (value <= min)
        return min;
    else if (max <= value)
        return max;
 
    return value;
    // won't be executed past 'return' statement
 
    std::exit(value);
}
 
int main() noexcept
{
    std::cout << clamp(1, 2, 4);
    std::cout << clamp(3, 2, 4);
    std::cout << clamp(5, 2, 4);
 
    return 0; // the value '0' that in main() indicates a success
}
Output
234

See also

(since C++17)
(since C++23)
(since C++20)

Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.