Is there a deep reason why replacing $\cos(x)$ with $e^{ix}$ and taking the real part often makes a contour integral work out? I'm grading a complex analysis course right now and it turns out to involve a lot of contour integration.  For instance, students are asked to find the integral
$$\int_0^\infty \frac{\cos (ax)}{(x^2 + b^2)^2} \, dx$$
where $a, b > 0$ are real parameters.  This can be done as follows:
\begin{align}
\int_0^\infty \frac{\cos (ax)}{(x^2 + b^2)^2} \, dx
& = \frac{1}{2} \int_{-\infty}^\infty \frac{\cos (ax)}{(x^2 + b^2)^2} \, dx \\ \\
&= \frac{1}{2} \operatorname{Re} \left[\int_{-\infty}^\infty \frac{e^{iax}}{(x^2 + b^2)^2} \, dx\right] \\ \\
&= \frac{1}{2} \operatorname{Re} \left[\lim_{R \to \infty} \int_{-R}^R \frac{e^{iax}}{(x^2 + b^2)^2} \, dx\right].
\end{align}
Now
$$\int_{-R}^R \frac{e^{iax} \, dx}{(x^2 + b^2)^2} = \oint_{L_R+C_R} \frac{e^{iaz}\, dz}{(z^2 + b^2)^2} - \oint_{C_R} \frac{e^{iaz} \, dz}{(z^2 + b^2)^2}$$
where $L_R$ is the line segment going from $-R$ to $R$ and $C_R$ is the circular arc with center 0 going from $R$ to $-R$.
By the residue theorem,
$$\oint_{L_R+C_R} \frac{e^{iaz}\, dz}{(z^2 + b^2)^2} = 2 \pi i \operatorname{res}_{z=bi} \left[\frac{e^{iaz}}{(z^2 + b^2)^2}\right] = \frac{\pi e^{-ab} (1 + ab)}{2 b^3}$$
for $R > b$.  Further, by Jordan's lemma
\begin{align}
\left|\left|\oint_{C_R} \frac{e^{iaz} \, dz}{(z^2 + b^2)^2}\right|\right|
&\leq \frac{\pi}{a} \max_{z \in C_r} \left|\left|\frac{1}{(z^2 + b^2)^2}\right|\right| = \frac{\pi}{a(R^2 + b^2)^2} \to 0
\end{align}
as $R \to 0$, from which we see that
$$\int_0^\infty \frac{\cos (ax)}{(x^2 + b^2)^2} \, dx = \frac{\pi e^{-ab} (1 + ab)}{4 b^3}.$$

Suppose, however, that instead of writing $\cos(a x)$ as $\operatorname{Re}[e^{i x}]$ and pulling the $\operatorname{Re}$ outside the integral we'd tried to do things the same way directly.  The problem in this case is that the integral
$$\oint_{C_R} \frac{\cos(az)}{(z^2 + b^2)^2}$$ doesn't vanish as $R \to \infty$.  By adding an extra term to our equation, though, we managed to make things work out quite nicely.

If I try to distill the general technique here, it's something like this: We had a function $f(z)$ for which 
$$\lim_{R \to \infty} \oint_{C_R} f(z) \, dz$$
didn't vanish, so we found a function $g(z)$ which vanished along $L_R$ and for which
$$\lim_{R \to \infty} \oint_{C_R} f(z) + g(z) \, dz$$
vanished.  However, it seems incredibly fortuitous that we had such a readily available choice for $g$.  Is this just a consequence of picking a homework problem that can actually be done with pencil in paper in a reasonable amount of time?  Or is something deeper going on?
Specific questions:


*

*Given a function $f$, is there any nice way of rephrasing the condition that
$$\oint_{C_R} f(z)\, dz \to 0 \text{ as } R \to \infty$$ as a more straightforward property of $f$?  (At first I wondered if $C_R$ was "a closed curve around $\infty$ in the limit" but this doesn't seem to be right.) 

*Given a meromorphic function $f$ such that $$\oint_{C_R} f(z)\, dz \not \to 0 \text{ as } R \to \infty,$$ can we always find a meromorphic function $g$ such that $g|_\mathbb{R} = 0$ and $$\lim_{R \to \infty} \oint_{C_R} f(z) + g(z) =0?$$

*If not, are there any nice constraints on $f$ that make it possible?  If so, how many such $g$ are there, and can we construct one of them easily?

*Same questions as above with $L_R$ and $C_R$ replaced by more general curves.

 A: Yes, there is a reason. The exponential $e^{iz}$ is bounded in the upper half plane with $|e^{iz}| = e^{-\Im z}$, which tends to $0$ uniformly at $\Im z\rightarrow\infty$. And $e^{-iz}$ is bounded and decays in the lower half plane. However, $\cos(z)$ is not bounded in either half plane.
If you have a bounded real differentiable function $g(t)$ on the real axis, then you can define
\begin{align}
    H(z)  & = \frac{1}{\pi}\int_{-\infty}^{\infty}g(t)\frac{1+tz}{t-z}\frac{dt}{1+t^{2}} \\
          & = \frac{1}{\pi}\int_{-\infty}^{\infty}g(t)\left[\frac{1}{t-z}-\frac{t}{1+t^{2}}\right]dt.
\end{align}
$H$ is holomorphic in $\mathbb{C}\setminus\mathbb{R}$ and $\overline{H(z)}=H(\overline{z})$. For $y > 0$,
$$
              \frac{H(x+iy)-H(x-iy)}{2i}=\frac{1}{\pi}\int_{-\infty}^{\infty}\frac{g(t)y}{(t-y)^{2}+y^{2}}dt.
$$
Therefore,
$$
                  \lim_{y\downarrow 0}\Im H(x+iy)=g(x)
$$
The function $H$ is bounded on $|\Im z| \ge \epsilon > 0$. So if you start with a function $g$ that has a bounded holomorphic extension to a strip $|\Im z| < \epsilon$ for some $\epsilon > 0$--such as with $\cos(t)$--then you get this kind of thing in general where there is a bounded holomprhic function $H_{+}(z)$ on a neighborhood of the upper half plane, and a bounded holomorphic function $H_{-}(z)$ on a neighborhood of the lower half plane such that $\Re H_{+}(t)=\Re H_{-}(t) = g(t)$.
