What is the difference between a function and a formula? I think that the difference is that the domain and codomain are part of a function definition, whereas a formula is just a relationship between variables, with no particular input set specified.

Hence, for two functions $f$ and $g$, $f(x)$ can be equal to $g(x)$ for all integers, say, but if the domain of $f$ is {2, 3, 4} and the domain of $g$ is {6, 7, 8, 9}, the two functions will be different. 

And on the converse, if the functions 'do different things' - i.e. $f(x) = x$ and $g(x) = x^3$ - but the domains of $f$ and $g$ (these are the same) are set up such that the values of the functions are the same over the domain (this would work in this case for {-1, 0, 1}), then the functions are the same, even though the formulas are different.

Is this correct?

Thank you.
A formula is a string of symbols, arranged according to mathematical grammar.

A function is a mathematical object that plays a role in arithmetic operations like "evaluation" or "composition". A key point is that if $f$ and $g$ are expressions that denote two functions with the same domain and codomain, and we have $f(x) = g(x)$ for every $x$ in the domain, then $f$ and $g$ denote the same function.

So, in your example of functions $f$ and $g$ on the set $\{ -1, 0, 1 \}$, it is indeed true that $f=g$.

Some examples of mathematical grammar

Let $f$ be a variable that denotes a function on the reals. Let $x$ be a real-valued variable. Then:


$f$ is a function.
$f(x)$ is a real number.
$x^2 + 3$ is a real number. In particular, it is not a function. Unfortunately, people are frequently grammatically incorrect on this point. :(
$f(x) = x^2 + 3$ is an equation that relates two real numbers.


and all of the bullet points above are examples of formulas.

Recall that I mentioned functions were 'defined' pointwise: if two functions solve "$f(x) = x^2 + 3$ for all $x$" for $f$, then they must be the same function. Because of this, we can use equations like this as a way to specify functions.

But as you note, if two different formulas for the right hand side actually give the same values when you substitute values from the domain, then the functions so defined will be the same.

Incidentally, it is possible to define functions directly rather than pointwise, although it often isn't pleasant. e.g. the function $f$ defined above is given by

$$ f = p \circ ((\mu \circ \Delta), c_3) $$

where I'm using the notation


$\Delta$ is the diagonal function $\Delta(x) = (x, x)$
$\mu$ is the multiplication function $\mu(x,y) = xy$
$p$ is the addition function $p(x,y) = x+y$
$c_3$ is the constant function $c_3(x) = 3$
$(,)$ is a binary operation on functions; it's defining property is $(g,h)(x) = (g(x), h(x))$. I don't actually know of standard notation for this; sometimes I see $\times$ in place of $,$.
$\circ$ is composition of functions: $(g \circ h)(x) = g(h(x))$.
I was taking a refresher course on Calculus and as a pre-requisite, was reviewing algebra and some pre-algebra as well :). It was said, for a function to be a function, it must have a definite output corresponding to every input. Based on that, $f(x) = x$ is a function, but $f(x) = \sqrt{x}$ is not, as there are always two answers that could be the square root of a positive number (one positive and the other negative). However, both can be called formula. I believe this is at least one key differentiator of the two.