First of all we need to distinguish formulas/expressions from functions.
Functions are easier to explain so lets start with them. A function $f:X\to Y$ is actually defined as a special subset $f\subseteq X\times Y$ that satisfies two conditions:
- For any $x\in X$ there is $y\in Y$ such that $(x,y)\in f$
- If $(x,y)\in f$ and $(x,z)\in f$ for some $x\in X$ and $y,z\in Y$ then $y=z$.
On the other hand a formula is concept taken from logic and related formal language theories. A formula would be a sequence of symbols that satisfies some semantic rules. This is an informal definition, and in fact there are many ways to formalize this idea, one of them being lambda calculus.
So what is the connection between functions and formulas? The connection comes in the form of axiom schema of specification which roughly speaking says that if $\varphi$ is any formula, then $\{x\in X\ |\ \varphi(x)\}$ exists (although may be empty) and is a subset of $X$.
We can apply this to functions. Lets say $X$ and $Y$ are sets and $\varphi$ is a formula. By the above axiom we have a set
$$F:=\{(x,y)\in X\times Y\ |\ \varphi(x,y)\}$$
But it doesn't mean that this set is a function! For example if $X=Y=\mathbb{R}$ and our formula is "$x^2=y^2$" then this is not a function. Although it may happen that this formula actually gives us a function, for example when $X=Y=\{0\}$. So everything is important here: $X$, $Y$ and $\varphi$.
So now we simply say that a triple $X,Y,\varphi$ is a "well defined function" if the subset defined above is a function.
Also note a strange behaviour here. Once $F$ is constructed from $X,Y,\varphi$ triple, the information about $\varphi$ is actually lost. Sometimes a formula is recoverable from a function, if we know more about the function (e.g. linear functions are particularly simple to express). Often it is not recoverable. Also, there are always multiple formulas that yield the same function. So again: formulas and functions are different things.
In practice this is often applied to situation where $X$ is a set of sets and a function $f:X\to Y$ is defined in terms of representatives of elements of $X$. For example if we define
$$f:\mathbb{Z}/4\mathbb{Z}\to\{0,1\}$$
$$f([n])=n\text{ mod }2$$
there is a more formal construction hidden under those symbols. Formally $f$ is defined as all those pairs $(x,y)$ such that $x\in\mathbb{Z}/4\mathbb{Z}$ (and therefore $x$ is a set) and $y=n\text{ mod }2$ for some $n\in x$. Our axiom schema of specification guarantees that $f$ is a subset of $\mathbb{Z}/4\mathbb{Z}\times\{0,1\}$. So being a "well defined function" means we need to check function conditions, which in this case are satisfied.
A counterexample would be
$$f:\mathbb{Z}/4\mathbb{Z}\to \mathbb{Z}$$
$$f([n])=n+1$$
which formally would be defined as all those pairs $(x,y)$ such that $x\in\mathbb{Z}/4\mathbb{Z}$ and $y=n+1$ for some $n\in x$. Again $f$ is a subset of $\mathbb{Z}/4\mathbb{Z}\times\mathbb{Z}$, but this time it is not a well defined function, because if we take $A=\{\ldots, -8, -4, 0, 4, 8,\ldots\}$, i.e. all integers divisible by $4$ then $A\in\mathbb{Z}/4\mathbb{Z}$. However $(A,1)\in f$ (because $1=0+1$) and $(A,5)\in f$ (because
$5=4+1$) which breaks rule number 2 of our function definition. Informally speaking $f([0])=1$ and $f([4])=5$ even though $[0]=[4]$.
Now one may say: I've just shifted focus from functions to formulas. So what makes a formula well defined? Or maybe are all formulas well defined? For example, is "$f(x)=x+$" a well defined formula? Maybe it is, maybe it isn't. It actually depends on the underlying formal language and semantic rules. In particular we may give a meaning to "$x+$" (i.e. extend language) so that it does make sense. However generally not every formula is valid, given a fixed language.
And it is important to realize that. For example every compiler/interpreter in the world does a verification whether formulas that you wrote are well defined in the given programming language or not. It is necessary, because otherwise it would not be able to compile/interpret it. So it is an interesting topic on its own, but maybe a story for another time.
there is a function $f:X\to Y$, and some object $x$, and I would like to be able to take $f(x)$, but it is not necessarily given that $x\in X$.
You cannot. By definition $f(x)$ makes sense only when $x\in X$, and for all $x\in X$ (by rule number 1). So if you have an object $x$ then what you actually need to show is that $x\in X$, nothing else. That is of course under assumption that you already know that $f$ is a function, not some random formula that not necessarily yields a function (unfortunately formulas and functions are still being confused and incorrectly used as synonyms).
But I understand your confusion. Unlike functions, formulas may apply to wider range of objects. Indeed, $\varphi(n)=3\cdot n$ formula does apply to say $5\in\mathbb{Z}$. But $5\not\in 2\mathbb{Z}$ and therefore $f(5)$ is invalid (because the domain of $f$ is $2\mathbb{Z}$). But again, it all boils down to showing that $n\in2\mathbb{Z}$. If it holds then $f(n)$ is valid and unique.