Imagine I tell you: “Run to the end of the street, then run back here”. If you do nothing and you just stand there, then you aren’t following the instructions, even though the end result is the same as if you had followed the instructions. Keep this in mind as I do a quick side-step:
(
Side step:) Two functions are considered to be “the same” if they have the exact same domain, the exact same codomain, and they take the same value at each and every point in the domain. To specify a function
$f$, I need to specify
three things: the domain, the codomain, and the rule by which we assign an element of the codomain to each element of the domain. In calculus and pre-calculus, the codomain is almost invariably all real numbers. But the domain matters. The function
$f\colon [0,\infty)\to\mathbb{R}$ given by
$f(x)=x^2$, and the function
$g\colon\mathbb{R}\to\mathbb{R}$ given by
$g(x)=x^2$ are
different functions, because they have different domains.
But specifying the domain each and every time we want to consider a function is annoying. So we have a convention (an agreement): if we described a function with a formula, and we do not explicitly give a domain, we mean the “natural domain”, which is, essentially, “all real numbers for which the formula makes sense.” So the domain of $f(x)=\sqrt{x+1}$ is $[-1,\infty)$, because those are exactly the real numbers for which the formula makes sense.
The “instructions” for the function $f(x)=(\sqrt{x})^2$ are “first take the square root of the input, then square the result.” Even though that seems like it is the same as just “spitting out” the input $x$, that does not follow the instructions: the function that says “the result is the same as the input” is different. Under the convention mentioned above, the domain of $f(x)=(\sqrt{x})^2$ is “all real numbers for which the formula makes sense”, and that’s the nonnegative real numbers only. Whereas the function $g(x) = x$ has domain all real numbers (because that formula makes sense for all numbers). That means that this $f$ and this $g$ are different functions, because they have different domains.
See also this previous answer, and this one.