One assumption in the proof of one result of Hahn-Banach theorem. Theorem: Let $X$ be a locally convex space, let $M$ be a linear subspace of $X$, and let $f\in M^*$. Then there exists $h\in X^*$ such that $h(x)=f(x)$ for all $x\in M$.
I saw one sentence in the proof that I don't understand. It is:
Replacing $M$ by $\overline{M}$ and replacing $f$ by its unique continuous linear extension to $\overline{M}$, we can assume that $M$ is closed in $X$.
My question is what is the reason to replace $M$ and $f$ and why we can assume that $M$ is closed in $X$?
Thank you in advance!

First, let's handle the technical problem (why can we assume $M$ is closed and $f$ is continuous on $M$?)  Recall that the topology on any locally convex space is generated by some family of seminorms $p_\alpha$.  Thus, a function $f$ is continuous on $M$ if and only if
$$|f(x-y)| \leq \sum_{i=1}^n c_i p_{\alpha_i}(x-y)$$
for soem finite $n$ and $\alpha_1,\cdots,\alpha_n$.  Now, let $x \in \overline{M}$ and $(x_\kappa)_\kappa$ a Cauchy net in $M$ converging to $x$.  Then, $f(x_\kappa)$ is a Cauchy net in $\mathbb{R}$, so it converges to some limit, call it $f(x)$.  
Now, to see that this extension of $f$ is continuous, let $(x_\mu)_\mu$ be another Cauchy net converging to $x$.  Then,
$$\begin{align*}|f(x-x_\mu)| &\leq |f(x) - f(x_\kappa)| + |f(x_\kappa - x_\mu)|\\
& \leq |f(x) - f(x_\kappa)| + \sum_{i=1}^n c_i p_{\alpha_i}(x_\kappa - x_\mu)\\
&\leq |f(x) - f(x_\kappa)| + \sum_{i=1}^n c_i p_{\alpha_i}(x - x_\kappa) + \sum_{i=1}^n c_i p_{\alpha_i}(x - x_\mu)\end{align*}$$
The first term on the right can be made arbitrarily small by choosing $\kappa$ appropriately by the definition of $f(x)$.  Likewise, the second and third terms can be made small in the limit (of nets) since $(x_\kappa)_\kappa$ and $(x_\mu)_\mu$ are both Cauchy nets converging to $x$.  
As to why we want to do this, it helps to remember the proof strategy: we want to argue that if $M$ is a strict subspace of $X$ with a continuous linear functional $f$ defined on it, then there always exists a superspace $N \supsetneq M$ and a continuous functional $g$ with $g|_{M} = f$ (Zorn's lemma then tells us that $h$ exists).  We construct $N$ as a codimension $1$ extension of $M$: that is, we choose some $x \not\in M$, define $N = M + \operatorname{span}(x)$, and define $f(x)$ somehow and extend by linearity.  
If $M$ is closed, then we are free to choose $f(x)$ however we like (and various refined versions of the theorem choose $f(x)$ so that the global extension $h$ has some nice geometric/analytic properties).  If $M$ is open, however, we are not be able to choose $f(x)$ freely if $x \in \partial M$, so we would have to define $f(x)$ by continuity anyways.
