# Function (mathematics)

In mathematics, a function from a set X to a set Y assigns to each element of X exactly one element of Y. The set X is called the domain of the function and the set Y is called the codomain of the function.
Functions were originally the idealization of how a varying quantity depends on another quantity. For example, the position of a planet is a function of time. Historically, the concept was elaborated with the infinitesimal calculus at the end of the 17th century, and, until the 19th century, the functions that were considered were differentiable (that is, they had a high degree of regularity). The concept of a function was formalized at the end of the 19th century in terms of set theory, and this greatly increased the possible applications of the concept.
A function is often denoted by a letter such as f, g or h. The value of a function f at an element x of its domain (that is, the element of the codomain that is associated with x) is denoted by f(x); for example, the value of f at x = 4 is denoted by f(4). Commonly, a specific function is defined by means of an expression depending on x, such as $f(x)=x^{2}+1;$ in this case, some computation, called function evaluation, may be needed for deducing the value of the function at a particular value; for example, if $f(x)=x^{2}+1,$ then $f(4)=4^{2}+1=17.$
Given its domain and its codomain, a function is uniquely represented by the set of all pairs (x, f (x)), called the graph of the function, a popular means of illustrating the function. When the domain and the codomain are sets of real numbers, each such pair may be thought of as the Cartesian coordinates of a point in the plane.
Functions are widely used in science, engineering, and in most fields of mathematics. It has been said that functions are "the central objects of investigation" in most fields of mathematics.
The concept of a function has evolved significantly over centuries, from its informal origins in ancient mathematics to its formalization in the 19th century. See History of the function concept for details.

## Definition

A function f from a set X to a set Y is an assignment of one element of Y to each element of X. The set X is called the domain of the function and the set Y is called the codomain of the function.
If the element y in Y is assigned to x in X by the function f, one says that f maps x to y, and this is commonly written $y=f(x).$ In this notation, x is the argument or variable of the function.
A specific element x of X is a value of the variable, and the corresponding element of Y is the value of the function at x, or the image of x under the function. The image of a function, sometimes called its range, is the set of the images of all elements in the domain.
A function f, its domain X, and its codomain Y are often specified by the notation $f:X\to Y.$ One may write $x\mapsto y$ instead of $y=f(x)$, where the symbol $\mapsto$ (read 'maps to') is used to specify where a particular element x in the domain is mapped to by f. This allows the definition of a function without naming. For example, the square function is the function $x\mapsto x^{2}.$
The domain and codomain are not always explicitly given when a function is defined. In particular, it is common that one might only know, without some (possibly difficult) computation, that the domain of a specific function is contained in a larger set. For example, if $f:\mathbb {R} \to \mathbb {R}$ is a real function, the determination of the domain of the function $x\mapsto 1/f(x)$ requires knowing the zeros of f. This is one of the reasons for which, in mathematical analysis, "a function from X to Y " may refer to a function having a proper subset of X as a domain. For example, a "function from the reals to the reals" may refer to a real-valued function of a real variable whose domain is a proper subset of the real numbers, typically a subset that contains a non-empty open interval. Such a function is then called a partial function.
A function f on a set S means a function from the domain S, without specifying a codomain. However, some authors use it as shorthand for saying that the function is f : S → S.

### Formal definition

The above definition of a function is essentially that of the founders of calculus, Leibniz, Newton and Euler. However, it cannot be formalized, since there is no mathematical definition of an "assignment". It is only at the end of the 19th century that the first formal definition of a function could be provided, in terms of set theory. This set-theoretic definition is based on the fact that a function establishes a relation between the elements of the domain and some (possibly all) elements of the codomain. Mathematically, a binary relation between two sets X and Y is a subset of the set of all ordered pairs $(x,y)$ such that $x\in X$ and $y\in Y.$ The set of all these pairs is called the Cartesian product of X and Y and denoted $X\times Y.$ Thus, the above definition may be formalized as follows.
A function with domain X and codomain Y is a binary relation R between X and Y that satisfies the two following conditions:
* For every $x$ in $X$ there exists $y$ in $Y$ such that $(x,y)\in R.$
* If $(x,y)\in R$ and $(x,z)\in R,$ then $y=z.$
This definition may be rewritten more formally, without referring explicitly to the concept of a relation, but using more notation (including set-builder notation):
A function is formed by three sets, the domain $X,$ the codomain $Y,$ and the graph $R$ that satisfy the three following conditions.
* $R\subseteq \{(x,y)\mid x\in X,y\in Y\}$
* $\forall x\in X,\exists y\in Y,\left(x,y\right)\in R\qquad$
* $(x,y)\in R\land (x,z)\in R\implies y=z\qquad$

### Partial functions

