Skip to content

Unit 2: Functions

Definition. A function f:A→Bf:A\to B assigns each element of the input set AA to exactly one element of the output set BB. The set AA is the domain, and the set BB is the codomain.

A function can be pictured as a machine or an arrow diagram, but the important rule is simple: every input gets exactly one output. Different inputs are allowed to share an output. What is not allowed is one input being sent to two different outputs.

The notation

f:A→Bf:A\to B

specifies three pieces of information:

  1. the rule or assignment ff,
  2. the domain AA,
  3. the codomain BB.

All three are part of the function. A formula by itself is not enough because its behavior depends on which inputs are allowed and which outputs are expected.

The expression f(x)f(x) is the output assigned to the input xx. The set of outputs the function actually reaches is its image or range:

im⁑(f)={f(x)∣x∈A}βŠ†B.\operatorname{im}(f)=\{f(x)\mid x\in A\}\subseteq B.

The codomain is part of the function’s definition. Two rules with the same formula but different domains or codomains are different functions.

Example. Let f:Rβ†’Rf:\mathbb R\to\mathbb R be defined by f(x)=x2f(x)=x^2. Identify its domain, codomain, and image, and evaluate f(βˆ’3)f(-3).

The domain and codomain come from the declaration f:Rβ†’Rf:\mathbb R\to\mathbb R, so both are R\mathbb R. Substituting βˆ’3-3 gives

f(βˆ’3)=(βˆ’3)2=9.f(-3)=(-3)^2=9.

Although the codomain is all real numbers, a real square cannot be negative. Every nonnegative real number does occur as a square, so

im⁑(f)=[0,∞).\operatorname{im}(f)=[0,\infty).

Thus, the image can be smaller than the codomain.

The image of a set of inputs is also useful. If SβŠ†AS\subseteq A, then

f(S)={f(x)∣x∈S}.f(S)=\{f(x)\mid x\in S\}.

For the squaring function above,

f([βˆ’2,1])=[0,4].f([-2,1])=[0,4].

The interval crosses 00, so the smallest output is 00; the input with the largest magnitude is βˆ’2-2, which gives the largest output 44.

Images behave predictably with unions. If S1,S2βŠ†AS_1,S_2\subseteq A, then

f(S1βˆͺS2)=f(S1)βˆͺf(S2).f(S_1\cup S_2)=f(S_1)\cup f(S_2).

An output is on the left exactly when it comes from an input in S1S_1 or an input in S2S_2, which is exactly what the right side says.

For intersections, only one direction is always guaranteed:

f(S1∩S2)βŠ†f(S1)∩f(S2).f(S_1\cap S_2)\subseteq f(S_1)\cap f(S_2).

If an input belongs to both sets, its output certainly belongs to both images. The reverse direction can fail because two different inputs may produce the same output.

Example. Let f:R→Rf:\mathbb R\to\mathbb R be given by f(x)=x2f(x)=x^2, and set

S1={βˆ’1,0},S2={0,1}.S_1=\{-1,0\}, \qquad S_2=\{0,1\}.

Their intersection is S1∩S2={0}S_1\cap S_2=\{0\}, so

f(S1∩S2)={0}.f(S_1\cap S_2)=\{0\}.

However,

f(S1)=f(S2)={0,1},f(S_1)=f(S_2)=\{0,1\},

and therefore

f(S1)∩f(S2)={0,1}.f(S_1)\cap f(S_2)=\{0,1\}.

The output 11 belongs to both images, but it comes from βˆ’1-1 in the first set and 11 in the second. There is no common input producing it. If ff is injective, this kind of collision cannot happen, and equality does hold for intersections.

There is also a formal set-based definition of a function. A function f:A→Bf:A\to B is a subset of the Cartesian product A×BA\times B in which every element of AA appears exactly once as a first coordinate. The ordered pair (a,b)(a,b) means that f(a)=bf(a)=b.

This definition captures both requirements at once: every input must appear, and it must appear with only one output. A general subset of AΓ—BA\times B is called a relation. A relation becomes a function only when it satisfies the exactly-one-output condition.

Example. Let A={1,2,3}A=\{1,2,3\} and B={2,3,4}B=\{2,3,4\}. The relation RR defined by a≀ba\leq b is

R={(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4)}.R=\{(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4)\}.

This relation is not a function from AA to BB. The input 11 appears with three different second coordinates, so it would have three outputs. By contrast, the identity function on AA lives in AΓ—AA\times A and is the set

id⁑A={(1,1),(2,2),(3,3)},\operatorname{id}_A=\{(1,1),(2,2),(3,3)\},

