Let:
$r=\sqrt{a^2+p^2-2ap \cos \theta}$
where $a,p,\theta$ are independent variables
$$\int_{a-p}^{a+p} a\ f(r) dr=\int_0^\pi a\ f(r) \dfrac{a\ p\ \sin \theta}{r} d\theta=a \int_0^\pi f(r) \dfrac{a\ p\ \sin \theta}{r} d\theta= a \int_{a-p}^{a+p} f(r) dr$$
Now I have arrived at an equation which is obviously incorrect.
$$\int_{a-p}^{a+p} a\ f(r) dr= a \int_{a-p}^{a+p} f(r) dr$$
$r$ is a function of $a$ and therefore $a$ cannot be taken outside the definite integral with respect to $r$.
Why does this contradiction arise. Am I missing anything. Please point out.
The problem isn’t in anything you did along the way. $a$ already occurs outside the integral at the very beginning, since the limits of the integral (which contain $a$ and $p$) are also outside the integral and should contain only free variables and not the integration variables.
So the question is what you originally meant by $\int_{a-p}^{a+p}af(r)\mathrm dr$. It’s hard to make sense of this under your interpretation of $a$,$p$ and $\theta$ as independent variables on an equal footing, since we should then be integrating over three variables, not over one that is a function of the three.
You seem to be integrating over the distance $r$ on a circle of radius $a$ from a point at distance $p$ from the centre. If so, it would seem to make sense to regard either the angle $\mathrm\theta$ along the circle or the distance $r$ as free variables and the radius $a$ of the circle and the distance $p$ of the point from the centre as fixed parameters that describe the situation. Then the dependence of the integrals on $a$ and $p$ would make sense.
But that’s just a guess; the main point of the answer is that this interpretational question doesn’t come up at the end of your calculations, but already at the beginning.