Do functions with the same gradient differ by a constant?

[Spoiler warning, this is more than a hint. I wanted to show this method because it avoids working with components.]


First suppose that $h:\mathbb R^n \to \mathbb R$ is differentiable and that $\nabla h(x) = 0$ for all $x \in \mathbb R^n$. I'll prove that $h$ is constant. Suppose (for a contradiction) that there exist points $a$ and $b$ in $\mathbb R^n$ such that $h(a) \neq h(b)$. Let $z:[0,1] \to \mathbb R$ be the function defined by $$ z(t) = h(a + t(b - a)). $$ Note that $z$ is continuous on $[0,1]$ and differentiable on $(0,1)$ and that $z(0) \neq z(1)$. By the mean value theorem, there exists a number $c$ such that $0 < c < 1$ and $$ z'(c) = z(1) - z(0) \neq 0. $$ But, by the chain rule, $$ z'(c) = \langle \nabla h(a + c(b -a)), b - a \rangle $$ which is $0$ because we are assuming that $\nabla h(x) = 0$ for all $x$ in $\mathbb R^n$. This is a contradiction. Therefore $h$ is constant.


Next, to solve the original problem, let $h = f - g$ and apply the above result.


If $\nabla f=\nabla g$, then $\frac{\partial f}{\partial x_k}=\frac{\partial g}{\partial x_k}$ for all $k\in\{1,\ldots,n\}$. Thus there exists $c_k(x_1,\ldots,x_{k-1},x_{k+1},\ldots,x_n)$ such that $f=g+c_k$. And, for $l\neq k$, we have $$ \frac{\partial c_k}{\partial x_l}=0 $$ and thus $dc_k=0$ and $c_k$ is a constant. The value of $c_k$ then does not depend of $k$ since for all $k,l$, $f-g=c_k=c_l$. Thus there exists a constant $c$ such that $f=g+c$.


HINT: Integrate both sides of

$df=\frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\cdots= \vec{\nabla f}\cdot \vec{dl}=\vec{\nabla g}\cdot \vec{dl}=dg$

EDIT: As pointed out by @peek-a-boo, this only works if the functions in question have an integrable derivative (so for example a sufficient condition is for $f$ to be $C^1$; i.e continuously differentiable).