where each input appears exactly once.

Definition. Functions f:A→Bf:A\to B and g:C→Dg:C\to D are equal if

A=C,B=D,A=C,\qquad B=D,

and

f(x)=g(x)for every x∈A.f(x)=g(x)\quad\text{for every }x\in A.

The identity function on a set AA is

id⁑A:Aβ†’A,id⁑A(x)=x.\operatorname{id}_A:A\to A, \qquad \operatorname{id}_A(x)=x.

It leaves every element unchanged.

Identity functions may look trivial, but they give a reference point for what it means to leave a space unchanged. Later, composing a function with id⁑A\operatorname{id}_A will play the same role as multiplying a number by 11.

Example. Define f:Rβ†’Rf:\mathbb R\to\mathbb R by f(x)=x2f(x)=x^2 and g:[0,∞)β†’Rg:[0,\infty)\to\mathbb R by g(x)=x2g(x)=x^2. Determine whether f=gf=g.

The formulas agree wherever both functions are defined, but the domains do not:

dom⁑(f)=R,dom⁑(g)=[0,∞).\operatorname{dom}(f)=\mathbb R, \qquad \operatorname{dom}(g)=[0,\infty).

Therefore, ff and gg are different functions.


A proposed function must give one unambiguous output for every input in its domain. This can fail in three main ways: an input has no output, an input has more than one output, or a single mathematical object has several representations and the rule depends on which representation is chosen.

For example, the rule f(x)=1/xf(x)=1/x does not define a function f:R→Rf:\mathbb R\to\mathbb R because the allowed input x=0x=0 has no real output. It does define a function

f:Rβˆ–{0}β†’Rf:\mathbb R\setminus\{0\}\to\mathbb R

because removing 00 from the domain removes the problem.

Example. Consider the proposed rule f:Q→Zf:\mathbb Q\to\mathbb Z given by

f(ab)=a+b.f\left(\frac{a}{b}\right)=a+b.

Determine whether ff is well-defined.

The same rational number can be written in different ways. For example,

12=24,\frac12=\frac24,

but the rule gives

f(12)=1+2=3f\left(\frac12\right)=1+2=3

and

f(24)=2+4=6.f\left(\frac24\right)=2+4=6.

One input would have two outputs, so the rule is not well-defined. To repair it, one could require a unique standard representation: a/ba/b must be in lowest terms and b>0b>0. Requiring lowest terms alone is not quite enough because

12=βˆ’1βˆ’2\frac12=\frac{-1}{-2}

still gives two reduced representations unless the sign convention is fixed.

Every sequence is a function whose domain is usually N\mathbb N. A sequence a1,a2,a3,…a_1,a_2,a_3,\ldots can be written as

a:N→B,a(n)=an.a:\mathbb N\to B, \qquad a(n)=a_n.

For example, the sequence

2,4,8,16,…2,4,8,16,\ldots

is the function

a:N→R,a(n)=2n.a:\mathbb N\to\mathbb R, \qquad a(n)=2^n.

The input is the position in the sequence, and the output is the term at that position. Thinking of sequences as functions lets the same definitions of image, injectivity, and composition apply to them later.


These three words describe how the arrows from the domain land in the codomain:

PropertyWhat can go wrong?Informal picture
InjectiveTwo inputs collide at one outputno collisions
SurjectiveA codomain element is never reachedno gaps
BijectiveNeither problem occursperfect pairing

Definition. A function f:A→Bf:A\to B is injective or one-to-one if different inputs always have different outputs:

a1β‰ a2β‡’f(a1)β‰ f(a2).a_1\neq a_2\Rightarrow f(a_1)\neq f(a_2).

Equivalently,

f(a1)=f(a2)β‡’a1=a2.f(a_1)=f(a_2)\Rightarrow a_1=a_2.

The two injectivity statements are contrapositives, so they are logically equivalent. In proofs, the second form is usually easier: assume two outputs are equal, then show the inputs must have been equal.

For a real-valued graph, injectivity is checked by the horizontal line test: every horizontal line may intersect the graph at most once.

Definition. A function f:A→Bf:A\to B is surjective or onto if every element of the codomain is reached:

βˆ€b∈B,Β βˆƒa∈AΒ suchΒ thatΒ f(a)=b.\forall b\in B,\ \exists a\in A\text{ such that }f(a)=b.

Surjectivity compares the image with the stated codomain:

f is surjective⟺im⁑(f)=B.f\text{ is surjective} \quad\Longleftrightarrow\quad \operatorname{im}(f)=B.

