When is it possible to extend a continuous function defined on a dense set on X to all the space X. Let $X $ and $Y $ be topological spaces, $D \subseteq X $ a dense set of $X $ and  $f: D \rightarrow Y$ a continuous function. My question  is:  Is  it  always  possible to define $g:X \rightarrow Y $ such  that  g  is continuous and  $g (x) = f (x) $ for  all $x \in D $?
 A: No, consider the function $f:\mathbb Q \rightarrow \mathbb R: x \mapsto -1$ if $x < \sqrt 2$ and $x \mapsto 1$ if $x>\sqrt 2$. 
It can be seen to not extend to $\mathbb R$. 
This is because extension of a map $D \rightarrow Y$ from a dense subset of $X$ to the whole space is unique if $Y$ is hausdorff. Thus we can deduce that if $f':\mathbb R \rightarrow \mathbb R$ is an extension of $f$, $f'$ would have to be locally constant except at $\sqrt 2$. 
Thus we can deduce that $f'$ is constant on the two connected components of $\mathbb R - \{\sqrt 2\}$. 
Now we divide the problem into a few cases.
Assume that $f'(\sqrt2)=1$. Then $f^{-1}((1- \epsilon, 1 + \epsilon)) = [\sqrt(2), \inf)$ and if we  make a similar argument for the case $f'(\sqrt 2) = -1$ we deduce that $f'(\sqrt 2) \neq -1$ and $f'(\sqrt 2) \neq 1$ since preimages of open sets don't yield open sets under these assumptions. 
Now working under the assumption that $f'(\sqrt 2) \neq 1 $ nor $ -1 $ consider the preimage of the set $(f'(\sqrt 2)-\epsilon, f'(\sqrt 2) + \epsilon)$ which we know has to be equal to $\{\sqrt 2\}$ which is not open. This covers all posibilities for what $f'(\sqrt 2)$ may equal and none of them make $f'$ continuous. Thus there is no extension of $f$ to $\mathbb R$.