Partial functions are defined similarly to ordinary functions, with the "total" condition removed. That is, a partial function from X to Y is a binary relation R between X and Y such that, for every $x\in X,$ there is at most one y in Y such that $(x,y)\in R.$
Using functional notation, this means that, given $x\in X,$ either $f(x)$ is in Y, or it is undefined.
The set of the elements of X such that $f(x)$ is defined and belongs to Y is called the domain of definition of the function. A partial function from X to Y is thus a ordinary function that has as its domain a subset of X called the domain of definition of the function. If the domain of definition equals X, one often says that the partial function is a total function.
In several areas of mathematics the term "function" refers to partial functions rather than to ordinary functions. This is typically the case when functions may be specified in a way that makes difficult or even impossible to determine their domain.
In calculus, a real-valued function of a real variable or real function is a partial function from the set $\mathbb {R}$ of the real numbers to itself. Given a real function $f:x\mapsto f(x)$ its multiplicative inverse $x\mapsto 1/f(x)$ is also a real function. The determination of the domain of definition of a multiplicative inverse of a (partial) function amounts to compute the zeros of the function, the values where the function is defined but not its multiplicative inverse.
Similarly, a function of a complex variable is generally a partial function with a domain of definition included in the set $\mathbb {C}$ of the complex numbers. The difficulty of determining the domain of definition of a complex function is illustrated by the multiplicative inverse of the Riemann zeta function: the determination of the domain of definition of the function $z\mapsto 1/\zeta (z)$ is more or less equivalent to the proof or disproof of one of the major open problems in mathematics, the Riemann hypothesis.
In computability theory, a general recursive function is a partial function from the integers to the integers whose values can be computed by an algorithm (roughly speaking). The domain of definition of such a function is the set of inputs for which the algorithm does not run forever. A fundamental theorem of computability theory is that there cannot exist an algorithm that takes an arbitrary general recursive function as input and tests whether 0 belongs to its domain of definition (see Halting problem).

### Multivariate functions

A multivariate function, multivariable function, or function of several variables is a function that depends on several arguments. Such functions are commonly encountered. For example, the position of a car on a road is a function of the time travelled and its average speed.
Formally, a function of n variables is a function whose domain is a set of n-tuples. For example, multiplication of integers is a function of two variables, or bivariate function, whose domain is the set of all ordered pairs (2-tuples) of integers, and whose codomain is the set of integers. The same is true for every binary operation. The graph of a bivariate surface over a two-dimensional real domain may be interpreted as defining a parametric surface, as used in, e.g., bivariate interpolation.
Commonly, an n-tuple is denoted enclosed between parentheses, such as in $(1,2,\ldots ,n).$ When using functional notation, one usually omits the parentheses surrounding tuples, writing $f(x_{1},\ldots ,x_{n})$ instead of $f((x_{1},\ldots ,x_{n})).$
Given n sets $X_{1},\ldots ,X_{n},$ the set of all n-tuples $(x_{1},\ldots ,x_{n})$ such that $x_{1}\in X_{1},\ldots ,x_{n}\in X_{n}$ is called the Cartesian product of $X_{1},\ldots ,X_{n},$ and denoted $X_{1}\times \cdots \times X_{n}.$
Therefore, a multivariate function is a function that has a Cartesian product or a proper subset of a Cartesian product as a domain.
$f:U\to Y,$
where the domain U has the form
$U\subseteq X_{1}\times \cdots \times X_{n}.$
If all the $X_{i}$ are equal to the set $\mathbb {R}$ of the real numbers or to the set $\mathbb {C}$ of the complex numbers, one talks respectively of a function of several real variables or of a function of several complex variables.

## Notation

There are various standard ways for denoting functions. The most commonly used notation is functional notation, which is the first notation described below.

### Functional notation

The functional notation requires that a name is given to the function, which, in the case of a unspecified function is often the letter f. Then, the application of the function to an argument is denoted by its name followed by its argument (or, in the case of a multivariate functions, its arguments) enclosed between parentheses, such as in
$f(x),\quad \sin(3),\quad {\text{or}}\quad f(x^{2}+1).$
The argument between the parentheses may be a variable, often x, that represents an arbitrary element of the domain of the function, a specific element of the domain (3 in the above example), or an expression that can be evaluated to an element of the domain ($x^{2}+1$ in the above example). The use of a unspecified variable between parentheses is useful for defining a function explicitly such as in "let $f(x)=\sin(x^{2}+1)$".
When the symbol denoting the function consists of several characters and no ambiguity may arise, the parentheses of functional notation might be omitted. For example, it is common to write sin x instead of sin(x).
Functional notation was first used by Leonhard Euler in 1734. Some widely used functions are represented by a symbol consisting of several letters (usually two or three, generally an abbreviation of their name). In this case, a roman type is customarily used instead, such as "sin" for the sine function, in contrast to italic font for single-letter symbols.
The functional notation is often used colloquially for referring to a function and simultaneously naming its argument, such as in "let $f(x)$ be a function". This is an abuse of notation that is useful for a simpler formulation.

### Arrow notation

Arrow notation defines the rule of a function inline, without requiring a name to be given to the function. It uses the ↦ arrow symbol, pronounced "maps to". For example, $x\mapsto x+1$ is the function which takes a real number as input and outputs that number plus 1. Again, a domain and codomain of $\mathbb {R}$ is implied.
The domain and codomain can also be explicitly stated, for example:
${\begin{aligned}\operatorname {sqr} \colon \mathbb {Z} &\to \mathbb {Z} \\x&\mapsto x^{2}.\end{aligned}}$
This defines a function sqr from the integers to the integers that returns the square of its input.
As a common application of the arrow notation, suppose $f:X\times X\to Y;\;(x,t)\mapsto f(x,t)$ is a function in two variables, and we want to refer to a partially applied function $X\to Y$ produced by fixing the second argument to the value t$_0$ without introducing a new function name. The map in question could be denoted $x\mapsto f(x,t_{0})$ using the arrow notation. The expression $x\mapsto f(x,t_{0})$ (read: "the map taking x to f of x comma t nought") represents this new function with just one argument, whereas the expression f(x$_0$, t$_0$) refers to the value of the function f at the point (x$_0$, t$_0$).

### Index notation

Index notation may be used instead of functional notation. That is, instead of writing f (x), one writes $f_{x}.$
This is typically the case for functions whose domain is the set of the natural numbers. Such a function is called a sequence, and, in this case the element $f_{n}$ is called the nth element of the sequence.
The index notation can also be used for distinguishing some variables called parameters from the "true variables". In fact, parameters are specific variables that are considered as being fixed during the study of a problem. For example, the map $x\mapsto f(x,t)$ (see above) would be denoted $f_{t}$ using index notation, if we define the collection of maps $f_{t}$ by the formula $f_{t}(x)=f(x,t)$ for all $x,t\in X$.

