Work done by system of ideal gas in isobaric expansion 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.
At the interface between the gas and its surroundings, by Newton's 3rd law, the pressure exerted by the gas on its surroundings is equal to the pressure exerted by the surroundings on the gas.  So we can use either.  For a reversible expansion, the gas pressure is given by the ideal gas law.  But the ideal gas law only applies at thermodynamic equilibrium and during a reversible process (which consists of a continuous sequence of thermodynamics equilibrium states).  For an irreversible expansion, the ideal gas law doesn't work (i.e., gives the wrong answer), and we can't use it to get the gas pressure at its interface with the surroundings (where the work is being done).  But, if we know the surroundings pressure during the irreversible expansion, we can still calculate the work (because, at the interface between the gas and surroundings), the two will match.  So using the external pressure is always a valid approach to getting the work.