Traditionally, functions were specified as explicit rules or formulas that converted some input value (or values) into an output value. If f is the name for the function and x is a name for an input value, then f(x) stands for the output value corresponding to x under the rule f. An input value is also called an argument of the function.
For example, the circumference C of a circle depends on its diameter d according to the formula C = Pi * d; therefore, one could say that the circumference is a function of the diameter, and the functional relationship is given by C(d) = Pi * d. One may also consider functions that expect several input values; for instance the volume V of a cone can be computed fromthe radius r of its base and its height h according to the rule V(r, h) = 1/3 * Pi * r^{2} * h.
In modern mathematics, the insistance on specifying an explicit effective rule has been abandoned; all that is required is that a function f associate with every element of some set X a unique element of some set Y. The set X of all admissable arguments is called the domain of f; the set Y is called the codomain of f and we write f : X -> Y.
Formally, a function then from a set X to a set Y is a subset f of the cartesian product X × Y which satisfies the following two properties:
We write "f(x) = y" instead of "(x, y) ∈ f".
A partial function is a subset of the cartesian product which only satisfies the first of the two properties. In other words, a partial function need not assign a value to all elements of its domain.
The subset of Y that consists of all elements in Y that are associated with at least one element in X is called the range or image of the function.
If the domain of a function is a subset of the Cartesian product of n sets then the function is called an n-ary function. For example, the relation dist has the domain R × R and is therefore a binary function. In that case dist((x, y)) is simply written as dist(x, y).
If f : X -> Y is a function and A is a subset of X, then the image of A is the subset of Y defined by
Functions can be specified in several ways. Most functions that relate (combinations of) numbers with numbers are specified by one or more equation. For example, the dist function can be specified with
Another way of specifying functions is by specifying the the binary relation (also called the extension of the function) by either enumerating it or specifying it in set theory. The wght function, for example, might be specified by
A third way of specifying functions that is often used in computer science is specification by computation where it is indicated how the result of the function is computed from the arguments. An example of this are Lambda expressions in Lambda calculus where the function max(a, b) with a and b integers might be specified as
The functions f : X -> Y and g : Y -> Z can be composed by first applying f to an argument x and then applying g to the result. Thus one obtains a function g o f : X -> Z defined by (g o f)(x) = g(f(x)) for all x in X.
As an example, suppose that a plane's height at time t is given by the function h(t) and that the oxygen concentration at height x is given by the function c(x). Then (c o h)(t) describes the oxygen concentration around the plane at time t.
If f : X -> R and g : X -> R are functions with common domain X and codomain the real numbers R, then one can define the sum function f + g : X -> R and the product function f * g : X -> R as follows: (f + g)(x) = f(x) + g(x) and (f * g)(x) = f(x) * g(x) for all x in X. This turns the set of all such functions into a commutative ring.
In programming, a function is a part of a program that can be called with certain arguments (or parameters) from inside the program or elsewhere, and returns a value. Although the term is related to the mathematical term it is less strict; a function may return different results each time, even if it is called with the same arguments, and a function may have side effects, that is, it may cause changes that remain after the call of the function has ended. An exception to this are functions in pure functional programming that return always the same result if called with the same arguments and have no side effects.
A procedure? is similar to a function but has only side effects and does not return a value. In some programming languages such as [ANSI C]? where there are only functions there is often a type such as void
for the result of a function that has no result.