### Dot notation

In the notation $x\mapsto f(x),$ the symbol x does not represent any value; it is simply a placeholder, meaning that, if x is replaced by any value on the left of the arrow, it should be replaced by the same value on the right of the arrow. Therefore, x may be replaced by any symbol, often an interpunct " ⋅ ". This may be useful for distinguishing the function f (⋅) from its value f (x) at x.
For example, $a(\cdot )^{2}$ may stand for the function $x\mapsto ax^{2}$, and ∫$_a$$^{(⋅)}$f(u)du{\textstyle \int _{a}^{\, (\cdot )}f(u)\, du} may stand for a function defined by an integral with variable upper bound: x↦∫$_a$$^x$f(u)du{\textstyle x\mapsto \int _{a}^{x}f(u)\, du}.

### Specialized notations

There are other, specialized notations for functions in sub-disciplines of mathematics. For example, in linear algebra and functional analysis, linear forms and the vectors they act upon are denoted using a dual pair to show the underlying duality. This is similar to the use of bra–ket notation in quantum mechanics. In logic and the theory of computation, the function notation of lambda calculus is used to explicitly express the basic notions of function abstraction and application. In category theory and homological algebra, networks of functions are described in terms of how they and their compositions commute with each other using commutative diagrams that extend and generalize the arrow notation for functions described above.

### Functions of more than one variable

In some cases the argument of a function may be an ordered pair of elements taken from some set or sets. For example, a function f can be defined as mapping any pair of real numbers $(x,y)$ to the sum of their squares, $x^{2}+y^{2}$. Such a function is commonly written as $f(x,y)=x^{2}+y^{2}$ and referred to as "a function of two variables". Likewise one can have a function of three or more variables, with notations such as $f(w,x,y)$, $f(w,x,y,z)$.

## Other terms

| Term | Distinction from "function" |
| Map/Mapping | None; the terms are synonymous. |
| Map/Mapping | A map can have any set as its codomain, while, in some contexts, typically in older books, the codomain of a function is specifically the set of real or complex numbers. |
| Map/Mapping | Alternatively, a map is associated with a special structure (e.g. by explicitly specifying a structured codomain in its definition). For example, a linear map. |
| Homomorphism | A function between two structures of the same type that preserves the operations of the structure (e.g. a group homomorphism). |
| Morphism | A generalisation of homomorphisms to any category, even when the objects of the category are not sets (for example, a group defines a category with only one object, which has the elements of the group as morphisms; see Category (mathematics) § Examples for this example and other similar ones). |
A function may also be called a map or a mapping, but some authors make a distinction between the term "map" and "function". For example, the term "map" is often reserved for a "function" with some sort of special structure (e.g. maps of manifolds). In particular map may be used in place of homomorphism for the sake of succinctness (e.g., linear map or map from G to H instead of group homomorphism from G to H). Some authors reserve the word mapping for the case where the structure of the codomain belongs explicitly to the definition of the function.
Some authors, such as Serge Lang, use "function" only to refer to maps for which the codomain is a subset of the real or complex numbers, and use the term mapping for more general functions.
In the theory of dynamical systems, a map denotes an evolution function used to create discrete dynamical systems. See also Poincaré map.
Whichever definition of map is used, related terms like domain, codomain, injective, continuous have the same meaning as for a function.

## Specifying a function

Given a function $f$, by definition, to each element $x$ of the domain of the function $f$, there is a unique element associated to it, the value $f(x)$ of $f$ at $x$. There are several ways to specify or describe how $x$ is related to $f(x)$, both explicitly and implicitly. Sometimes, a theorem or an axiom asserts the existence of a function having some properties, without describing it more precisely. Often, the specification or description is referred to as the definition of the function $f$.

### By listing function values

On a finite set a function may be defined by listing the elements of the codomain that are associated to the elements of the domain. For example, if $A=\{1,2,3\}$, then one can define a function $f:A\to \mathbb {R}$ by $f(1)=2,f(2)=3,f(3)=4.$

### By a formula

Functions are often defined by an expression that describes a combination of arithmetic operations and previously defined functions; such a formula allows computing the value of the function from the value of any element of the domain. For example, in the above example, $f$ can be defined by the formula $f(n)=n+1$, for $n\in \{1,2,3\}$.
When a function is defined this way, the determination of its domain is sometimes difficult. If the formula that defines the function contains divisions, the values of the variable for which a denominator is zero must be excluded from the domain; thus, for a complicated function, the determination of the domain passes through the computation of the zeros of auxiliary functions. Similarly, if square roots occur in the definition of a function from $\mathbb {R}$ to $\mathbb {R} ,$ the domain is included in the set of the values of the variable for which the arguments of the square roots are nonnegative.
For example, $f(x)={\sqrt {1+x^{2}}}$ defines a function $f:\mathbb {R} \to \mathbb {R}$ whose domain is $\mathbb {R} ,$ because $1+x^{2}$ is always positive if x is a real number. On the other hand, $f(x)={\sqrt {1-x^{2}}}$ defines a function from the reals to the reals whose domain is reduced to the interval [−1, 1]. (In old texts, such a domain was called the domain of definition of the function.)
Functions can be classified by the nature of formulas that define them:
* A quadratic function is a function that may be written $f(x)=ax^{2}+bx+c,$ where a, b, c are constants.
* More generally, a polynomial function is a function that can be defined by a formula involving only additions, subtractions, multiplications, and exponentiation to nonnegative integer powers. For example, $f(x)=x^{3}-3x-1$ and $f(x)=(x-1)(x^{3}+1)+2x^{2}-1$ are polynomial functions of $x$.
* A rational function is the same, with divisions also allowed, such as $f(x)={\frac {x-1}{x+1}},$ and $f(x)={\frac {1}{x+1}}+{\frac {3}{x}}-{\frac {2}{x-1}}.$
* An algebraic function is the same, with nth roots and roots of polynomials also allowed.
* An elementary function is the same, with logarithms and exponential functions allowed.

