Consider a system of ideal gas in a container with piston and the isobaric expansion of gas takes place.
As the process is isobaric, so initially the pressure of gas is equal to atmospheric pressure as the piston remains in equilibrium over the container. Then, first we increase the temperature and as a result pressure of gas increases and the piston rises till the pressure reduces to atmospheric pressure. So at initial and final state pressure of gas is constant thus the process is isobaric.
So, work done by gas should be
$dW=F_{applied\;by\;gas}(x).dx$
$dW=P_{applied\;by\;gas}(x)Adx$
$s.t.\;P_{applied\;by\;gas}(x_{initial\;state})=P_{applied\;by\;gas}(x_{final\;state})=P_{atm}$
So, total work done, $W=\int_{x_{initial}}^{x_{final}}P(x)Adx$
So, we should know pressure as a function of $x$ to calculate the work done by gas.
My question is that in books, they directly use the argument that work done by gas is dependent on $P_{external}$ and as it is constant, they give the expression of work as $dW=-P_{external}Adx$.
I don't understand how they do that and use external pressure in the expression of work done.
I think we should take into account pressure of gas as a function of $x$ to find the work done by gas.
Please help me in clarifying the doubt.