# Introduction to functions

A function is like a machine which relates an output to an input.

Example. We can think of “multiply by 3” as a function.

## Function notation

Every function has a domain (the set of inputs) and a codomain (the set of possible outputs), and allocates each element in the domain exactly one output in the codomain.

We often give a function a name such as $$f$$. If $$f$$ has domain $$X$$ and codomain $$Y$$, we can use arrow notation to write $$f:X\to Y$$ (read “the function $$f$$ from $$X$$ to $$Y$$”).

For a particular input $$x$$, we write $$f(x)$$ to mean the output when $$f$$ is applied to $$x$$ (read “$$f$$ of $$x$$”). We often define a function by what $$f$$ does to an argument $$x$$.

Example. A function $$f$$ can be defined by the equation $$f(x) = x^2,$$ which takes an input $$x$$ and squares it. We can evaluate the value of $$f$$ with any input by substituting $$x$$. For example, $$f(1)=1^2=1, \quad f(-3)=(-3)^2=9.$$ It is common to not explicitly define the domain and codomain, so we typically use the natural domain of the function, which is the maximum set of values for which the function is defined. The natural domain in this case is the set of real numbers $$\mathbb{R}$$.

Keep in mind that the $$x$$ is just a placeholder or “dummy variable”. The $$x$$ simply tells you where to substitute the input.

Example. The following functions are all the same function:

• $$f(x)=x^2-x+1$$
• $$f(t)=t^2-t+1$$
• $$f(Q)=Q^2-Q+1$$
• $$f(\theta)=\theta^2-\theta+1$$

## Examples of functions

In the above examples, we have used real numbers as inputs and outputs. However, this is not always the case.

Example – number of children. Let the domain be the set of humans who have ever lived, and the codomain be the natural numbers $$\{0,1,2,\dots\}$$. Let $$g$$ be the “number of children” function. For example, $$g(\text{Mick Jagger}) = 8$$ (this is true at the time of writing, this function can change over time!)1.

Example – Caesar cipher. Let the domain and the codomain be the letters of the alphabet. Let $$c$$ be the Caesar cipher function, where the function shifts the letters of the alphabet forward by 3 places (and wraps around to the beginning of the alphabet). For example, $$c(A)=D$$, $$c(B)=E$$ and $$c(Z)=C$$.

Example – negation. Let the domain and codomain be $$\{\text{False, True}\}$$ (which you can also think of $$\{0,1\}$$. Let $$\text{not}$$ be the negation function, defined by $$\text{not}(\text{True})=\text{False}, \quad \text{not}(\text{False})=\text{True}.$$ This is an example of a truth function, which takes a truth value as an input and produces a truth value as an output.

Example – first letter. Let the domain be the natural numbers $$\{0,1,2,\dots\}$$, and the codomain be the letters of the alphabet. Let $$h$$ be the function which outputs the first letter of the English representation of the input. For example, the number 12 in English is “twelve”, so $$h(12)=t$$.

Example – prime omega function. Let the domain and the codomain be the positive integers $$\{1,2,3,\dots\}$$. Let $$\omega(n)$$ be the number of distinct prime factors of $$n$$. For example, 15 has prime factors 3 and 5, so $$\omega(15)=2$$.

Most of the functions we will encounter will have the domain and codomain as the set of real numbers, and will usually be defined by an algebraic rule. Examples of functions we will deal with:

• Linear functions
• Trigonometric functions (e.g. $$\sin x$$, $$\cos x$$, $$\tan x$$)
• Exponential and logarithmic functions (e.g. $$e^x$$, $$\log x$$).
Example. Let $$f(x) = x^2+x+1$$. Then we can evaluate the value of $$f$$ with any input by substituting $$x$$. For example, $$f(1)=1^2+1+1=3, \quad f(-1)=(-1)^2+(-1)+1=1.$$
Example – piecewise function. Let $$f(x) = \left\{ \begin{array}{ll} x+1 & \text{for } x \ge 1 \\ x^2 & \text{for } x<1 \end{array}\right.$$ We have $$f(3) = 3+1=4 \quad f(-2)=(-2)^2=4.$$