### Inverse and implicit functions

A function $f:X\to Y,$ with domain X and codomain Y, is bijective, if for every y in Y, there is one and only one element x in X such that y = f(x). In this case, the inverse function of f is the function $f^{-1}:Y\to X$ that maps $y\in Y$ to the element $x\in X$ such that y = f(x). For example, the natural logarithm is a bijective function from the positive real numbers to the real numbers. It thus has an inverse, called the exponential function, that maps the real numbers onto the positive numbers.
If a function $f:X\to Y$ is not bijective, it may occur that one can select subsets $E\subseteq X$ and $F\subseteq Y$ such that the restriction of f to E is a bijection from E to F, and has thus an inverse. The inverse trigonometric functions are defined this way. For example, the cosine function induces, by restriction, a bijection from the interval [0, π] onto the interval [−1, 1], and its inverse function, called arccosine, maps [−1, 1] onto [0, π]. The other inverse trigonometric functions are defined similarly.
More generally, given a binary relation R between two sets X and Y, let E be a subset of X such that, for every $x\in E,$ there is some $y\in Y$ such that x R y. If one has a criterion allowing selecting such a y for every $x\in E,$ this defines a function $f:E\to Y,$ called an implicit function, because it is implicitly defined by the relation R.
For example, the equation of the unit circle $x^{2}+y^{2}=1$ defines a relation on real numbers. If −1 < x < 1 there are two possible values of y, one positive and one negative. For x = ± 1, these two values become both equal to 0. Otherwise, there is no possible value of y. This means that the equation defines two implicit functions with domain [−1, 1] and respective codomains [0, +∞) and (−∞, 0].
In this example, the equation can be solved in y, giving $y=\pm {\sqrt {1-x^{2}}},$ but, in more complicated examples, this is impossible. For example, the relation $y^{5}+y+x=0$ defines y as an implicit function of x, called the Bring radical, which has $\mathbb {R}$ as domain and range. The Bring radical cannot be expressed in terms of the four arithmetic operations and nth roots.
The implicit function theorem provides mild differentiability conditions for existence and uniqueness of an implicit function in the neighborhood of a point.

### Using differential calculus

Many functions can be defined as the antiderivative of another function. This is the case of the natural logarithm, which is the antiderivative of 1/x that is 0 for x = 1. Another common example is the error function.
More generally, many functions, including most special functions, can be defined as solutions of differential equations. The simplest example is probably the exponential function, which can be defined as the unique function that is equal to its derivative and takes the value 1 for x = 0.
Power series can be used to define functions on the domain in which they converge. For example, the exponential function is given by e$^x$=∑$_{n=0}$$^∞$ x$^n$/n! {\textstyle e^{x}=\sum _{n=0}^{\infty }{x^{n} \over n!}}. However, as the coefficients of a series are quite arbitrary, a function that is the sum of a convergent series is generally defined otherwise, and the sequence of the coefficients is the result of some computation based on another definition. Then, the power series can be used to enlarge the domain of the function. Typically, if a function for a real variable is the sum of its Taylor series in some interval, this power series allows immediately enlarging the domain to a subset of the complex numbers, the disc of convergence of the series. Then analytic continuation allows enlarging further the domain for including almost the whole complex plane. This process is the method that is generally used for defining the logarithm, the exponential and the trigonometric functions of a complex number.

### By recurrence

Functions whose domain are the nonnegative integers, known as sequences, are sometimes defined by recurrence relations.
The factorial function on the nonnegative integers ($n\mapsto n!$) is a basic example, as it can be defined by the recurrence relation
$n!=n(n-1)!\quad {\text{for}}\quad n>0,$
and the initial condition
$0!=1.$

## Representing a function

A graph is commonly used to give an intuitive picture of a function. As an example of how a graph helps to understand a function, it is easy to see from its graph whether a function is increasing or decreasing. Some functions may also be represented by bar charts.

### Graphs and plots

Given a function $f:X\to Y,$ its graph is, formally, the set
$G=\{(x,f(x))\mid x\in X\}.$
In the frequent case where X and Y are subsets of the real numbers (or may be identified with such subsets, e.g. intervals), an element $(x,y)\in G$ may be identified with a point having coordinates x, y in a 2-dimensional coordinate system, e.g. the Cartesian plane. Parts of this may create a plot that represents (parts of) the function. The use of plots is so ubiquitous that they too are called the graph of the function. Graphic representations of functions are also possible in other coordinate systems. For example, the graph of the square function
$x\mapsto x^{2},$
consisting of all points with coordinates $(x,x^{2})$ for $x\in \mathbb {R} ,$ yields, when depicted in Cartesian coordinates, the well known parabola. If the same quadratic function $x\mapsto x^{2},$ with the same formal graph, consisting of pairs of numbers, is plotted instead in polar coordinates $(r,\theta )=(x,x^{2}),$ the plot obtained is Fermat's spiral.

### Tables

