I found this line about the condition of inverse function at Inverse function.Here is the line.
In the verification step we technically really do need to check that both $\left( {{f} \circ f^{-1}} \right)\left( x \right) = x$ and $\left( {{f^{ - 1}} \circ f} \right)\left( x \right) = x$ are true. For all the functions that we are going to be looking at in this course if one is true then the other will also be true. However, there are functions (they are beyond the scope of this course however) for which it is possible for only one of these to be true. This is brought up because in all the problems here we will be just checking one of them. We just need to always remember that technically we should check both.
Can someone give me an example of such a function?
Indeed, $\tan(\tan^{-1} x)=x$ is true for all real $x$, but $\tan^{-1}(\tan x)=x$ is only true for $x\in(-\frac {\pi}{2}, \frac {\pi}{2})$. In general, both the conditions will simultaneously hold only in the intersection of the principal domain of $f(x)$ where it is bijective, with the domain of $f^{-1}(x)$.
One might say a similar thing for $f(x)=e^x$ and $g(x)=f^{-1}(x)=\ln x$.
$g(f(x))=x$ is true for all real $x$, but $f(g(x))=x$ holds only for $x\in (0,\infty)$.