39. Functions

39.1 The concept of function

In analytic geometry or calculus class you may have studied a real-valued function such as $G(x) = x^{2} + 2x + 5$. This function takes as input a real number and gives a real number as value; for example, the statement that $G(3) = 20$ means that an input of 3 gives an output of 20. It also means that the point $\langle 3,20\rangle$ is on the graph of the equation $y = G(x)$.

The concept of function to be studied here is more general than that example in several ways. In the first place, a function $F$ can have one type of input and another type of output. An example is the function which gives the number of characters in an English word: the input is a string of characters, say ‘cat’, and the output is its length, 3 in this case. Also, the most general sort of function need not be given by a formula the way $G$ is. For example, a price list is a function with input the name of an item and output the price of the item. The relationship between the name and the price is rarely given by a formula.

Here is the specification:

39.2 Specification: function

A function $F$ is a mathematical object which determines and is completely determined by the following data:

F.1 $F$ has a domain, which is a set and is denoted $\operatorname{dom} F$.

F.2 $F$ has a codomain, which is also a set and is denoted $\operatorname{cod} F$.

F.3 For each element $a \in \operatorname{dom} F$, $F$ has a value at $a$. This value is completely determined by $a$ and $F$ and must be an element of $\operatorname{cod} F$. It is denoted by $F(a)$.

39.2.1 Warning This specification for function is both complicated and subtle and has conceptual traps. One of the complications is that the concept of function given here carries more information with it than what is usually given in calculus books. One of the traps is that you may tend to think of a function in terms of a formula for computing it, whereas a major aspect of our specification is that what a function is is independent of how you compute it.

39.2.2 Usage The standard notation $F\colon A \to B$ communicates the information that $F$ is a function with domain $A$ and codomain $B$. $A$ and $B$ are sets; $A$ consists of exactly those data which you can use as input to (you can “plug into”) the function $F$, and every value of $F$ must lie in $B$. (Not every element of $B$ has to be a value.)

In the expression “$F (a)$”, $a$ is called the argument or independent variable or input to $F$ and $F (a)$ is the value or dependent variable or output. The operation of finding $F (a)$ given $F$ and $a$ is called evaluation or application.

If $F (a ) =b$, one may say “$F$ takes $a$ to $b$” or “$a$ goes to $b$ under $F$”.

It follows from the definition that we have the following rule of inference:

$$F\colon A \to B,\ a\in A \vdash F(a)\in B \tag{39.1}$$

39.2.3 Warning You should distinguish between $F$, which is the name of the function, and $F (a)$, which is the value of $F$ at the input value $a$. Nevertheless, a function is often referred to as $F (x)$ — a notation which has the value of telling you what the notation for the input variable is.

39.2.4 Usage Functions are also called mappings, although in some texts the word “mapping” is given a special meaning.

39.3 Examples of functions

We give some simple examples of functions to illustrate the basic idea, and then after some more discussion and terminology we will give more substantial examples.

39.3.1 Example The first example is the function $G\colon \mathrm{R} \to \mathrm{R}$ defined by $G(x) = x^{2} + 2x + 5$, which was discussed previously. Referring to it as $G\colon \mathrm{R} \to \mathrm{R}$ specifies that the domain and codomain are both $\mathrm{R}$.

39.3.2 Usage As is often the case in analytic geometry and calculus texts, we did not formally specify the domain and codomain of $G$ when we defined it at the beginning of this section. In such texts, the domain is often defined implicitly as the set of all real numbers for which the defining formula makes sense. For example, a text might set $S(x) = 1/x$, leaving you to see that the domain is $\mathrm{R} - \{ 0\}$. Normally the codomain is not mentioned at all; it may usually be assumed to be $\mathrm{R}$. In this text, on the other hand, every function will always have an explicit domain and codomain.

39.3.3 Example Here is an example of a function with a finite domain and codomain. Let $A = \{ 1, 2, 3\}$ and $B = \{ 2, 4, 5, 6\}$. Let $F\colon A \to B$ be defined by requiring that $F(1) = 4$ and $F(2) = F(3) = 5$.

