If derivative of a function is the zero function in $\mathbb R^n$, then the function is constant when the domain is path-connected

As pointed out in the comments, such a function cannot exist. In order to define “$f$ is differentiable”, you need your set to be open. Every pair of points in an open and connected set can be connected by a smooth path, see below. Your argument then shows that $f$ must be constant. (Alternatively, you can argue as Amitesh suggests)


Here's a proof of the fact that any two points of an open and connected set $U \subset \mathbb{R}^n$ can be connected by a piecewise smooth path:

Define an equivalence relation on $U$ by $x \sim y$ if and only if there is a piecewise smooth path $\gamma: [a,b] \to U$ such that $\gamma(a) = x$ and $\gamma(b) = y$.

Let $x \in U$ be arbitrary.

Notice that the equivalence class of $[x]$ of $x$ is open. If $y \in [x]$ there is some open ball $B_r(y) \subset U$. We may connect $y$ to any point $z \in B_{r}(y)$ using the straight line segment $(1-t)y + tz$, $t \in [0,1]$.

Since the complement of $[x]$ is a union of (open) equivalence classes, $[x]$ is closed. Thus, $[x]$ is open, closed and non-empty, hence all of $U$ by connectedness.


If you want to get “smooth paths” instead of only “piecewise smooth paths”, refine the argument slightly by allowing only paths $\gamma: [a,b] \to U$ such that $\gamma|_{[a,a+\varepsilon)} \equiv a$ and $\gamma|_{(b-\varepsilon,b]} \equiv b$ for some $\varepsilon \gt 0$. Then the concatenation of two such paths is still a smooth path. Instead of taking the straight line segment connecting $y$ and $z$ take a smooth function $f: [0,1] \to [0,1]$ such that $f|_{[0,\varepsilon)} \equiv 0$ and $f((1-\varepsilon, 1] \equiv 1$ and take the path $(1-f(t))y + f(t)z$.