What is the actual definition of a function? I am learning precalculus and my book defines the following:


  A function $f$ from a set $A$ to a set $B$ is a rule that assigns to every element $a$ in $A$ one and only one value in $B$.


Well, I am thinking, a rule isn't something that I've seen defined mathematically. So what is a function, really? Is it a subset of $A\times B$ or something?
Indeed, a function is a subset of $A \times B$ with the following very important property: for every $x \in A$, there exist a unique $y \in B$ such that $(x,y) \in A \times B$. Intuitively, this tells you that a function cannot take an element $x$ into several distinct values $y$ - that wouldn't be a function anymore, but somehing called "binary relation" (that you need not worry about).

Nevertheless, for many practical purposes, thinking of a function as a rule, or a correspondence between $x$ values and $y$ values, will suffice. Still, keep the correct definition somewhere in the back of your mind, readily available if necessary.
A function is a rule which relates the values of one variable quantity to the values of another variable quantity, and does so in such a way that the value of the second variable quantity is uniquely determined by (i.e. is a function of) the value of the first variable quantity. 

This is clearly a very general definition. Such rules are general enough to serve as mathematical models for a large number of natural relationships between variable physical quantities.

We name functions by using symbols like "f," "g," "h," etc. Just as your name signifies all of the many things that make "you," a symbol like "f" serves as a shorthand for what may be a long or complicated rule expressing a particular relationship between variable quantities. We name the variable quantities with symbols like "x," "y," "t," etc. We then write  y = f(x)  to show that  f  is a rule which assigns a unique value of the variable quantity  y  to values of the variable quantity  x.

Notice that the definition distinguishes between a first variable quantity and a second variable quantity. It is the second variable which is a function of the first, and when we write  y = f(x)  we mean that the value of  y  depends (in a way specified by the rule  f ) on the value of  x, but not necessarily vice versa. For example, what you wear may depend on the weather, but the reverse is certainly not true. We make this distinction among variables by calling  x  the input to the function and  y  the output, or by calling  x  the independent variable and  y  the dependent variable.