39.3.4 Example Let $S$ be some set of English words, for example the set of words in a given dictionary. Then the length of a word is a function $L\colon S \to \mathrm{N}$. For example, $L(\text{‘}cat\text{’}) = 3$ and $L(\text{‘}abbadabbadoo\text{’}) = 12$. (This function in Mathematica is StringLength. For example, StringLength["cat"] returns 3.)

39.3.5 Example Let $F\colon \mathrm{N} \to \mathrm{N}$ be defined by requiring that $F(0) = 0$ and for $n > 0$, $F(n)$ is the $n$th prime in order. Thus $F(1) = 2$, $F(2) = 3$, $F(3) = 5$, and $F(100) = 541$. (This function is given by the word Prime in Mathematica.)

39.3.6 Remarks The preceding examples illustrate several points:

a) You don’t have to give a formula to give a function. In the case of Example 39.3.3, we defined $F$ by giving its value explicitly at every element of the domain. Of course, this is possible only when the domain is a small finite set.

b) There must be a value $F(x)$ for every element $x$ of the domain, but not every element of the codomain has to be a value of the function. Thus 4 is not a value of the function in 39.3.5.

c) Different elements of the domain can have the same value (different inputs can give the same output). This happens in Example 39.3.1 too; thus $G(1) = G(-3) = 8$.

39.3.7 Exercise Let $A = \{ 1, 2, 3\}$. Let $F\colon A\to \mathcal{PP}A$ be defined by requiring that $F(n) = \{B \in \mathcal{P}A \mid n \in B\}$. What are $F(1)$ and $F(2)$? (Answer on page 244.)

39.3.8 Exercise Let $A$ be as in the preceding exercise, and define $G\colon A \to \mathcal{PP}A$ by $G(n) = \{B \in \mathcal{P}A \mid n \notin B\}$. What are $G(1)$ and $G(2)$?

39.3.9 Exercise Let $F\colon \mathrm{Z} \to \mathcal{P}\mathrm{Z}$ be defined by requiring that $F(n)$ be the set of divisors of $n$. What are $F(0)$, $F(1)$, $F(6)$ and $F(12)$?

39.3.10 Exercise Give an example of a function $F\colon \mathrm{R} \to \mathrm{R}$ with the property

$$r \text{ is an integer if and only if } F(r) \text{ is not an integer}$$

39.3.11 Exercise Let $S$ be a set and $G\colon S \to \mathcal{P}S$ a function. Let the subset $A$ of $S$ be defined by

$$A = \{x \mid x \notin G(x)\}$$

Show that there is no element $x \in S$ for which $G(x) = A$.

39.4 Functions in Mathematica

In Mathematica, the name of the function is followed by the input in square brackets. For example, $\sin x$ is entered as Sin[x].

You can define your own functions in Mathematica. The function $G(x) = x^{2} + 2x + 5$ of Example 39.3.1 can be defined by typing

g[x_] := x^2 + 2 x + 5 (39.2)

Then if you typed g[3], Mathematica would return 20, and if you typed g[t], Mathematica would return 5 + 2 t + t^2. Comments:

a) All built-in Mathematica functions, such as Sin, start with a capital letter. It is customary for the user to use lowercase names so as to avoid overwriting the Mathematica definition of some function. (You could define Sin to be anything you want, but that would be undesirable.) Thus there would be no error message if you typed G instead of g in (39.2), but it is not the Done Thing.

b) On the left side of a definition, the variable must be followed by an underline. This is how Mathematica distinguishes between a function and the value of a function.

c) One normally writes := for the equals sign in making a definition. There are occasions when the ordinary equals sign may be used, but a rule of thumb for definitions is to use :=.

A function that is defined by giving individual values instead of a formula can be defined in Mathematica by doing just that. For example, the function $F$ in Example 39.3.3 can be defined by entering

F[1] := 4; F[2] := 5; F[3] := 5 (39.3)