A function can be represented as a table of values. If the domain of a function is finite, then the function can be completely specified in this way. For example, the multiplication function $f:\{1,\ldots ,5\}^{2}\to \mathbb {R}$ defined as $f(x,y)=xy$ can be represented by the familiar multiplication table
| yx | 1 | 2 | 3 | 4 | 5 |
| 1 | 1 | 2 | 3 | 4 | 5 |
| 2 | 2 | 4 | 6 | 8 | 10 |
| 3 | 3 | 6 | 9 | 12 | 15 |
| 4 | 4 | 8 | 12 | 16 | 20 |
| 5 | 5 | 10 | 15 | 20 | 25 |
On the other hand, if a function's domain is continuous, a table can give the values of the function at specific values of the domain. If an intermediate value is needed, interpolation can be used to estimate the value of the function. For example, a portion of a table for the sine function might be given as follows, with values rounded to 6 decimal places:
| x | sin x |
| 1.289 | 0.960557 |
| 1.290 | 0.960835 |
| 1.291 | 0.961112 |
| 1.292 | 0.961387 |
| 1.293 | 0.961662 |
Before the advent of handheld calculators and personal computers, such tables were often compiled and published for functions such as logarithms and trigonometric functions.

### Bar chart

A bar chart can represent a function whose domain is a finite set, the natural numbers, or the integers. In this case, an element x of the domain is represented by an interval of the x-axis, and the corresponding value of the function, f(x), is represented by a rectangle whose base is the interval corresponding to x and whose height is f(x) (possibly negative, in which case the bar extends below the x-axis).

## General properties

This section describes general properties of functions, that are independent of specific properties of the domain and the codomain.

### Standard functions

There are a number of standard functions that occur frequently:
* For every set X, there is a unique function, called the empty function, or empty map, from the empty set to X. The graph of an empty function is the empty set. The existence of empty functions is needed both for the coherency of the theory and for avoiding exceptions concerning the empty set in many statements. Under the usual set-theoretic definition of a function as an ordered triplet (or equivalent ones), there is exactly one empty function for each set, thus the empty function $\varnothing \to X$ is not equal to $\varnothing \to Y$ if and only if $X\neq Y$, although their graphs are both the empty set.
* For every set X and every singleton set {s}, there is a unique function from X to {s}, which maps every element of X to s. This is a surjection (see below) unless X is the empty set.
* Given a function $f:X\to Y,$ the canonical surjection of f onto its image $f(X)=\{f(x)\mid x\in X\}$ is the function from X to f(X) that maps x to f(x).
* For every subset A of a set X, the inclusion map of A into X is the injective (see below) function that maps every element of A to itself.
* The identity function on a set X, often denoted by id$_X$, is the inclusion of X into itself.

### Function composition

Given two functions $f:X\to Y$ and $g:Y\to Z$ such that the domain of g is the codomain of f, their composition is the function $g\circ f:X\rightarrow Z$ defined by
$(g\circ f)(x)=g(f(x)).$
That is, the value of $g\circ f$ is obtained by first applying f to x to obtain y = f(x) and then applying g to the result y to obtain g(y) = g(f(x)). In this notation, the function that is applied first is always written on the right.
The composition $g\circ f$ is an operation on functions that is defined only if the codomain of the first function is the domain of the second one. Even when both $g\circ f$ and $f\circ g$ satisfy these conditions, the composition is not necessarily commutative, that is, the functions $g\circ f$ and $f\circ g$ need not be equal, but may deliver different values for the same argument. For example, let f(x) = x$^2$ and g(x) = x + 1, then $g(f(x))=x^{2}+1$ and $f(g(x))=(x+1)^{2}$ agree just for $x=0.$
The function composition is associative in the sense that, if one of $(h\circ g)\circ f$ and $h\circ (g\circ f)$ is defined, then the other is also defined, and they are equal, that is, $(h\circ g)\circ f=h\circ (g\circ f).$ Therefore, it is usual to just write $h\circ g\circ f.$
The identity functions $\operatorname {id} _{X}$ and $\operatorname {id} _{Y}$ are respectively a right identity and a left identity for functions from X to Y. That is, if f is a function with domain X, and codomain Y, one has $f\circ \operatorname {id} _{X}=\operatorname {id} _{Y}\circ f=f.$
* A composite function g(f(x)) can be visualized as the combination of two "machines".
* A simple example of a function composition
* Another composition. In this example, (g ∘ f )(c) = #.

### Image and preimage

