In algebra a function was usually a formula, and you found its domain and range. In discrete math a function can be any rule between sets: from people to birthdays, from strings to lengths, from a set to itself. The definition focuses on what makes something a function at all.
Key idea
A function $f : A \to B$ assigns to each element of the domain $A$ exactly one element $f(a)$ of the codomain $B$. The range (or image) is the set of outputs actually reached, $f(A) = \{f(a) : a \in A\}$; it is a subset of the codomain and can be much smaller.
Why it works
"Exactly one" has two parts: every element of $A$ gets an output (none is skipped), and no element gets two. In an arrow diagram, that means exactly one arrow leaves each dot of $A$; the dots of $B$ may receive any number of arrows, including none. The codomain is part of the definition, a promise about where outputs are allowed to land, while the range records where they actually land. Keeping the two apart is what makes the next lesson's idea of "onto" possible.
Worked examples
Worked example: Function or not?
Let $A = \{1, 2, 3\}$ and $B = \{a, b\}$. Which of these is a function from $A$ to $B$? (i) $\{(1, a), (2, a), (3, b)\}$; (ii) $\{(1, a), (2, b)\}$; (iii) $\{(1, a), (1, b), (2, a), (3, a)\}$.
- (i) Each of $1, 2, 3$ has exactly one output. Two inputs share the output $a$, which is allowed: a function.
- (ii) $3$ has no output: not a function from $A$.
- (iii) $1$ has two outputs: not a function.
Answer: Only (i)
Worked example: Range versus codomain
Let $f : \{-2, -1, 0, 1, 2\} \to \mathbb{Z}$ be $f(x) = x^{2}$. Find the range.
- Outputs: $f(-2) = 4$, $f(-1) = 1$, $f(0) = 0$, $f(1) = 1$, $f(2) = 4$.
- Range: $\{0, 1, 4\}$. The codomain is still $\mathbb{Z}$; most integers are simply never reached.
Answer: $\{0, 1, 4\}$
Worked example: Counting functions
How many functions are there from $A = \{1, 2, 3\}$ to $B = \{a, b\}$?
- Each of the $3$ elements of $A$ independently chooses one of $2$ outputs.
- Product rule: $2 \cdot 2 \cdot 2 = 2^{3} = 8$. In general there are $|B|^{|A|}$ functions from $A$ to $B$.
Answer: $8$
Common mistake
Treating codomain and range as the same set. For $f : \mathbb{Z} \to \mathbb{Z}$, $f(x) = 2x$, the codomain is $\mathbb{Z}$, but the range is only the even integers. Saying "the range is $\mathbb{Z}$" claims that $3$ is an output, and it is not. Check: the codomain is given in the arrow $f : A \to B$; the range you must work out by asking which outputs actually occur.