(When commands are strung together with semicolons in this way, Mathematica answers with the last value, 5 in this case. These commands could have been entered on separate lines.)

Mathematica does not keep track of the domain and codomain of the function. If you try to evaluate the function at an input for which it has not been defined, you get back what you typed. For example, if you had entered only the commands in (39.3) and then typed F[6], Mathematica would return F[6].

39.5 More about the input to a function

Let’s look at the function $G$ of Example 39.3.1 again. We can calculate that $G(3) = 20$. Since $1 + 2 = 3$, it follows that $G(1 + 2) = 20$. Since $\sqrt{9} = 3$, it follows that $G(\sqrt{9}) = 20$. It is the element $x$ (here 3) of the domain (here $\mathrm{R}$) that is being given as input to $G$, not the name of the element. It doesn’t matter how you represent 3, the value of $G$ at 3 is still 20.

This is summed up by the following theorem:

39.6 Theorem: The Substitution Property

Let $F\colon A \to B$ be any function, and suppose that $a \in A$ and $a' \in A$. If $a = a'$, then $F(a) = F(a')$.

The last sentence of Specification 39.2 can be stated more precisely this way:

39.7 Theorem: How to tell if functions are equal

If $F_{i}\colon A_{i} \to B_{i}$, $(i = 1,2)$, are two functions, then

$$(F_{1} = F_{2}) \Leftrightarrow \left( A_{1} = A_{2} \wedge B_{1} = B_{2} \wedge (\forall x\colon A_{1})\left( F_{1}(x) = F_{2}(x) \right) \right) \tag{39.4}$$

39.7.1 Method

To show that two functions are the same you have to show they have the same domain, the same codomain and for each element of the domain they have the same value.

39.7.2 Exercise Suppose $F\colon A \to B$ and $G\colon A \to B$. What do you have to do to prove that $F \neq G$?

39.7.3 Warning Since Formula (39.4) is an equivalence, this means that the function $S\colon \mathrm{R} \to \mathrm{R}$ for which $S(x) = x^{2}$ is not the same as the function $T\colon \mathrm{R} \to \mathrm{R}^{+}$ for which $T(x) = x^{2}$. They have the same domain and the same value at every element of the domain, but they do not have the same codomain. This distinction is often not made in the literature. In some theoretical contexts it is vital to make it, but in others (for example calculus) it makes no difference and is therefore quite rightly ignored. In this text we are purposely making all the distinctions made in a sizeable fraction of the research literature.

39.8 The abstract idea of function

As was noted previously, the specification given for functions says nothing about the formula for the function. The function $G$ in Example 39.3.1 was defined by the formula $G(x) = x^{2} + 2x + 5$, but the definition of the function called $F$ in Example 39.3.3 never mentioned a formula.

Until late in the nineteenth century, functions were usually thought of as defined by formulas. However, problems arose in the theory of Fourier analysis which made mathematicians require a more general notion of function. The definition of function given here is the modern version of that more general concept.It replaces the algorithmic and dynamic idea of a function as a way of computing an output value given an input value by the static, abstract concept of a function as having a domain, a codomain, and a value lying in the codomain for each element of the domain. Of course, often a definition by formula will give a function in this modern sense. However, there is no requirement that a function be given by a formula.

The modern concept of function has been obtained from the formula-based idea by abstracting basic properties the old concept had and using them as the basis of the new definition. This process of definition by abstracting properties is a major tool in mathematics, and you will see more examples of it later in the book (see Chapter 51, for example).

The concept of function as a formula never disappeared entirely, but was studied mostly by logicians who generalized it to the study of function-as-algorithm. (This is an oversimplification of history.) Of course, the study of algorithms is one of the central topics of modern computer science, so the notion of function-as-formula (more generally, function-as-algorithm) has achieved a new importance in recent years.

Nevertheless, computer science needs the abstract definition of function given here. Functions such as sin may be (and quite often are) programmed to look up their values in a table instead of calculating them by a formula, an arrangement which gains speed at the expense of using more memory.

Pages 56–60 of the book (PDF).