If $X$ is a connected metric space, then a locally constant function $f: X \to $ M, $M $ a metric space, is constant

Generally when you have to make use of the connectedness assumption, the basic framework of the proof is always to first define a subset $U\subset X$ on which a property is true, and show that $U$ is both open and closed (in the topology of $X$), and is nonempty. If $U$ is closed, then $U^c$ is open. And $U \cup U^c = X$ is a disjoint union of open sets covering $X$, which contradicts the assumption that $X$ is connected unless one of the two sets $U$ and $U^c$ is empty.

So now, pick an arbitrary $x_0$ in $X$, let $U$ be the subset

$$ U:= \{ y\in X | f(y) = f(x_0) \} $$

Clearly $x_0\in U$, so $U$ is non-empty. The definition of locally constant immediately implies that $U$ is open. (If $x\in U$, then the neighborhood on which $f$ is locally constant is in $U$.)

It remains to show that $U$ is closed. To do so we use that $f$ is necessarily continuous. The definition of locally constant says that for any point $y\in X$, there exists some open neighborhood $N_y\subset X$ on which $f(y)$ is constant. It is easy to see that the same neighborhood can be used as the "$\delta-\epsilon$" neighborhood for continuity.

Then since $f$ is a continuous function, the set $U$ which is defined by an equality condition must be closed (take a limit of a sequence of points $x_n$ in $U$ converging to $x\in X$, $f(x_0) = \lim f(x_n) = f(x)$).


This question has been asked a long time ago, but the accepted answer is not as general as it could be, so I add a proof here for future readers.

A function $f : X \to Y$ where $X$ is a topological space and $Y$ is a set is called locally constant if every point $x \in X$ admits a neighborhood $U$ where $f|_U$ is constant. An equivalent way to state this is that $f$ is continuous when $Y$ is equipped with the discrete topology ; saying that every point admits a neighborhood where the function is constant is equivalent to saying that $f^{-1}(f(x))$ is open for every $x \in X$.

So suppose $X$ connected. Then for $y \in Y$, $f^{-1}(y)$ is open, and it is also closed since $X \backslash f^{-1}(y) = f^{-1}(Y \backslash \{y\}) = \bigcup_{z \in Y \backslash\{y\} } f^{-1}(z)$ is a union of open sets in $X$. Since it is clopen and $X$ is connected, $f^{-1}(f(x)) = X$ for any $x \in X$, that is, $f$ is constant.

In particular, a locally constant function $f : X \to Y$ is a function which is constant on the connected components of $X$, since the restriction of a locally constant function to a subspace is locally constant (use the fact that inclusion maps are continuous and use the topological definition of locally constant given above). Note however that the connected components need not be one of those neighborhoods where the function is constant as in the original definition, since they are not necessarily open.

To know exactly on what open sets $f$ has to be constant, consider the following equivalence relation : for $x,y \in X$, we write $x \equiv y$ if and only if there exists a connected subset $C \subseteq X$ with $x,y \in C$. Since the intersection of two connected subsets with non-empty intersection is connected, $\equiv$ is an equivalence relation whose partition of $X$ gives the connected components of $X$. Consider the space $X/\equiv$ of equivalence classes, i.e. we collapsed each connected component to a point ; equip $X/\equiv$ with the quotient topology. Then a locally constant function $f : X \to Y$ (i.e. a continuous map with $Y$ discretE), being constant on connected components, factors through a continuous map $\widetilde f : X/\equiv \to Y$. Since in $X/\equiv$, the connected components are points (we say that $X/\equiv$ is totally disconnected), but the topology on $X/\equiv$ might not be trivial ; a locally constant function $f$ is precisely one for which $\widetilde f$ is continuous, i.e. for every $[x] \in X/\equiv$, $f$ is constant on $\pi^{-1}(U)$ for some open neighborhood $U$ of $[x]$ in $X/\equiv$.

Think of a locally constant function on $\mathbb Q$ (with the subspace topology from the reals). Even though the connected components are points, not every function on $\mathbb Q$ is locally constant.

Hope that helps,