You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Functions are C++ entities that associate a sequence of <DocLinksrc="/cpp/language/statements">statements</DocLink> (a _function body_) with a _name_ and a list of zero or more _function parameters_.
11
+
Functions are C++ entities that associate a sequence of <DocLinkdest="/cpp/language/statements">statements</DocLink> (a _function body_) with a _name_ and a list of zero or more _function parameters_.
12
12
13
13
```cpp
14
14
// function name: "isodd"
@@ -19,7 +19,7 @@ bool isodd(int n) { // the body of the function begins
19
19
} // the body of the function ends
20
20
```
21
21
22
-
When a function is invoked, e.g. in a <DocLink src="/cpp/language/expressions/operator_other" section="built-in-function-call-operator">function-call expression</DocLink>, the parameters are initialized from the arguments (either provided at the place of call or <DocLink src="/cpp/language/functions/default_arguments">defaulted</DocLink>) and the statements in the function body are executed. If the <DocLink src="/cpp/language/functions/function" section="parameter-list">parameter list</DocLink> ends with `...`, extra arguments can be supplied to the function, such a function is called <DocLink src="/cpp/language/functions/variadic_arguments">variadic function</DocLink>.
22
+
When a function is invoked, e.g. in a <DocLink dest="/cpp/language/expressions/operator_other" section="built-in-function-call-operator">function-call expression</DocLink>, the parameters are initialized from the arguments (either provided at the place of call or <DocLink dest="/cpp/language/functions/default_arguments">defaulted</DocLink>) and the statements in the function body are executed. If the <DocLink dest="/cpp/language/functions/function" section="parameter-list">parameter list</DocLink> ends with `...`, extra arguments can be supplied to the function, such a function is called <DocLink dest="/cpp/language/functions/variadic_arguments">variadic function</DocLink>.
23
23
24
24
```cpp
25
25
int main() {
@@ -29,28 +29,28 @@ int main() {
29
29
}
30
30
```
31
31
32
-
<DocLinksrc="/cpp/language/basic_concepts/unqualified_lookup">Unqualified</DocLink> function names in function-call expressions are looked up with an extra set of rules called <DocLinksrc="/cpp/language/functions/adl">"argument-dependent lookup" (ADL)</DocLink>.
32
+
<DocLinkdest="/cpp/language/basic_concepts/unqualified_lookup">Unqualified</DocLink> function names in function-call expressions are looked up with an extra set of rules called <DocLinkdest="/cpp/language/functions/adl">"argument-dependent lookup" (ADL)</DocLink>.
33
33
34
-
A function can terminate by <DocLinksrc="/cpp/language/statements/return">returning</DocLink> or by <DocLinksrc="/cpp/language/exceptions/throw">throwing</DocLink> an <DocLinksrc="/cpp/language/exceptions">exception</DocLink>.
34
+
A function can terminate by <DocLinkdest="/cpp/language/statements/return">returning</DocLink> or by <DocLinkdest="/cpp/language/exceptions/throw">throwing</DocLink> an <DocLinkdest="/cpp/language/exceptions">exception</DocLink>.
35
35
36
36
<RevisionBlocksince="C++20">
37
-
A function may be a <DocLinksrc="/cpp/language/functions/coroutines">coroutine</DocLink>, in which case it can suspend execution to be resumed later.
37
+
A function may be a <DocLinkdest="/cpp/language/functions/coroutines">coroutine</DocLink>, in which case it can suspend execution to be resumed later.
38
38
</RevisionBlock>
39
39
40
-
A <DocLinksrc="/cpp/language/functions/function">function declaration</DocLink> may appear in any scope, but a <DocLinksrc="/cpp/language/functions/function">function definition</DocLink> may only appear in namespace scope or, for <DocLinksrc="/cpp/language/classes/member_functions">member</DocLink> and <DocLinksrc="/cpp/language/classes/friend">friend</DocLink> 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, see <DocLinksrc="/cpp/language/classes/member_functions">member functions</DocLink> for details.
40
+
A <DocLinkdest="/cpp/language/functions/function">function declaration</DocLink> may appear in any scope, but a <DocLinkdest="/cpp/language/functions/function">function definition</DocLink> may only appear in namespace scope or, for <DocLinkdest="/cpp/language/classes/member_functions">member</DocLink> and <DocLinkdest="/cpp/language/classes/friend">friend</DocLink> 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, see <DocLinkdest="/cpp/language/classes/member_functions">member functions</DocLink> for details.
41
41
42
-
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 for the <DocLinksrc="/cpp/language/basic_concepts/main_function">main function</DocLink><Revisionsince="C++20"> and <DocLinksrc="/cpp/language/extending_std"section="addressing-restriction">most standard library functions</DocLink></Revision>) are allowed, and may be used where these functions themselves cannot. Therefore we say these functions are "addressable".
42
+
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 for the <DocLinkdest="/cpp/language/basic_concepts/main_function">main function</DocLink><Revisionsince="C++20"> and <DocLinkdest="/cpp/language/extending_std"section="addressing-restriction">most standard library functions</DocLink></Revision>) are allowed, and may be used where these functions themselves cannot. Therefore we say these functions are "addressable".
43
43
44
-
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, see <DocLink src="/cpp/language/functions/function" section="parameter-list">parameter list</DocLink>) <Revision since="C++17">, whether the function is <DocLink src="/cpp/language/exceptions/noexcept_spec">noexcept</DocLink> or not</Revision>, and, for non-static member functions, cv-qualification <Revision since="C++11">and ref-qualification</Revision>. Function types also have <DocLink src="/cpp/language/declarations/language_linkage">language linkage</DocLink>. There are no cv-qualified function types (not to be confused with the types of <DocLink src="/cpp/language/declarations/language_linkage">cv-qualified functions</DocLink> such as `int f() const;` or functions returning <DocLink src="/cpp/language/declarations/cv">cv-qualified types</DocLink>, such as `std::string const f();`). Any cv-qualifier is ignored if it is added to an alias for a function type.
44
+
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, see <DocLink dest="/cpp/language/functions/function" section="parameter-list">parameter list</DocLink>) <Revision since="C++17">, whether the function is <DocLink dest="/cpp/language/exceptions/noexcept_spec">noexcept</DocLink> or not</Revision>, and, for non-static member functions, cv-qualification <Revision since="C++11">and ref-qualification</Revision>. Function types also have <DocLink dest="/cpp/language/declarations/language_linkage">language linkage</DocLink>. There are no cv-qualified function types (not to be confused with the types of <DocLink dest="/cpp/language/declarations/language_linkage">cv-qualified functions</DocLink> such as `int f() const;` or functions returning <DocLink dest="/cpp/language/declarations/cv">cv-qualified types</DocLink>, such as `std::string const f();`). Any cv-qualifier is ignored if it is added to an alias for a function type.
45
45
46
-
Multiple functions in the same scope may have the same name, as long as their parameter lists and, for non-static member functions, cv<Revisionsince="C++11">/ref</Revision>-qualifications are different. This is known as <DocLinksrc="/cpp/language/functions/overload_resolution">function overloading</DocLink>. Function declarations that differ only in the return type <Revisionsince="C++17">and the noexcept specification</Revision> cannot be overloaded. The <DocLinksrc="/cpp/language/functions/overloaded_address">address of an overloaded function</DocLink> is determined differently.
46
+
Multiple functions in the same scope may have the same name, as long as their parameter lists and, for non-static member functions, cv<Revisionsince="C++11">/ref</Revision>-qualifications are different. This is known as <DocLinkdest="/cpp/language/functions/overload_resolution">function overloading</DocLink>. Function declarations that differ only in the return type <Revisionsince="C++17">and the noexcept specification</Revision> cannot be overloaded. The <DocLinkdest="/cpp/language/functions/overloaded_address">address of an overloaded function</DocLink> is determined differently.
47
47
48
48
<RevisionBlocksince="C++11">
49
-
C++ implements [anonymous functions](https://en.wikipedia.org/wiki/anonymous_function) using <DocLinksrc="/cpp/language/functions/lambda">lambda-expressions</DocLink>.
49
+
C++ implements [anonymous functions](https://en.wikipedia.org/wiki/anonymous_function) using <DocLinkdest="/cpp/language/functions/lambda">lambda-expressions</DocLink>.
50
50
</RevisionBlock>
51
51
52
52
## Function objects
53
53
54
-
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<Revisionsince="C++11">( including <DocLinksrc="/cpp/language/functions/lambda">lambda-expressions</DocLink>)</Revision>. Together, these types are known as <NamedReqname="FunctionObjects" />, and they are used ubiquitously through the C++ standard library, see for example, usages of <NamedReqname="BinaryPredicate" /> and <NamedReqname="Compare" />.
54
+
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<Revisionsince="C++11">( including <DocLinkdest="/cpp/language/functions/lambda">lambda-expressions</DocLink>)</Revision>. Together, these types are known as <NamedReqname="FunctionObjects" />, and they are used ubiquitously through the C++ standard library, see for example, usages of <NamedReqname="BinaryPredicate" /> and <NamedReqname="Compare" />.
55
55
56
-
The standard library also provides a number of predefined <DocLinksrc="/cpp/library/utility/functional">function object templates</DocLink> as well as the methods to compose new ones (including <DocLinksrc="/cpp/library/utility/functional/less">`std::less`</DocLink><Revisionsince="C++11">, <DocLinksrc="/cpp/library/utility/functional/mem_fn">`std::mem_fn`</DocLink>, <DocLinksrc="/cpp/library/utility/functional/bind">`std::bind`</DocLink>, <DocLinksrc="/cpp/library/utility/functional/function">`std::function`</DocLink></Revision><Revisionsince="C++17">, <DocLinksrc="/cpp/library/utility/functional/not_fn">`std::not_fn`</DocLink></Revision><Revisionsince="C++20">, <DocLinksrc="/cpp/library/utility/functional/bind_front">`std::bind_front`</DocLink></Revision><Revisionsince="C++23">, `std::bind_back`, `std::move_only_function`</Revision><Revisionsince="C++26">, `std::copyable_function`, and `std::function_ref`</Revision>).
56
+
The standard library also provides a number of predefined <DocLinkdest="/cpp/library/utility/functional">function object templates</DocLink> as well as the methods to compose new ones (including <DocLinkdest="/cpp/library/utility/functional/less">`std::less`</DocLink><Revisionsince="C++11">, <DocLinkdest="/cpp/library/utility/functional/mem_fn">`std::mem_fn`</DocLink>, <DocLinkdest="/cpp/library/utility/functional/bind">`std::bind`</DocLink>, <DocLinkdest="/cpp/library/utility/functional/function">`std::function`</DocLink></Revision><Revisionsince="C++17">, <DocLinkdest="/cpp/library/utility/functional/not_fn">`std::not_fn`</DocLink></Revision><Revisionsince="C++20">, <DocLinkdest="/cpp/library/utility/functional/bind_front">`std::bind_front`</DocLink></Revision><Revisionsince="C++23">, `std::bind_back`, `std::move_only_function`</Revision><Revisionsince="C++26">, `std::copyable_function`, and `std::function_ref`</Revision>).
0 commit comments