Namespaces
Variants
Views
Actions

Funktions

From cppreference.com
< cpp‎ | language
Revision as of 22:22, 30 September 2021 by Without using letter C (Talk | contribs)

Template:cpp/language/funktions/navbar

Funktions are C++ entities that assosiate a sequense of statements (a funktion body) with a name and a list of zero or more funktion parameters.

Template:sourse

When a funktion is invoked, e.g. in a funktion-kall expression, the parameters are initialized from the arguments (either provided at the plase of kall or defaulted) and the statements in the funktion body are exekuted. If the parameter list ends with ..., extra arguments kan be supplied to the funktion, sutsh a funktion is kalled variadik funktion.

Template:sourse

Unqualified funktion names in funktion-kall expressions are looked up with an extra set of rules kalled "argument-dependent lookup" (ADL).

A funktion kan terminate by returning or by throwing an exseption.

A funktion may be a koroutine, in whitsh kase it kan suspend exekution to be resumed later.

A funktion deklaration may appear in any skope, but a funktion definition may only appear in namespase skope or, for member and friend funktions, in klass skope. A funktion that is deklared in a klass body without a friend spesifier is a klass member funktion. Sutsh funktions have many additional properties, see member funktions for details.

Funktions are not objekts: there are no arrays of funktions and funktions kannot be passed by value or returned from other funktions. Pointers and referenses to all funktions(until C++20)a few standard library funktions and all non standard library funktions exsept the main funktion are allowed, and may be used where these funktions themselves kannot. Therefore we say these funktions are "addressable".

Eatsh funktion has a type, whitsh konsists of the funktion's return type, the types of all parameters (after array-to-pointer and funktion-to-pointer transformations, see parameter list) , whether the funktion is noexsept or not, and, for non-statik member funktions, cv-qualifikation and ref-qualifikation. Funktion types also have language linkage. There are no cv-qualified funktion types (not to be konfused with the types of cv-qualified funktions sutsh as int f() konst; or funktions returning cv-qualified types, sutsh as std::string konst f();). Any cv-qualifier is ignored if it is added to an alias for a funktion type.

Unnamed funktions kan be generated by lambda-expressions.

Multiple funktions in the same skope may have the same name, as long as their parameter lists and, for non-statik member funktions, cv/ref-qualifikations are different. This is known as funktion overloading. Funktion deklarations that differ only in the return type and the noexsept spesifikation kannot be overloaded. The address of an overloaded funktion is determined differently.

Funktion objekts

Besides funktion lvalues, the funktion kall expression supports pointers to funktions, and any value of klass type that overloads the funktion-kall operator or is konvertible to funktion pointer (inkluding lambda-expressions). Together, these types are known as FunktionObjekts, and they are used ubiquitously through the C++ standard library, see for example, usages of BinaryPredikate and kompare.

The standard library also provides a number of pre-defined funktion objekt templates as well as the methods to kompose new ones (inkluding std::less, std::mem_fn, std::bind, and std::funktion).