It is possible to have only 1-sided inverses, which will be the case if your function is injective but not bijective, and viceversa. If f is 1-1 , then it will have a left inverse g,
i.e., g is such that gof=Id , i.e., gof(x)=x.
Basically, you define g(x) to be the left-inverse, i.e., g(f(x)):=x , for those elements in the range, and define it anyway you want for those elements not in the range.
e.g., take the map from the non-negative integers to themselves, that doubles every number, i.e., f: n-->2n . Then n is 1-1 , but not onto (misses all odd numbers). Define, then g: 2n->n , for even numbers in Z, and any other way for odd numbers. Then gof(n)=n .
A similar argument shows that if f:X-->Y is onto, then f has a right-inverse, i.e., there is h(y) with h:Y-->X , and foh(y)=y. Basically, for all y in Y,the fiber { f^{-1}(y)} of y is non-empty. Map, then , h:y-->yo , i.e., each element of y into an element yo of its fiber*, and , then, almost tautologically, foh(y)=f(yo)=y. Think of , e.g., Sinx from
the reals into [-1,1]. So f(x)=sinx . Define h to send yo in [-1,1] into some element in its fiber :{Sin^{-1}(yo)} .
* Note: if you have not done set theory, feel free to ignore the following:To be more rigorous, you need the axiom of choice to define the right-inverse (not sure about the left inverse; it is too late, i.e., to choose an element in each fiber, when X,Y are infinite). If you're not seeing this, it is not necessary for your understanding.