Why doesn't trig substitution work for definite integrals? In the following example, I am attempting to find the area of a semicircle using calculus, which is obviously $\frac{\pi{r}^2}{2}$. Effectively, I am trying to find
$$\int_{-r}^r\sqrt{r^2-x^2}dx$$
Here goes:
Let $x=r\sin\theta$:
$$\frac{dx}{d\theta}=r\cos\theta\implies dx=r\cos\theta d\theta$$
When:
$$x=r,  ~~~~\text{Then}~~~~\sin\theta=1\implies\theta=\frac{\pi}{2}$$
$$x=-r,  ~~~~\text{Then}~~~~\sin\theta=-1\implies\theta=-\frac{\pi}{2}$$
$$\therefore\int_{-r}^r\sqrt{r^2-x^2}dx=\int_{-\frac{\pi}{2}}^{\frac{\pi}{2}}r\cos\theta\sqrt{r^2-r^2\sin^2\theta} ~~d\theta$$
$$=\int_{-\frac{\pi}{2}}^{\frac{\pi}{2}}r^2\cos^2\theta~~d\theta=\frac{r^2}{2}\int_{-\frac{\pi}{2}}^{\frac{\pi}{2}}(1+\cos2\theta)~~d\theta=\frac{r^2}{2}\left[\theta+0.5\sin2\theta\right]_{-\frac{\pi}{2}}^{\frac{\pi}{2}}=\frac{r^2}{2}((\frac{\pi}{2}+0)-(-\frac{\pi}{2}-0))=\frac{\pi r^2}{2}$$
as required.

BUT
Back to the stage when I was working out the limits of the new integral with respect to $\theta$. Let's say I wrote something like this:
When:
$$x=r,  ~~~~\text{Then}~~~~\sin\theta=1\implies\theta=\frac{5\pi}{2}$$
$$x=-r,  ~~~~\text{Then}~~~~\sin\theta=-1\implies\theta=-\frac{5\pi}{2}$$
Then , I would get an eventual answer of
$$\frac{5\pi r^2}{2}$$
Where have I gone wrong? It seems to me that my logic is faultless, but the definite integral is simply ambiguous when using trig substitutions. This would apply to any integral involving trig substitutions; in short, can't all definite integrals computed using trig substitutions generate an incorrect answer?
If so, isn't the maths of definite integrals faulty? Thanks for your help.

Ideally, as others have pointed out, you will set up the bounds of your trigonometric substitution so that each value of $x$ you need is produced once and only once, in a continuous stream.
For $-\frac\pi2 \leq \theta \leq \frac\pi2,$
if $x = r\sin\theta$ then $x$ increases monotonically from $-r$ to $r$
as $\theta$ increases monotonically from $-\frac\pi2$ to $\frac\pi2.$
This makes a very "clean" substitution.
(Note: throughout this answer I am assuming $r > 0.$ In the case where $r<0$ then $\int_{-r}^r\sqrt{r^2-x^2}dx$ is negative and is not the answer to the initial problem, "What is the area of a semicircle?")
When you increase $\theta$ from $-\frac{5\pi}2$ to $\frac{5\pi}2,$
the value of $x$ starts at $-r,$ increases to $r,$ then decreases back to $-r$,
then increases to $r$ again, decreases to $-r$ again, and finally increases to $r.$
That's a lot of increasing and decreasing just to cover the distance from $-r$ to $r.$
But the up-and-down-and-up movement of $x$ is not really the problem.
What is the problem is that your substitution is not correct over the
entire domain $-\frac{5\pi}2 \leq \theta \leq \frac{5\pi}2.$
In particular, look at this equation on which you rely
(where I have written $a$ and $b$ as the bounds of the interval of integration,
since you propose to use the same method from $-\frac{5\pi}2$ to $\frac{5\pi}2$
as for $-\frac{\pi}2$ to $\frac{\pi}2$):
$$\int_a^b r\cos\theta \sqrt{r^2-r^2\sin^2\theta} \,d\theta
=\int_a^b r^2\cos^2\theta\,d\theta.$$
In order to justify this equation, you must show that
$\sqrt{r^2-r^2\sin^2\theta} = r\cos\theta.$
That is easily proved when $\cos\theta \geq 0,$
but it is false when $\cos\theta < 0.$
When $\cos\theta < 0,$ the correct equation is
$$\int_a^b r\cos\theta \sqrt{r^2-r^2\sin^2\theta} \,d\theta
=\int_a^b -r^2\cos^2\theta\,d\theta.$$
Alternatively, you could combine the two equations as
$$\int_a^b r\cos\theta \sqrt{r^2-r^2\sin^2\theta} \,d\theta
=\int_a^b -r^2\cos\theta \lvert\cos\theta\rvert \,d\theta,$$
but the integral of $\cos\theta \lvert\cos\theta\rvert$ is not the same as the integral of $\cos^2\theta,$ so you still have some work to do to sort things out.
Here's how the integral can be correctly integrated from
$-\frac{5\pi}2$ to $\frac{5\pi}2$:
\begin{align}
\int_{-r}^r \sqrt{r^2-x^2}\,dx
&= \int_{-5\pi/2}^{5\pi/2} r\cos\theta\sqrt{r^2-r^2\sin^2\theta} \,d\theta\\
&= \int_{-5\pi/2}^{5\pi/2} r^2(\cos\theta)\lvert\cos\theta\rvert \,d\theta\\
&= \int_{-5\pi/2}^{-3\pi/2} r^2\cos^2\theta \,d\theta\\
&\qquad +  \int_{-3\pi/2}^{-\pi/2} -r^2\cos^2\theta \,d\theta\\
&\qquad +  \int_{-\pi/2}^{\pi/2} r^2\cos^2\theta \,d\theta\\
&\qquad +  \int_{\pi/2}^{3\pi/2} -r^2\cos^2\theta \,d\theta\\
&\qquad +  \int_{3\pi/2}^{5\pi/2} r^2\cos^2\theta \,d\theta\\
&= \frac{\pi r^2}{2} -  \frac{\pi r^2}{2} +  \frac{\pi r^2}{2}
  -  \frac{\pi r^2}{2} +  \frac{\pi r^2}{2} \\
&= \frac{\pi r^2}{2},
\end{align}
using the fact that $\lvert\cos\theta\rvert = -\cos\theta$
when $\cos\theta \leq 0.$
You actually get the correct answer, but only if you integrate the correct function over the entire interval.
Also notice that each time $\sin\theta$ decreases from $1$ to $-1$
(that is, each time $x$ decreases from $r$ to $-r$) you precisely wipe out the amount you integrated on the previous increase.
In effect, by allowing $x$ to go up and down all these times, you end up integrating
\begin{multline}
\int_{-r}^r \sqrt{r^2-x^2}\,dx
 + \int_r^{-r} \sqrt{r^2-x^2}\,dx
 + \int_{-r}^r \sqrt{r^2-x^2}\,dx\\
 + \int_r^{-r} \sqrt{r^2-x^2}\,dx
 + \int_{-r}^r \sqrt{r^2-x^2}\,dx,
\end{multline}
in which the first four integrals cancel each other out.
