Unit content
Work by a variable force in one dimension
The formula
$$W=F\Delta x$$
applies only when the force component along the motion is constant. If that component changes with position, the displacement can be divided into many short intervals over which the force is approximately constant.
For a force $F_x(x)$ acting along the $x$-direction, the small amount of work over displacement $dx$ is
$$dW=F_x(x),dx.$$
Adding these contributions from $x_i$ to $x_f$ gives
$$\boxed{W=\int_{x_i}^{x_f}F_x(x),dx}.$$
Thus work by a variable one-dimensional force is the signed area under the force-versus-position curve.
Sign of the accumulated work
Where $F_x$ and $dx$ have the same sign, the contribution to work is positive. Where they have opposite signs, it is negative.
A changing force can therefore do positive work over one part of a path and negative work over another. The total work is the algebraic sum of all contributions.
Worked example
Suppose a force varies with position according to
$$F_x(x)=2x\ \mathrm N$$
when $x$ is measured in metres. Find the work done as an object moves from $x=0$ to $x=3,\mathrm m$.
Using the integral,
$$W=\int_0^3 2x,dx.$$
Therefore
$$W=\left[x^2\right]_0^3=9,\mathrm J.$$
Using the final force value $F(3)=6,\mathrm N$ and multiplying by the full displacement would incorrectly give $18,\mathrm J$, because the force was smaller earlier in the motion.
Example with a restoring force
If a force has the form
$$F_x=-kx,$$
then moving from $x=0$ to $x=A>0$ gives
$$W=\int_0^A(-kx),dx =-\frac12kA^2.$$
The negative sign means the force transfers energy out of the object's mechanical motion as it moves outward in the positive $x$ direction.
Integration changes how the work is calculated, not what work means: the contributions of force along the displacement are still being accumulated over the motion.