Regular functions on $\Bbb A^n$. Let $k$ be algebraically closed and let $\Bbb A^n_k$ be affine $n$-space over $k$.
A regular function on $U\in\mathcal{T}_{\Bbb{A}^n}$ is a function $\varphi:U\to k$ such that for each $p\in U$ there exists


*

*$U_p\in\mathcal{T}_{\Bbb A^n}$ where $p\in U_p$ and

*Two polynomials $f,g\in k[x_1,\dots,x_n]$ where for every $q\in U_p$, $g(p)\ne 0$ and $\varphi(q)=f(q)/g(q)$.


I want to show that the collection of all regular functions on $\Bbb A^n_k$ is just $k[x_1,\dots,x_n]$. 
If $\varphi$ is such a regular function, then locally it is of the form $f/g$. But because it is a different $g$ locally everywhere, I cannot simply argue that $\varphi=f/g$ and $g$ must be nonvanishing everywhere on $\Bbb A^n_k$, which would imply $g$ is a constant not equal to zero.
How do I do this?
 A: This is more or less a corollary of $k[x_1,\ldots,x_n]$ being factorial. Let $k[X]$ and $k(X)$ be shorthands for this ring and its fraction field.
Suppose $\alpha$ is a global regular function, and on some nonempty open $U$, $\alpha=\frac{f_1}{g_1}$, and on another nonempty open $V$, $\alpha=\frac{f_2}{g_2}$. Then on $U\cap V$, which is dense since $\Bbb{A}^n$ is irreducible, $\frac{f_1}{g_1}=\frac{f_2}{g_2}$ as regular functions, which implies $f_1g_2-f_2g_1=0$ as polynomials by density of $U\cap V$. Thus if I assume that $\frac{f_1}{g_1}$ and $\frac{f_2}{g_2}$ are in lowest terms by using factoriality of $k[X]$, we have that $\frac{f_1}{g_1}=\frac{f_2}{g_2}$ as elements of $k(X)$, so $f_1=f_2$ and $g_1=g_2$ (up to scalars) since both fractions are in lowest terms. Hence the representation $\alpha =\frac{f_1}{g_1}$ extends to any other open subset on which $\alpha$ can be represented by a rational function. Then either using quasicompactness or Zorn's lemma or Noetherianness, it follows that in fact the representation $\alpha=\frac{f_1}{g_1}$ extends to all of $\Bbb{A}^n$. Since $\frac{f_1}{g_1}$ is in reduced terms, it follows that $V(g_1)$ is empty, hence $g_1$ is a constant (WLOG 1), and hence $\alpha = f_1$ is a polynomial function.
