The naive conception of a function $f:X\to Y$ is that it is a "rule" or "transformation" with the following property: to every $x\in X$, there is associated a unique $y\in Y$; this $y$ is denoted as $f(x)$. The set $X$ is called the domain of the function, and $Y$ is called the codomain. The graph of $f:X\to Y$ is the set
$$
\{(x,y)\in X\times Y\mid\text{$y=f(x)$}\} \, .
$$
Two functions $f:X\to Y$ and $g:A\to B$ are considered equal if and only if:
- They have the same domain: $X=A$.
- They have the same codomain: $Y=B$.
- They have the same values: for all $x\in X$, the equality $f(x)=g(x)$ holds. Equivalently, we could ask that they have the same graph.
Note that we do not require that $f$ has to be expressible by an algebraic formula, or even by one uniform condition that applies to every element of $X$. For example, it is perfectly acceptable to define a function $f:\mathbb R\to\mathbb R$ such that $f(x)=1$ if $x$ is rational and $f(x)=0$ if $x$ is irrational. This means that the term "rule" arguably does not do justice to the modern viewpoint of functions, which is that each $x\in X$ can be associated to any $y\in Y$, and it is irrelevant whether a function can be described by a nice formula (or even be described at all).
Although the naive conception of a function suffices for doing most ordinary mathematics, you might reasonably question whether it is a precise enough notion – just what do we mean by "rule" or "transformation", after all? To allay your discomfort, mathematicians formulated the triple definition, which makes no mention of vague words like "rule" or "transformation". The triple definition might not seem particularly intuitive to you – I will add some further comments about this at the end of my answer. For now, I ask you just to go along with it.
Definition. A function is an (ordered) triple $(X,Y,f)$, where $X$ and $Y$ are sets, and $f$ is a subset of $X\times Y$ satisfying the following properties:
- For every $x\in X$, there is a $y\in Y$ such that $(x,y)\in f$.
- For every $x\in X$, and for all $z,z'\in Y$, if $(x,z)\in f$ and $(x,z')\in f$ then $z=z'$.
We give the following names to the sets $X$, $Y$, and $f$:
- $X$ is called the domain.
- $Y$ is called the codomain.
- $f$ is called the graph.
If $x$ is an arbitrary element of $X$, then it follows from properties (1) and (2) that there is one and only one $y\in Y$ such that $(x,y)\in f$; this $y$ is denoted as $f(x)$.
Earlier, we mentioned that two functions are consider equal if and only if they have the same domain, codomain, and values. The triple definition reflects this.
It is common to refer to the function as $f:X\to Y$ rather than $(X,Y,f)$. This practice should not lead to any confusion, since we are still specifying the three "ingredients" that make up the function. For completeness, we should note that the range (or image) of $f:X\to Y$ is defined as
$$
\{y\in Y\mid\text{there is an $x\in X$ such that $f(x)=y$}\} \, .
$$
However, this concept does not enter the following discussion.
Commentary. If our definition is to be taken seriously, then $f$ is not the function: rather, it is the graph of the function. Nevertheless, it is conventional to abuse notation and refer to the triple $(X,Y,f)$ as $f$ for short. Another way to make sense of this is to use a different letter, say $\mathcal G$, for the graph of the function, and let $f=(X,Y,\mathcal G)$. Then, $f$ is literally the function.
As it happens, you will often hear even more informal statements such as "consider the function $f(x)=x^2$". Such ellipsis only makes sense when the domain and codomain are clear from context. In this case, it is most likely that what is meant is the following: "consider the function $(\mathbb R,\mathbb R,f)$, where $f=\{(x,y)\in\mathbb R\times\mathbb R\mid y=x^2\}$".
Although the triple definition is undoubtedly precise, it is quite normal to find it unsatisfying in a number of ways. For instance, the idea of thinking of a function as a triple, and $f$ as a subset of the cartesian product of two sets, will probably seem quite unnatural to you. Actually, most mathematicians would agree with you. So why is the triple definition the "accepted" way of defining functions? This is quite a difficult question to answer, but I will try my best.
The first thing to note is that few people actually claim to think of a function as a triple. The definition above should not be interpreted as the "correct" way of thinking of functions in practice. Instead, it serves a different purpose: it convinces us that it is possible to give a precise meaning to the naive conception of a function. This helps reassure us that the naive conception will not lead to any contradictions. It also shows that more complicated mathematical objects can be "implemented" in terms of simpler ones, if one so desires.
This phenomenon can be seen throughout pure mathematics. In fact, ordered triples can themselves be defined in terms of ordered pairs $(a,b)$, and ordered pairs can be defined in terms of sets. The prevailing convention in set theory is to define $(a,b)$ as the set $\{\{a\},\{a,b\}\}$! Again, nobody is claiming that this is how they think of ordered pairs in practice. The naive conception of an ordered pair is that it is some object $(x,y)$ such that $(x,y)=(x',y')$ if and only if $x=x'$ and $y=y'$. All the set-theoretic definition does is help convince us that this idea make sense: there is a mathematical object with the aforementioned property. However, for our day-to-day purposes, it is completely irrelevant what an ordered pair is formally defined as: what matters is the properties that it has. In fact, some mathematicians would go as far as saying that $(x,y)=\{\{x\},\{x,y\}\}$ is not really a definition: rather, it is simply how ordered pairs are implemented in a set theory. In practice, we favour the idea that numbers, ordered pairs, functions, and sets etc. all have different "types"; thinking of them all being sets in disguise is rather unnatural and unhelpful.
Useful reading
- In this post, Asaf Karagila offers some more thoughts about how we should think of functions from a foundational point of view. (By contrast, my answer mainly focuses on how we should think of functions in "ordinary mathematics" – i.e. outside of logic and set theory.) Asaf's answer also mentions a different way that functions are sometimes defined in set theory.
- The difference between the two definitions of a function is elaborated further in this post by John Bentin. Briefly, if a function is identified with its graph, then the concept of the codomain no longer makes sense: the function $f:\mathbb R\to\mathbb R$ given by $f(x)=x^2$ is identical to the function $g:\mathbb R\to[0,\infty)$ given by $g(x)=x^2$ according to the "set of ordered pairs..." definition.