##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Functions

From cppreference.com
< cpp‎ | language

Functions are C++ entities that associate a sequence of statements (a function body) with a name and a list of zero or more function parameters.

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

When a function is invoked, e.g. in a function-call expression, the parameters are initialized from the arguments (either provided at the place of call or defaulted) and the statements in the function body are executed.

    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
    }

A function can terminate by returning or by throwing an exception.

A function declaration may appear in any scope, but a function definition may only appear in namespace scope or, for member and friend 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 member 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 are allowed, and may be used where functions themselves cannot.

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 parameter list), and, for member functions, cv-qualification and ref-qualification. Function types also have language linkage. There are no cv-qualified function types (not to be confused with the types of cv-qualified functions such as int f() const; or functions returning cv-qualified types, such as std::string const f();)

Unnamed functions can be generated by lambda-expressions.

Multiple functions in the same scope may have the same name, as long as their parameter lists and, for member functions, cv/ref qualifications are different. This is known as function overloading.

| This section is incomplete Reason: links to other Function subpages

### [edit] Function objects

Besides function lvalues, the function call expression supports pointers to functions, dereferenced pointers to member functions, lambda-expressions, and any variable of class type that overloads the function-call operator. Together, these types are known as `FunctionObject`s, and they are used ubiquitously through the C++ standard library, see for example, usages of `BinaryPredicate` and `Compare`.

The standard library also provides a number of pre-defined function object templates as well as the methods to compose new ones (including std::mem_fn, std::bind, and std::function).

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

- Todo with reason