This is why changing only the codomain can change whether a function is onto. The outputs do not change, but the target the function is expected to cover does.

A function that is both injective and surjective is bijective. A bijection pairs every input with exactly one output and reaches every element of the codomain.

A bijection is reversible: every output points back to exactly one input. This is the reason bijections are used to compare the sizes of sets and why invertible linear transformations become so important later.

Example. Classify each version of the squaring rule as injective, surjective, both, or neither:

f:Rβ†’R,g:[0,∞)β†’R,h:[0,∞)β†’[0,∞),f:\mathbb R\to\mathbb R, \qquad g:[0,\infty)\to\mathbb R, \qquad h:[0,\infty)\to[0,\infty),

where each function sends xx to x2x^2.

The function ff is not injective because

f(1)=f(βˆ’1)=1.f(1)=f(-1)=1.

It is not surjective because no negative real number is an output. Thus, ff is neither.

Restricting the domain removes the collision between positive and negative inputs, so gg is injective. Its codomain is still R\mathbb R, however, so it still misses every negative number and is not surjective.

The function hh uses the restricted domain and the exact image as its codomain. It is injective and surjective, so it is bijective.

Proof (A bijection on the rational numbers). Define f:Q→Qf:\mathbb Q\to\mathbb Q by

f(q)=3q+2.f(q)=3q+2.

To prove injectivity, suppose f(q1)=f(q2)f(q_1)=f(q_2). Then

3q1+2=3q2+2,3q_1+2=3q_2+2,

so q1=q2q_1=q_2.

To prove surjectivity, let b∈Qb\in\mathbb Q. Choose

q=bβˆ’23.q=\frac{b-2}{3}.

Since rational numbers are closed under subtraction and division by a nonzero rational number, q∈Qq\in\mathbb Q. Moreover,

f(q)=3(bβˆ’23)+2=b.f(q)=3\left(\frac{b-2}{3}\right)+2=b.

Thus, ff is both injective and surjective, so it is bijective.

Example. Let g:C→Rg:\mathbb C\to\mathbb R be defined by

g(a+bi)=a2+b2.g(a+bi)=\sqrt{a^2+b^2}.

Determine whether gg is injective or surjective.

It is not injective because distinct complex numbers can have the same magnitude. For example,

g(1+2i)=g(2+i)=5.g(1+2i)=g(2+i)=\sqrt5.

It is also not surjective onto R\mathbb R because magnitudes are never negative. In particular, there is no z∈Cz\in\mathbb C such that g(z)=βˆ’1g(z)=-1.

A function that is not injective on its full domain may become injective after the domain is restricted. The restriction must remove every repeated output, not just some of them.

Example. Let

f:[Ο€,2Ο€]β†’[βˆ’1,1]f:[\pi,2\pi]\to[-1,1]

be defined by f(x)=cos⁑xf(x)=\cos x. Determine whether ff is bijective.

On the interval [Ο€,2Ο€][\pi,2\pi], cosine increases from βˆ’1-1 to 11 without reversing direction. Therefore, no horizontal line meets this part of the graph more than once, so ff is injective.

Every value between βˆ’1-1 and 11 occurs as cosine moves continuously from βˆ’1-1 to 11. Thus,

im⁑(f)=[βˆ’1,1],\operatorname{im}(f)=[-1,1],

which equals the codomain. The function is also surjective, so it is bijective.

Restricting the domain carelessly may not work. For example, x2x^2 is still not injective on

(βˆ’βˆž,βˆ’1)βˆͺ(1,∞)(-\infty,-1)\cup(1,\infty)

because both xx and βˆ’x-x remain in the domain whenever ∣x∣>1\lvert x\rvert>1. A restriction makes a function injective only if each output is left with at most one input.


Functions can be connected so that the output of one becomes the input of another. If

f:A→Bandg:B→C,f:A\to B \qquad\text{and}\qquad g:B\to C,

then the composition of gg with ff is the function

g∘f:Aβ†’Cg\circ f:A\to C

defined by

(g∘f)(a)=g(f(a)).(g\circ f)(a)=g(f(a)).

The rightmost function acts first: begin with aa, apply ff, and then apply gg to the result. The codomain of ff must fit the domain of gg so that the second step is meaningful.

Example. Let f,g:R→Rf,g:\mathbb R\to\mathbb R be defined by

f(x)=x3+1andg(x)=x3.f(x)=x^3+1 \qquad\text{and}\qquad g(x)=x^3.

For f∘gf\circ g, apply gg first:

