Direct sum is isomorphic?

Given R, a PID, and two finitely generated R-modules A and B, suppose $\varphi: A \rightarrow B$ is a homomorphism.

If B is a free module, show that $A \cong \ker(\varphi) \oplus \mathrm{im}(\varphi)$.

I understand how to use the universal property of modules to do this if $\varphi$ was a surjective homomorphism, but I'm not sure how to do this without assuming that.

Assuming that by "free" you mean "free of finite rank": Consider following exact sequence $$0 \to \ker (\phi) \to A \to \mathrm{im}(\phi) \to 0.$$ Then note that, since $R$ is a PID, $\mathrm{im}(\phi)$ is also a free module.
• I see that this means the map $A \rightarrow \mathrm{im}(\phi)$ is surjective, but how does that show $A \rightarrow B$ is surjective? – user223236 Mar 13 '15 at 5:21
• I didn't say that $A \to B$ is surjective. Why do you need this one to be surjective? All you need is that the map $A \to \mathrm{im}(\phi)$ is surjective (which is by definition of functions) and $\mathrm{im}(\phi)$ is free (it is, since $R$ is a PID). – Krish Mar 13 '15 at 5:40