Let $x$ be a positive integer.
Is there a mathematical formula for $$f(x)=\text{nearest square to } x \text{ }(\text{in terms of } x)?$$
I tried searching for related questions in MSE and found this one, but my inquiry is not covered there.
I also tried searching via Google and found this closely related question in StackOverflow.
I used the search string "nearest-square function".
By virtue of the accepted answer in your linked question, you can take the nearest integer function of $\sqrt x$ and square it. However it may not be obvious (or seemingly too obvious to be true) so here are more details:
The nearest integer function (for $x \notin \{k+\frac12| k \in \mathbb Z\}$ to avoid ambiguities) is equal to $\left\lfloor x + \frac12\right\rfloor$. Fortunately for the square root, it is never an integer plus half. Hence we can also write
$$f(x) = \left\lfloor \sqrt x + \frac12\right\rfloor^2$$
To prove that this formula works, we consider the cases (where $n \in \mathbb N \cup \{0\}$):
Case 1: $n^2 \le x \le n^2+n < (n+\frac12)^2$
Case 2: $(n+\frac12)^2< n^2+n+1 \le x < (n+1)^2$
and we see that both cases work perfectly fine, given that $x$ is a positive integer.