Search

Search papers and lessons

Function

Level 1 · 11 min read

A function assigns exactly one output to every input, and that single rule is how most of mathematics and programming describes a relationship.

On this page

  • Colour = topic mix
  • Discrete math
  • Sign in to see your progress

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 y=f(x)y = f(x), 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 A=s2A = s^2 describes the area for every side length ss at once. The function is the compact way to say "this value is determined by that one".

−3−2−1012302468xf(x)
  • f(x) = x²
Each input has one output on this curve. The points at x = 2 and x = -2 both give 4, which is allowed: two inputs may share an output.

The math

Definition: Function

A function ff from a set AA to a set BB, written f:A→Bf : A \to B, is a rule that assigns to each element a∈Aa \in A exactly one element f(a)∈Bf(a) \in B. The set AA is the domain, the set BB is the codomain, and the set of values actually produced,

range⁡(f)={ f(a):a∈A },\operatorname{range}(f) = \{\, f(a) : a \in A \,\},

is the image or range. The range is a subset of the codomain, and it need not equal it.

The symbol ∈\in means "is an element of" and ⊆\subseteq means "is a subset of". Inside the definition, aa stands for any input and f(a)f(a) for its output.

The graph

The graph of ff is the set of pairs {(a,f(a)):a∈A}\{ (a, f(a)) : a \in A \}. 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: f(a)=f(a′)f(a) = f(a') implies a=a′a = a';
  • surjective (onto) if every element of the codomain is hit: for every b∈Bb \in B there is an a∈Aa \in A with f(a)=bf(a) = b;
  • bijective if it is both.

A bijection has an inverse function f−1:B→Af^{-1} : B \to A, defined by f−1(b)=af^{-1}(b) = a when f(a)=bf(a) = b. Without both properties, no such inverse can be defined, because either an output would point back to two inputs, or some element of BB would point back to nothing.

Composition

If f:A→Bf : A \to B and g:B→Cg : B \to C, the composition g∘f:A→Cg \circ f : A \to C is defined by (g∘f)(a)=g(f(a))(g \circ f)(a) = g(f(a)). You apply ff first and then gg. In general g∘f≠f∘gg \circ f \neq f \circ g, which Worked example 3 demonstrates.

Worked examples

Example
1. A function on a small finite set

Let A={1,2,3,4}A = \{1, 2, 3, 4\} and B={0,1}B = \{0, 1\}. Define f(n)f(n) to be the remainder when nn is divided by 2.

  1. Compute each output: f(1)=1f(1) = 1, f(2)=0f(2) = 0, f(3)=1f(3) = 1, f(4)=0f(4) = 0.
  2. Every input has exactly one output, so ff is a function from AA to BB.
  3. The range is {0,1}\{0, 1\}, which equals BB, so ff is surjective.
  4. f(1)=f(3)=1f(1) = f(3) = 1 with 1≠31 \neq 3, so ff is not injective.

Interpretation: ff 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.

Example
2. An invertible function, checked

Let f(x)=2x+3f(x) = 2x + 3 from the real numbers to the real numbers.

  1. Solve y=2x+3y = 2x + 3 for xx: subtract 3 to get y−3=2xy - 3 = 2x, then divide by 2 to get x=(y−3)/2x = (y - 3)/2.
  2. So the inverse is f−1(y)=(y−3)/2f^{-1}(y) = (y - 3)/2. Each real yy gives exactly one real xx, so ff is a bijection.
  3. Check with a number: f(5)=2⋅5+3=13f(5) = 2 \cdot 5 + 3 = 13. Then f−1(13)=(13−3)/2=5f^{-1}(13) = (13 - 3)/2 = 5, which returns the original input.
  4. Check the other direction: f(f−1(y))=2⋅y−32+3=y−3+3=yf(f^{-1}(y)) = 2 \cdot \dfrac{y-3}{2} + 3 = y - 3 + 3 = y.

Interpretation: a bijection can be undone. Converting a temperature from Celsius to Fahrenheit and back is the same kind of pair.

Example
3. Squaring is not invertible, but restriction fixes it

Let f(x)=x2f(x) = x^2 from the real numbers to the real numbers, and let g(x)=x+1g(x) = x + 1.

  1. f(2)=4f(2) = 4 and f(−2)=4f(-2) = 4. Two different inputs share an output, so ff is not injective.
  2. Its range is [0,∞)[0, \infty), the non-negative numbers, which is not all of the real numbers, so ff is not surjective either.
  3. Restrict both domain and codomain to [0,∞)[0, \infty). Now ff is injective and surjective, so it has an inverse, f−1(y)=yf^{-1}(y) = \sqrt{y}.
  4. Compose: (g∘f)(3)=g(f(3))=g(9)=10(g \circ f)(3) = g(f(3)) = g(9) = 10, while (f∘g)(3)=f(g(3))=f(4)=16(f \circ g)(3) = f(g(3)) = f(4) = 16.

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.

SituationProblemBetter choice
A quantity has two possible values for one input, like ±x\pm\sqrt{x}A single-valued function cannot describe bothChoose a branch explicitly, or use a relation
Inputs are partly undefined, like 1/x1/x at x=0x = 0The rule gives no output thereRestrict the domain and state it
You need to undo a many-to-one mapNo inverse existsWork with preimages, or a different invertible map
Data may be missing or noisyA deterministic rule hides the uncertaintyA 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 f:{a,b,c}→{1,2}f : \{a, b, c\} \to \{1, 2\} with f(a)=1f(a) = 1, f(b)=1f(b) = 1, f(c)=2f(c) = 2. Is ff a function? Is it injective? Is it surjective?Show answer

It is a function, since each of a,b,ca, b, c has exactly one output. It is not injective, because f(a)=f(b)=1f(a) = f(b) = 1 with a≠ba \neq b. It is surjective, because the range {1,2}\{1, 2\} is the whole codomain.

QuestionFor f(x)=2x+3f(x) = 2x + 3 from the reals to the reals, find f−1(11)f^{-1}(11).Show answer

Solve 11=2x+311 = 2x + 3. Then 2x=82x = 8 and x=4x = 4, so f−1(11)=4f^{-1}(11) = 4. Check: f(4)=2⋅4+3=11f(4) = 2 \cdot 4 + 3 = 11.

QuestionWhy can the squaring function x↦x2x \mapsto x^2 not have an inverse on all real numbers, and what must change to give it one?Show answer

It sends both 22 and −2-2 to 44, so going back from 44 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 [0,∞)[0, \infty) and the codomain to [0,∞)[0, \infty) makes it a bijection, and its inverse is the square root.

QuestionYou have a function ff that maps a student's ID to their exam score, and a second function gg that maps a score to a letter grade. Which composition is meaningful, g∘fg \circ f or f∘gf \circ g, and why?Show answer

g∘fg \circ f is meaningful. It takes a student ID, finds the score with ff, and then maps that score to a letter with gg. The other order would feed a letter grade into ff, but ff 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