TL;DR
A function takes each element of one set, called the domain, and assigns to it exactly one element of another set, called the codomain. The rule has two parts: every input gets an output, and no input gets two outputs. The set of outputs that actually occur is the range.
Functions describe how one quantity depends on another, such as the area of a square as a function of its side length. They are the basic object behind formulas, probability distributions and programs.
Where you'll see it: in every formula you have ever written as , in a probability density, and in a database lookup that maps a key to a value.
Intuition
A machine with one output per input
Picture a vending machine. You press B3 and you get one specific snack. Pressing B3 twice gives you the same snack, not a different one, and every button leads to something. A function is the same kind of rule: you feed it an input, and it returns a single output, the same way every time.
The two conditions matter. If one input could produce two outputs, you would not know what to expect, and a formula would fail to give an answer. If some input produced nothing, the rule would be incomplete. The function definition is exactly the statement that neither of those problems can happen.
Why does the idea exist? Because we constantly want to describe a dependence without listing every case. The formula describes the area for every side length at once. The function is the compact way to say "this value is determined by that one".
- f(x) = x²
The math
A function from a set to a set , written , is a rule that assigns to each element exactly one element . The set is the domain, the set is the codomain, and the set of values actually produced,
is the image or range. The range is a subset of the codomain, and it need not equal it.
The symbol means "is an element of" and means "is a subset of". Inside the definition, stands for any input and for its output.
The graph
The graph of is the set of pairs . On the plane, a set of pairs describes a function exactly when every vertical line hits the graph at most once. That is the vertical line test, and it is the same statement as "one output per input" drawn on a picture.
Three kinds of function
A function is:
- injective (one-to-one) if different inputs always give different outputs: implies ;
- surjective (onto) if every element of the codomain is hit: for every there is an with ;
- bijective if it is both.
A bijection has an inverse function , defined by when . Without both properties, no such inverse can be defined, because either an output would point back to two inputs, or some element of would point back to nothing.
Composition
If and , the composition is defined by . You apply first and then . In general , which Worked example 3 demonstrates.
Worked examples
Let and . Define to be the remainder when is divided by 2.
- Compute each output: , , , .
- Every input has exactly one output, so is a function from to .
- The range is , which equals , so is surjective.
- with , so is not injective.
Interpretation: sorts the four numbers into two groups, odd and even. Many inputs share an output, and that is typical for a function that summarises data.
Let from the real numbers to the real numbers.
- Solve for : subtract 3 to get , then divide by 2 to get .
- So the inverse is . Each real gives exactly one real , so is a bijection.
- Check with a number: . Then , which returns the original input.
- Check the other direction: .
Interpretation: a bijection can be undone. Converting a temperature from Celsius to Fahrenheit and back is the same kind of pair.
Let from the real numbers to the real numbers, and let .
- and . Two different inputs share an output, so is not injective.
- Its range is , the non-negative numbers, which is not all of the real numbers, so is not surjective either.
- Restrict both domain and codomain to . Now is injective and surjective, so it has an inverse, .
- Compose: , while .
Interpretation: the two composites differ, so the order of operations matters. The restriction in step 3 is why the square root is well defined: it returns the non-negative root only, which is a choice that the restricted domain forces.
Tradeoffs and failure modes
Functions are the right model when each input should have one answer, and most of the time they are. They fail in a few predictable ways.
| Situation | Problem | Better choice |
|---|---|---|
| A quantity has two possible values for one input, like | A single-valued function cannot describe both | Choose a branch explicitly, or use a relation |
| Inputs are partly undefined, like at | The rule gives no output there | Restrict the domain and state it |
| You need to undo a many-to-one map | No inverse exists | Work with preimages, or a different invertible map |
| Data may be missing or noisy | A deterministic rule hides the uncertainty | A random mapping, or a conditional distribution |
Two cautions are worth remembering. First, a function is defined by its domain and codomain as well as its rule. The same formula on two different domains can be injective on one and not on the other, as Worked example 3 showed. Second, in programming a "function" often has side effects or may not always return the same answer, which goes beyond the mathematical definition. Here we mean the mathematical kind.
A related idea is the relation, which allows one input to have several outputs. Every function is a relation, but not every relation is a function.
History
The word "function" was used in a loose sense by Gottfried Leibniz in the 1670s, for a quantity that depends on a curve, such as a tangent length. For a long time it meant an expression built from a formula.
Leonhard Euler, in the 1700s, treated a function as an analytic expression in a variable, and his textbooks made that view standard. The modern definition, in which a function is any rule that assigns a unique output to each input, is usually credited to Peter Gustav Lejeune Dirichlet around 1837. That definition allowed functions with no simple formula at all.
The set-theoretic version, with explicit domain and codomain, came with the development of set theory at the end of the 19th century and was standardised in the 20th century, largely through the work of the Bourbaki group. Once functions were defined as sets of pairs, they could be studied with the same tools as sets.
Check yourself
QuestionLet with , , . Is a function? Is it injective? Is it surjective?Show answer
It is a function, since each of has exactly one output. It is not injective, because with . It is surjective, because the range is the whole codomain.
QuestionFor from the reals to the reals, find .Show answer
Solve . Then and , so . Check: .
QuestionWhy can the squaring function not have an inverse on all real numbers, and what must change to give it one?Show answer
It sends both and to , so going back from would have to pick one of two inputs. The function is also not onto the reals, since negative numbers are never outputs. Restricting the domain to and the codomain to makes it a bijection, and its inverse is the square root.
QuestionYou have a function that maps a student's ID to their exam score, and a second function that maps a score to a letter grade. Which composition is meaningful, or , and why?Show answer
is meaningful. It takes a student ID, finds the score with , and then maps that score to a letter with . The other order would feed a letter grade into , but expects a student ID, so the composition does not type-check.
QuestionA data set has the rule 'each day maps to the closing price of a stock'. Is this a function, and what would break if a day had two recorded closing prices?Show answer
Yes, it is a function, provided each day has one recorded closing price. If a day had two prices, the rule would assign two outputs to one input, and the data would no longer be a function. The fix is to decide which price counts as the closing price, or to store the data as a relation with a timestamp.
Sources
- Function (mathematics), Wikipedia: https://en.wikipedia.org/wiki/Function_(mathematics)
- Paul Halmos, Naive Set Theory (Springer): https://link.springer.com/book/10.1007/978-1-4757-1645-0
- Herbert Enderton, Elements of Set Theory (Academic Press): https://www.elsevier.com/books/elements-of-set-theory/enderton/978-0-12-238440-0