On “well-defined” functions

As a TA for CS251 I frequently see people verify that a function is well-defined when there is no need to. And just as often I see the opposite. So let’s set the record straight here: what exactly is a function, and when do we need to check whether something is genuinely a function?

Given two sets XX and YY, a function if f:XYf \colon X \to Y is just a subset of the Cartesian product X×YX \times Y such that for each xXx \in X, there is exactly one yYy \in Y such that (x,y)f(x, y) \in f.

As you expect, we write f(x)f(x) to denote said unique yy.

So what happens when we attempt to define a function? Essentially, we are associating each xXx \in X with some subset of elements in YY. For example, in the function f:f : \mathbb N \to \mathbb N where f(x)=2xf(x) = 2x, we are associating xx with {2x}\{2x\}. For example, we just associate 11 with 22, 22 with 44, and so on.

When we attempt to define a function ff, we would like to have ff associate each xx with exactly one value in yy.

Most of the time we write a function as f(x)=f(x) = \dots and in this case there are no problems. If it is some formula of xx, this is always fine. Perhaps it is a piecewise formula, where you first have to check whether xx is even or odd. No matter. This will always be fine.

Now let me give you a very quick bad definition of a function. Suppose that we are attempting to map the positive rationals to positive integers. Suppose you say, “we associate the rational p/qp / q (where pp, qq are integers) with the integer p+qp + q.” News flash: we associate 2/32 / 3 with 2+3=52 + 3 = 5 and 4/64 / 6 with 4+6=104 + 6 = 10. Since 2/3=4/62 / 3 = 4 / 6, we have associated multiple elements with the same rational.

Now what is the actual issue? When we define a function, we are associating a representation of an element in XX with some value in YY. What we are really doing when we say “for all xXx \in X, f(x)=2xf(x) = 2x” is that we are representing each element of XX as xx. The “for all xx” quantifier guarantees that each element in XX is accessed exactly once with the representation xx, and then we associate each xx with 2x2x.

But when we look at f(p/q)=p+qf(p/q) = p + q, what we are doing is associating a value to every fractional representation of a number. And we have already seen that a rational number has many fractional representations, such as 2/3=4/62 /3 = 4 / 6. Of course, the way we fix this is stipulate “for all relatively prime p,qp, q, we have f(p/q)=p+qf(p/q) = p + q”. Now this genuinely is a function, because we have ensured that we are only associating values to one representation of each rational number.

If we are earnestly trying to define a function ff, we are not going to associate a representation of an element with more than one value. Because if we do, the subset of X×YX \times Y we are describing automatically is not a function anymore.

Now, we do not need ascribe every representation of an element in XX with a value in YY. Ideally, we would like to ascribe exactly one representation of an element with a value, which is why there is no need to check that functions of the form “for all xXx \in X, f(x)=f(x) = \dots” are well-defined. There is nothing to check! It is obviously a function.

But what of functions where you genuinely have no choice but to associate a value to multiple representations of an element? In this case, I personally say we check for representation invariance of the function, or that such a function is representation invariant.

As an example, take modular arithmetic. We would like to be able to define 13+1013 + 10 modulo 1212 the same way that 1+101 + 10 is defined modulo 1212, and it turns out that 13+101+1013 + 10 \equiv 1 + 10 modulo 1212. Likewise, 131011013 \cdot 10 \equiv 1 \cdot 10 modulo 1212. So it is okay to ascribe a value to multiple representations when defining multiplication, because they are all consistent.

In general, what we need to check here is representation invariance. In other words, if aba \equiv b and cdc \equiv d, we need to make sure that acbda c \equiv b d. (All these equivalences are modulo nn.) And it is easy to check that this is indeed true, so multiplication can be properly defined in modular arithmetic.

To summarize,

And the last bullet point is the only time you need to check that a function is “well-defined”.