(f∘g)(x)=f(x3)=(x3)3+1=x9+1.(f\circ g)(x)=f(x^3)=(x^3)^3+1=x^9+1.

For g∘fg\circ f, apply ff first:

(g∘f)(x)=g(x3+1)=(x3+1)3=x9+3x6+3x3+1.(g\circ f)(x)=g(x^3+1)=(x^3+1)^3 =x^9+3x^6+3x^3+1.

The two compositions are different. For instance,

(f∘g)(1)=2,(g∘f)(1)=8.(f\circ g)(1)=2, \qquad (g\circ f)(1)=8.

Thus, function composition is generally not commutative: changing the order can change the result.

Although composition is not usually commutative, it is associative.

Proof (Associativity of function composition). Suppose

f:A→B,g:B→C,h:C→D.f:A\to B, \qquad g:B\to C, \qquad h:C\to D.

For every a∈Aa\in A,

(h∘(g∘f))(a)=h((g∘f)(a))=h(g(f(a))).\bigl(h\circ(g\circ f)\bigr)(a) =h\bigl((g\circ f)(a)\bigr) =h(g(f(a))).

On the other hand,

((h∘g)∘f)(a)=(h∘g)(f(a))=h(g(f(a))).\bigl((h\circ g)\circ f\bigr)(a) =(h\circ g)(f(a)) =h(g(f(a))).

The two functions have the same domain, codomain, and output at every input, so

h∘(g∘f)=(h∘g)∘f.h\circ(g\circ f)=(h\circ g)\circ f.

The identity function behaves like doing nothing before or after a function:

f∘id⁑A=f,id⁑B∘f=ff\circ\operatorname{id}_A=f, \qquad \operatorname{id}_B\circ f=f

for every f:A→Bf:A\to B. Also, the composition of two bijections is again a bijection, so several reversible steps can be joined into one reversible process.


An inverse function reverses another function. If f:A→Bf:A\to B, an inverse of ff is a function g:B→Ag:B\to A satisfying both

f∘g=id⁑Bf\circ g=\operatorname{id}_B

and

g∘f=id⁑A.g\circ f=\operatorname{id}_A.

The first identity says that starting in BB, moving backward with gg, and then forward with ff returns to the original element. The second says the same thing for an element that starts in AA. When an inverse exists, it is unique and is written fβˆ’1f^{-1}.

Theorem. A function is invertible if and only if it is bijective.

Proof. First suppose f:A→Bf:A\to B has an inverse g:B→Ag:B\to A. If f(a1)=f(a2)f(a_1)=f(a_2), applying gg gives

g(f(a1))=g(f(a2)),g(f(a_1))=g(f(a_2)),

so a1=a2a_1=a_2. Thus, ff is injective. For any b∈Bb\in B, choose a=g(b)a=g(b). Then

f(a)=f(g(b))=b,f(a)=f(g(b))=b,

so ff is surjective.

Conversely, suppose ff is bijective. For each b∈Bb\in B, surjectivity guarantees at least one a∈Aa\in A with f(a)=bf(a)=b, and injectivity guarantees that this aa is unique. Define g(b)g(b) to be that unique input. Then f∘g=id⁑Bf\circ g=\operatorname{id}_B and g∘f=id⁑Ag\circ f=\operatorname{id}_A, so g=fβˆ’1g=f^{-1}.

This theorem explains both possible failures of reversibility. If a function is not injective, one output does not reveal which input produced it. If it is not surjective, some element of the codomain has no input to return to.

Example. The bijection f:Q→Qf:\mathbb Q\to\mathbb Q defined by

f(q)=3q+2f(q)=3q+2

has an inverse. To find it, set y=3q+2y=3q+2 and solve for qq:

q=yβˆ’23.q=\frac{y-2}{3}.

Therefore,

fβˆ’1(y)=yβˆ’23.f^{-1}(y)=\frac{y-2}{3}.

Checking both directions gives

f(fβˆ’1(y))=3(yβˆ’23)+2=yf\left(f^{-1}(y)\right) =3\left(\frac{y-2}{3}\right)+2 =y

and

fβˆ’1(f(q))=(3q+2)βˆ’23=q.f^{-1}(f(q)) =\frac{(3q+2)-2}{3} =q.

The squaring rule on all of R\mathbb R has no inverse because it is not injective. After restricting it to

h:[0,∞)β†’[0,∞),h(x)=x2,h:[0,\infty)\to[0,\infty), \qquad h(x)=x^2,

it becomes bijective, and its inverse is

hβˆ’1(y)=y.h^{-1}(y)=\sqrt y.

