Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Functions

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
       

      Functions are C++ entities that associate a sequence ofstatements (afunction body) with aname and a list of zero or morefunction parameters.

      // function name: "isodd"// parameter list has one parameter, with name "n" and type int// the return type is boolbool isodd(int n){// the body of the function beginsreturn n%2;}// the body of the function ends

      When a function is invoked, e.g. in afunction-call expression, the parameters are initialized from the arguments (either provided at the place of call ordefaulted) and the statements in the function body are executed. If theparameter list ends with..., extra arguments can be supplied to the function, such a function is calledvariadic function.

      int main(){for(int arg:{-3,-2,-1,0,1,2,3})std::cout<< isodd(arg)<<' ';// isodd called 7 times, each// time n is copy-initialized from arg}

      Unqualified function names in function-call expressions are looked up with an extra set of rules called"argument-dependent lookup" (ADL).

      A function can terminate byreturning or bythrowing anexception.

      A function may be acoroutine, in which case it can suspend execution to be resumed later.

      (since C++20)

      Afunction declaration may appear in any scope, but afunction definition may only appear in namespace scope or, formember andfriend functions, in class scope. A function that is declared in a class body without a friend specifier is a class member function. Such functions have many additional properties, seemember functions for details.

      Functions are not objects: there are no arrays of functions and functions cannot be passed by value or returned from other functions. Pointers and references to functions (except forthe main function andmost standard library functions(since C++20)) are allowed, and may be used where these functions themselves cannot. Therefore we say these functions are "addressable".

      Each function has a type, which consists of the function's return type, the types of all parameters (after array-to-pointer and function-to-pointer transformations, seeparameter list), whether the function isnoexcept or not(since C++17), and, for non-static member functions, cv-qualification and ref-qualification(since C++11). Function types also havelanguage linkage. There are no cv-qualified function types (not to be confused with the types ofcv-qualified functions such asint f()const; or functions returningcv-qualified types, such asstd::stringconst f();). Any cv-qualifier is ignored if it is added to an alias for a function type.

      Multiple functions in the same scope may have the same name, as long as their parameter lists and, for non-static member functions, cv/ref(since C++11)-qualifications are different. This is known asfunction overloading. Function declarations that differ only in the return typeand the noexcept specification(since C++17) cannot be overloaded. Theaddress of an overloaded function is determined differently.

      C++ implementsanonymous functions usinglambda-expressions.

      (since C++11)

      [edit]Function objects

      Besides function lvalues, the function call expression supports pointers to functions, and any value of class type that overloads the function-call operator or is convertible to function pointer (includinglambda-expressions)(since C++11). Together, these types are known asFunctionObjects, and they are used ubiquitously through the C++ standard library, see for example, usages ofBinaryPredicate andCompare.

      The standard library also provides a number of predefinedfunction object templates as well as the methods to compose new ones (includingstd::less,std::mem_fn,std::bind,std::function(since C++11),std::not_fn(since C++17),std::bind_front(since C++20),std::bind_back,std::move_only_function(since C++23),std::copyable_function, andstd::function_ref(since C++26)).

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/functions&oldid=159983"

      [8]ページ先頭

      ©2009-2025 Movatter.jp