Let $f:X\to Y.$ The image under f of an element x of the domain X is f(x). If A is any subset of X, then the image of A under f, denoted f(A), is the subset of the codomain Y consisting of all images of elements of A, that is,
$f(A)=\{f(x)\mid x\in A\}.$
The image of f is the image of the whole domain, that is, f(X). It is also called the range of f, although the term range may also refer to the codomain.
On the other hand, the inverse image or preimage under f of an element y of the codomain Y is the set of all elements of the domain X whose images under f equal y. In symbols, the preimage of y is denoted by $f^{-1}(y)$ and is given by the equation
$f^{-1}(y)=\{x\in X\mid f(x)=y\}.$
Likewise, the preimage of a subset B of the codomain Y is the set of the preimages of the elements of B, that is, it is the subset of the domain X consisting of all elements of X whose images belong to B. It is denoted by $f^{-1}(B)$ and is given by the equation
$f^{-1}(B)=\{x\in X\mid f(x)\in B\}.$
For example, the preimage of $\{4,9\}$ under the square function is the set $\{-3,-2,2,3\}$.
By definition of a function, the image of an element x of the domain is always a single element of the codomain. However, the preimage $f^{-1}(y)$ of an element y of the codomain may be empty or contain any number of elements. For example, if f is the function from the integers to themselves that maps every integer to 0, then $f^{-1}(0)=\mathbb {Z}$.
If $f:X\to Y$ is a function, A and B are subsets of X, and C and D are subsets of Y, then one has the following properties:
* $A\subseteq B\Longrightarrow f(A)\subseteq f(B)$
* $C\subseteq D\Longrightarrow f^{-1}(C)\subseteq f^{-1}(D)$
* $A\subseteq f^{-1}(f(A))$
* $C\supseteq f(f^{-1}(C))$
* $f(f^{-1}(f(A)))=f(A)$
* $f^{-1}(f(f^{-1}(C)))=f^{-1}(C)$
The preimage by f of an element y of the codomain is sometimes called, in some contexts, the fiber of y under f.
If a function f has an inverse (see below), this inverse is denoted $f^{-1}.$ In this case $f^{-1}(C)$ may denote either the image by $f^{-1}$ or the preimage by f of C. This is not a problem, as these sets are equal. The notation $f(A)$ and $f^{-1}(C)$ may be ambiguous in the case of sets that contain some subsets as elements, such as $\{x,\{x\}\}.$ In this case, some care may be needed, for example, by using square brackets $f[A],f^{-1}[C]$ for images and preimages of subsets and ordinary parentheses for images and preimages of elements.

### Injective, surjective and bijective functions

Let $f:X\to Y$ be a function.
The function f is injective (or one-to-one, or is an injection) if f(a) ≠ f(b) for every two different elements a and b of X. Equivalently, f is injective if and only if, for every $y\in Y,$ the preimage $f^{-1}(y)$ contains at most one element. An empty function is always injective. If X is not the empty set, then f is injective if and only if there exists a function $g:Y\to X$ such that $g\circ f=\operatorname {id} _{X},$ that is, if f has a left inverse. Proof: If f is injective, for defining g, one chooses an element $x_{0}$ in X (which exists as X is supposed to be nonempty), and one defines g by $g(y)=x$ if $y=f(x)$ and $g(y)=x_{0}$ if $y\not \in f(X).$ Conversely, if $g\circ f=\operatorname {id} _{X},$ and $y=f(x),$ then $x=g(y),$ and thus $f^{-1}(y)=\{x\}.$
The function f is surjective (or onto, or is a surjection) if its range $f(X)$ equals its codomain $Y$, that is, if, for each element $y$ of the codomain, there exists some element $x$ of the domain such that $f(x)=y$ (in other words, the preimage $f^{-1}(y)$ of every $y\in Y$ is nonempty). If, as usual in modern mathematics, the axiom of choice is assumed, then f is surjective if and only if there exists a function $g:Y\to X$ such that $f\circ g=\operatorname {id} _{Y},$ that is, if f has a right inverse. The axiom of choice is needed, because, if f is surjective, one defines g by $g(y)=x,$ where $x$ is an arbitrarily chosen element of $f^{-1}(y).$
The function f is bijective (or is a bijection or a one-to-one correspondence) if it is both injective and surjective. That is, f is bijective if, for every $y\in Y,$ the preimage $f^{-1}(y)$ contains exactly one element. The function f is bijective if and only if it admits an inverse function, that is, a function $g:Y\to X$ such that $g\circ f=\operatorname {id} _{X}$ and $f\circ g=\operatorname {id} _{Y}.$ (Contrarily to the case of surjections, this does not require the axiom of choice; the proof is straightforward).
Every function $f:X\to Y$ may be factorized as the composition $i\circ s$ of a surjection followed by an injection, where s is the canonical surjection of X onto f(X) and i is the canonical injection of f(X) into Y. This is the canonical factorization of f.
"One-to-one" and "onto" are terms that were more common in the older English language literature; "injective", "surjective", and "bijective" were originally coined as French words in the second quarter of the 20th century by the Bourbaki group and imported into English. As a word of caution, "a one-to-one function" is one that is injective, while a "one-to-one correspondence" refers to a bijective function. Also, the statement "f maps X onto Y" differs from "f maps X into B", in that the former implies that f is surjective, while the latter makes no assertion about the nature of f. In a complicated reasoning, the one letter difference can easily be missed. Due to the confusing nature of this older terminology, these terms have declined in popularity relative to the Bourbakian terms, which have also the advantage of being more symmetrical.

### Restriction and extension

If $f:X\to Y$ is a function and S is a subset of X, then the restriction of $f$ to S, denoted $f|_{S}$, is the function from S to Y defined by
$f|_{S}(x)=f(x)$
for all x in S. Restrictions can be used to define partial inverse functions: if there is a subset S of the domain of a function $f$ such that $f|_{S}$ is injective, then the canonical surjection of $f|_{S}$ onto its image $f|_{S}(S)=f(S)$ is a bijection, and thus has an inverse function from $f(S)$ to S. One application is the definition of inverse trigonometric functions. For example, the cosine function is injective when restricted to the interval [0, π]. The image of this restriction is the interval [−1, 1], and thus the restriction has an inverse function from [−1, 1] to [0, π], which is called arccosine and is denoted arccos.
Function restriction may also be used for "gluing" functions together. Let X=⋃$_{i∈I}$U$_i${\textstyle X=\bigcup _{i\in I}U_{i}} be the decomposition of X as a union of subsets, and suppose that a function $f_{i}:U_{i}\to Y$ is defined on each $U_{i}$ such that for each pair $i,j$ of indices, the restrictions of $f_{i}$ and $f_{j}$ to $U_{i}\cap U_{j}$ are equal. Then this defines a unique function $f:X\to Y$ such that $f|_{U_{i}}=f_{i}$ for all i. This is the way that functions on manifolds are defined.
An extension of a function f is a function g such that f is a restriction of g. A typical use of this concept is the process of analytic continuation, that allows extending functions whose domain is a small part of the complex plane to functions whose domain is almost the whole complex plane.
Here is another classical example of a function extension that is encountered when studying homographies of the real line. A homography is a function $h(x)={\frac {ax+b}{cx+d}}$ such that ad − bc ≠ 0. Its domain is the set of all real numbers different from $-d/c,$ and its image is the set of all real numbers different from $a/c.$ If one extends the real line to the projectively extended real line by including ∞, one may extend h to a bijection from the extended real line to itself by setting $h(\infty )=a/c$ and $h(-d/c)=\infty$.