The domain restriction is not a technical detail: it is what makes each nonnegative output point back to exactly one input.

The notation fβˆ’1(T)f^{-1}(T) is also used for the preimage of a subset TβŠ†BT\subseteq B:

fβˆ’1(T)={a∈A∣f(a)∈T}.f^{-1}(T)=\{a\in A\mid f(a)\in T\}.

A preimage asks which inputs land inside a chosen set of outputs. It exists for every function; ff does not need to be invertible. The context makes clear whether fβˆ’1f^{-1} means an inverse function or the preimage operation on sets.

Preimages preserve both unions and intersections exactly:

fβˆ’1(T1βˆͺT2)=fβˆ’1(T1)βˆͺfβˆ’1(T2)f^{-1}(T_1\cup T_2)=f^{-1}(T_1)\cup f^{-1}(T_2)

and

fβˆ’1(T1∩T2)=fβˆ’1(T1)∩fβˆ’1(T2).f^{-1}(T_1\cap T_2)=f^{-1}(T_1)\cap f^{-1}(T_2).

There is no injectivity requirement here. A single input has only one output, so checking whether that output belongs to both target sets creates no ambiguity.

Example. Let f:R→Rf:\mathbb R\to\mathbb R be defined by f(x)=x2f(x)=x^2. Find the preimage of [1,4][1,4].

We need all real inputs whose squares lie between 11 and 44:

1≀x2≀4.1\leq x^2\leq 4.

This occurs when 1β‰€βˆ£xβˆ£β‰€21\leq\lvert x\rvert\leq 2, so

fβˆ’1([1,4])=[βˆ’2,βˆ’1]βˆͺ[1,2].f^{-1}([1,4])=[-2,-1]\cup[1,2].

The function itself is not invertible on R\mathbb R, but the preimage of a set is still perfectly well-defined.


Many important functions in linear algebra take vectors, matrices, or even other functions as inputs. What matters is not whether there is a familiar algebraic formula, but whether every allowed input receives exactly one output in the stated codomain.

Example. Let R[x]\mathbb R[x] be the set of polynomials with real coefficients, and define the derivative operator

D:R[x]β†’R[x],D(p)=pβ€².D:\mathbb R[x]\to\mathbb R[x], \qquad D(p)=p'.

This function is not injective because different polynomials can have the same derivative. For example,

D(x2)=2x=D(x2+1).D(x^2)=2x=D(x^2+1).

It is surjective. If

q(x)=βˆ‘k=0nakxk,q(x)=\sum_{k=0}^{n}a_kx^k,

then the polynomial

p(x)=βˆ‘k=0nakk+1xk+1p(x)=\sum_{k=0}^{n}\frac{a_k}{k+1}x^{k+1}

satisfies D(p)=qD(p)=q. In other words, every polynomial has a polynomial antiderivative.

If the domain is restricted to polynomials satisfying p(0)=1p(0)=1, the arbitrary constant is fixed. On that restricted domain, differentiation becomes bijective, with inverse

Dβˆ’1(q)(x)=1+∫0xq(t) dt.D^{-1}(q)(x)=1+\int_0^x q(t)\,dt.

Example. Define m:R2β†’Rm:\mathbb R^2\to\mathbb R by

m(x,y)=xy.m(x,y)=xy.

This function is not injective because, for example,

m(1,2)=m(2,1)=2.m(1,2)=m(2,1)=2.

It is surjective because any real number rr is the output of the input (r,1)(r,1):

m(r,1)=r.m(r,1)=r.

This example also shows that having more input coordinates does not prevent a function from being onto a smaller-looking codomain.

Example. The trace function sends a square matrix to the sum of its diagonal entries:

Tr⁑:Mn(R)β†’R,Tr⁑(A)=a11+a22+β‹―+ann.\operatorname{Tr}:M_n(\mathbb R)\to\mathbb R, \qquad \operatorname{Tr}(A)=a_{11}+a_{22}+\cdots+a_{nn}.

It is surjective: for any r∈Rr\in\mathbb R, the diagonal matrix with first diagonal entry rr and all other entries 00 has trace rr.

It is not injective because many matrices have the same trace. For example, when n=2n=2,

Tr⁑(1000)=1=Tr⁑(0001).\operatorname{Tr}\begin{pmatrix}1&0\\0&0\end{pmatrix} =1 =\operatorname{Tr}\begin{pmatrix}0&0\\0&1\end{pmatrix}.

Trace keeps one useful number while discarding most of the information in the matrix. This is a common theme: a function can compress a complicated object into a simpler output without being reversible.