let $M$ be a compact smooth manifold with dimension $4$ and $f:M\to \mathbb{R}^4$ be a smooth function.
Claim: $f$ is not an immersion.
my attempt: since $M$ is compact, there is $p\in M$ such that $f(p)=\max f$ where the maximum is taken from the all points in $M$. hence $df|_p=0$ since $df|_p=\sum_{i=1}^4 \frac{\partial f}{\partial x_i}|_pdx|_p$ and $\frac{\partial f}{\partial x_i}|_p=0$ for all $i=1,2,3,4$ since at $p$, $f$ attains its maximum. $x_i$'s are the local chart around $p$ in $M$.
now let $v, w\in T_pM$ such that $v\neq w$. but then $df|_p(v)=\sum_{i=1}^4 \frac{\partial f}{\partial x_i}|_pdx|_p(v)=\sum_{i=1}^4 0\cdot dx|_p(v)=0=\sum_{i=1}^4 0\cdot dx|_p(w)=df|_p(w)$, hence $df$ is not injective for all points in $M$, thus $f$ is not an immersion.
this is what I did, and I'm not sure if it's correct. I mean, at the first step, I chose the maximum naturally as if I'm in the Euclidean space, but the domain $M$ is just a general manifold. is it okay to do it this way? how would you do to prove this problem? like, is there any general approach?
edit: and what would happen if the dimensions of the domain and codomain don't match? I guess I didn't use the fact that they are the same.