## In calculus

The idea of function, starting in the 17th century, was fundamental to the new infinitesimal calculus. At that time, only real-valued functions of a real variable were considered, and all functions were assumed to be smooth. But the definition was soon extended to functions of several variables and to functions of a complex variable. In the second half of the 19th century, the mathematically rigorous definition of a function was introduced, and functions with arbitrary domains and codomains were defined.
Functions are now used throughout all areas of mathematics. In introductory calculus, when the word function is used without qualification, it means a real-valued function of a single real variable. The more general definition of a function is usually introduced to second or third year college students with STEM majors, and in their senior year they are introduced to calculus in a larger, more rigorous setting in courses such as real analysis and complex analysis.

### Real function

A real function is a real-valued function of a real variable, that is, a function whose codomain is the field of real numbers and whose domain is a set of real numbers that contains an interval. In this section, these functions are simply called functions.
The functions that are most commonly considered in mathematics and its applications have some regularity, that is they are continuous, differentiable, and even analytic. This regularity insures that these functions can be visualized by their graphs. In this section, all functions are differentiable in some interval.
Functions enjoy pointwise operations, that is, if f and g are functions, their sum, difference and product are functions defined by
${\begin{aligned}(f+g)(x)&=f(x)+g(x)\\(f-g)(x)&=f(x)-g(x)\\(f\cdot g)(x)&=f(x)\cdot g(x)\\\end{aligned}}.$
The domains of the resulting functions are the intersection of the domains of f and g. The quotient of two functions is defined similarly by
${\frac {f}{g}}(x)={\frac {f(x)}{g(x)}},$
but the domain of the resulting function is obtained by removing the zeros of g from the intersection of the domains of f and g.
The polynomial functions are defined by polynomials, and their domain is the whole set of real numbers. They include constant functions, linear functions and quadratic functions. Rational functions are quotients of two polynomial functions, and their domain is the real numbers with a finite number of them removed to avoid division by zero. The simplest rational function is the function $x\mapsto {\frac {1}{x}},$ whose graph is a hyperbola, and whose domain is the whole real line except for 0.
The derivative of a real differentiable function is a real function. An antiderivative of a continuous real function is a real function that has the original function as a derivative. For example, the function x↦ 1/x {\textstyle x\mapsto {\frac {1}{x}}} is continuous, and even differentiable, on the positive real numbers. Thus one antiderivative, which takes the value zero for x = 1, is a differentiable function called the natural logarithm.
A real function f is monotonic in an interval if the sign of ${\frac {f(x)-f(y)}{x-y}}$ does not depend of the choice of x and y in the interval. If the function is differentiable in the interval, it is monotonic if the sign of the derivative is constant in the interval. If a real function f is monotonic in an interval I, it has an inverse function, which is a real function with domain f(I) and image I. This is how inverse trigonometric functions are defined in terms of trigonometric functions, where the trigonometric functions are monotonic. Another example: the natural logarithm is monotonic on the positive real numbers, and its image is the whole real line; therefore it has an inverse function that is a bijection between the real numbers and the positive real numbers. This inverse is the exponential function.
Many other real functions are defined either by the implicit function theorem (the inverse function is a particular instance) or as solutions of differential equations. For example, the sine and the cosine functions are the solutions of the linear differential equation
$y''+y=0$
such that
$\sin 0=0,\quad \cos 0=1,\quad {\frac {\partial \sin x}{\partial x}}(0)=1,\quad {\frac {\partial \cos x}{\partial x}}(0)=0.$

### Vector-valued function

When the elements of the codomain of a function are vectors, the function is said to be a vector-valued function. These functions are particularly useful in applications, for example modeling physical properties. For example, the function that associates to each point of a fluid its velocity vector is a vector-valued function.
Some vector-valued functions are defined on a subset of $\mathbb {R} ^{n}$ or other spaces that share geometric or topological properties of $\mathbb {R} ^{n}$, such as manifolds. These vector-valued functions are given the name vector fields.

## Function space

In mathematical analysis, and more specifically in functional analysis, a function space is a set of scalar-valued or vector-valued functions, which share a specific property and form a topological vector space. For example, the real smooth functions with a compact support (that is, they are zero outside some compact set) form a function space that is at the basis of the theory of distributions.
Function spaces play a fundamental role in advanced mathematical analysis, by allowing the use of their algebraic and topological properties for studying properties of functions. For example, all theorems of existence and uniqueness of solutions of ordinary or partial differential equations result of the study of function spaces.

## Multi-valued functions

