Proving that a locally constant function is constant [duplicate]

How can I prove that if $$f: X \to Y$$ is locally constant and $$X$$ is connected then $$f$$ is constant?

Definition of locally constant: for all $$x_0$$ in $$X$$ there is a neighborhood $$U$$ of $$x_0$$ then for all $$x\in{U}$$ $$f(x)=f(x_0)$$.

• Consider $A=\{x\in X\mid f(x)=f(x_0)\}$. Is it open? Is it closed? – Thomas Shelby Jan 4 at 17:11

Take a connected component $$C \subseteq X$$ and $$a \in C$$. Denote $$b = f(a)$$ and let’s prove that $$f(x)=b$$ for $$x \in C$$. By hypothesis, $$U=\{x \in C \mid f(x)=b\}$$ is open in $$C$$. Now consider $$V=\cup_{Y \setminus \{b\}} V_y$$ where $$V_y$$ is an open subset of $$C$$ where $$f$$ is constant with value equal to $$y$$. $$V$$ is an open subset of $$C$$. $$V$$ is empty. If it was not, $$U,V$$ would be two non empty open subsets of $$C$$ with empty intersection. A contradiction with the fact that $$C$$ is a connected component.
$$f$$ is constant on all connected components of $$C$$ which is supposed to be connected. Hence $$f$$ is constant.