Use residues to evaluate $\int_{0}^{\infty} \frac{dx}{x^2 + 1}$ 
Use residues to evaluate $\int_{0}^{\infty} \frac{dx}{x^2 + 1}$.

Okay so these are the integrals in complex analysis I am a little uncomfortable with. I purposely chose a simple problem out of a book so that I can save the slightly more difficult problems for when I actually understand them. The answer to this problem is $\frac{\pi}{2}$. Could someone please explain to me how to solve improper integrals in complex analysis using the problem I have provided as an example. I would appreciate it very much. 
 A: The given function is even, so we can actually compute $$\int_{-\infty}^{\infty} \frac{dx}{x^2 + 1}$$
and divide by $2$. Define $f(z) = \frac{1}{z^2 + 1}$; this is analytic with simple poles at $z = \pm i$. 
Define a contour $\gamma_R$ to be the line segment between $-R$ and $R$, together with a semicircle $S_R$ around $0$ of radius $R$. Then
$$\int_{-R}^{R} \frac{dx}{x^2 + 1} = \int_{\gamma_R} f(z) dz - \int_{S_R} f(z) dz \,\,(*)$$
Assuming that $R > 1$, we can compute the first integral explicitly from the residue theorem: $f$ has a simple pole at $z = i$, with residue
$$\operatorname{Res}_{i} f(z) = \operatorname{Res}_{i} \frac{1}{(z + i)(z - i)} = \frac{1}{2i}$$
So the first integral is
$$2 \pi i \frac{1}{2i} = \pi$$
On the other hand, if $|z| = R$ is sufficiently large, then
$$|f(z)| \le \frac{1}{\frac{1}{2} R^2} = \frac{2}{R^2}$$
So the second integral can be estimated (arc length) (value of $f$), leading to
$$\left|\int_{S_R} f(z) dz\right| \le \pi R \frac{2}{R^2} = \frac{2\pi}{R}$$
This tends to $0$ as $R$ grows, and so taking a limit in $(*)$ leads to
$$\int_{-\infty}^{\infty} f(x) dx = \pi$$