Several methods for specifying functions of real or complex variables start from a local definition of the function at a point or on a neighbourhood of a point, and then extend by continuity the function to a much larger domain. Frequently, for a starting point $x_{0},$ there are several possible starting values for the function.
For example, in defining the square root as the inverse function of the square function, for any positive real number $x_{0},$ there are two choices for the value of the square root, one of which is positive and denoted ${\sqrt {x_{0}}},$ and another which is negative and denoted $-{\sqrt {x_{0}}}.$ These choices define two continuous functions, both having the nonnegative real numbers as a domain, and having either the nonnegative or the nonpositive real numbers as images. When looking at the graphs of these functions, one can see that, together, they form a single smooth curve. It is therefore often useful to consider these two square root functions as a single function that has two values for positive x, one value for 0 and no value for negative x.
In the preceding example, one choice, the positive square root, is more natural than the other. This is not the case in general. For example, let consider the implicit function that maps y to a root x of $x^{3}-3x-y=0$ (see the figure on the right). For y = 0 one may choose either $0,{\sqrt {3}},{\text{ or }}-{\sqrt {3}}$ for x. By the implicit function theorem, each choice defines a function; for the first one, the (maximal) domain is the interval [−2, 2] and the image is [−1, 1]; for the second one, the domain is [−2, ∞) and the image is [1, ∞); for the last one, the domain is (−∞, 2] and the image is (−∞, −1]. As the three graphs together form a smooth curve, and there is no reason for preferring one choice, these three functions are often considered as a single multi-valued function of y that has three values for −2 < y < 2, and only one value for y ≤ −2 and y ≥ −2.
Usefulness of the concept of multi-valued functions is clearer when considering complex functions, typically analytic functions. The domain to which a complex function may be extended by analytic continuation generally consists of almost the whole complex plane. However, when extending the domain through two different paths, one often gets different values. For example, when extending the domain of the square root function, along a path of complex numbers with positive imaginary parts, one gets i for the square root of −1; while, when extending through complex numbers with negative imaginary parts, one gets −i. There are generally two ways of solving the problem. One may define a function that is not continuous along some curve, called a branch cut. Such a function is called the principal value of the function. The other way is to consider that one has a multi-valued function, which is analytic everywhere except for isolated singularities, but whose value may "jump" if one follows a closed loop around a singularity. This jump is called the monodromy.

## In the foundations of mathematics

The definition of a function that is given in this article requires the concept of set, since the domain and the codomain of a function must be a set. This is not a problem in usual mathematics, as it is generally not difficult to consider only functions whose domain and codomain are sets, which are well defined, even if the domain is not explicitly defined. However, it is sometimes useful to consider more general functions.
For example, the singleton set may be considered as a function $x\mapsto \{x\}.$ Its domain would include all sets, and therefore would not be a set. In usual mathematics, one avoids this kind of problem by specifying a domain, which means that one has many singleton functions. However, when establishing foundations of mathematics, one may have to use functions whose domain, codomain or both are not specified, and some authors, often logicians, give precise definitions for these weakly specified functions.
These generalized functions may be critical in the development of a formalization of the foundations of mathematics. For example, Von Neumann–Bernays–Gödel set theory, is an extension of the set theory in which the collection of all sets is a class. This theory includes the replacement axiom, which may be stated as: If X is a set and F is a function, then F[X] is a set.
In alternative formulations of the foundations of mathematics using type theory rather than set theory, functions are taken as primitive notions rather than defined from other kinds of object. They are the inhabitants of function types, and may be constructed using expressions in the lambda calculus.

## In computer science

In computer programming, a function is, in general, a subroutine which implements the abstract concept of function. That is, it is a program unit that produces an output for each input. Functional programming is the programming paradigm consisting of building programs by using only subroutines that behave like mathematical functions, meaning that they have no side effects and depend only on their arguments: they are referentially transparent. For example, if_then_else is a function that takes three (nullary) functions as arguments, and, depending on the value of the first argument (true or false), returns the value of either the second or the third argument. An important advantage of functional programming is that it makes easier program proofs, as being based on a well founded theory, the lambda calculus (see below). However, side effects are generally necessary for practical programs, ones that perform input/output. There is a class of purely functional languages, such as Haskell, which encapsulate the possibility of side effects in the type of a function. Others, such as the ML family, simply allow side effects.
In many programming languages, every subroutine is called a function, even when there is no output but only side effects, and when the functionality consists simply of modifying some data in the computer memory.
Outside the context of programming languages, "function" has the usual mathematical meaning in computer science. In this area, a property of major interest is the computability of a function. For giving a precise meaning to this concept, and to the related concept of algorithm, several models of computation have been introduced, the old ones being general recursive functions, lambda calculus, and Turing machine. The fundamental theorem of computability theory is that these three models of computation define the same set of computable functions, and that all the other models of computation that have ever been proposed define the same set of computable functions or a smaller one. The Church–Turing thesis is the claim that every philosophically acceptable definition of a computable function defines also the same functions.
General recursive functions are partial functions from integers to integers that can be defined from
* constant functions,
* successor, and
* projection functions
via the operators
* composition,
* primitive recursion, and
* minimization.
Although defined only for functions from integers to integers, they can model any computable function as a consequence of the following properties:
* a computation is the manipulation of finite sequences of symbols (digits of numbers, formulas, etc.),
* every sequence of symbols may be coded as a sequence of bits,
* a bit sequence can be interpreted as the binary representation of an integer.
Lambda calculus is a theory that defines computable functions without using set theory, and is the theoretical background of functional programming. It consists of terms that are either variables, function definitions (𝜆-terms), or applications of functions to terms. Terms are manipulated by interpreting its axioms (the α-equivalence, the β-reduction, and the η-conversion) as rewriting rules, which can be used for computation.
In its original form, lambda calculus does not include the concepts of domain and codomain of a function. Roughly speaking, they have been introduced in the theory under the name of type in typed lambda calculus. Most kinds of typed lambda calculi can define fewer functions than untyped lambda calculus.