The way the calculation works is a bit more subtle. Let's say the interval $[a,b]$ is given, and you want to calculate the work done by the force $F(x)$ as $x$ changes from $a$ to $b$.
We now do subdivide the interval $[a,b]$ into parts, which are usually equal (but sometimes inequal). This is done by placing additional points $a=x_0<x_1<x_2<\cdots<x_n=b$. For example, set $x_i=a+\frac{b-a}{n}i$.
What we do next is not exactly keeping $F(x)$ constant on the intervals $[x_i,x_{i+1}]$ - this is sort of a simplification of what we really do.
Instead, on each of those intervals we take the maximum and the minimum force: the actual work done on $[x_i,x_{i+1}]$ by real $F(x)$ must be between $(x_{i+1}-x_i)\min_{x_i\le x\le x_{i+1}}F(x)$ and $(x_{i+1}-x_i)\max_{x_i\le x\le x_{i+1}}F(x)$. (For pedants: we are really taking the supremum and infimum, not maximum and minimum, but I will use the symbols $\max$, $\min$ throughout.)
Now we sum those across $i=0,1,2,\ldots,n-1$ to get something we call lower Darboux sum and upper Darboux sum:
$$\sum_{i=0}^{n-1}(x_{i+1}-x_i)\min_{x_i\le x\le x_{i+1}}F(x)$$
$$\sum_{i=0}^{n-1}(x_{i+1}-x_i)\max_{x_i\le x\le x_{i+1}}F(x)$$
Now, if we can prove that those two sums converge to the same number as the division of $[a,b]$ grows finer and finer, then it is natural to call that number the work done by $F(x)$ on $x\in[a,b]$. Or, in maths, we call that the Riemann integral of $F(x)$ on interval $[a,b]$ and denote it:
$$\int_a^b F(x)dx$$
More precisely, if the upper and lower Darboux sums exist for every subdivision of $[a,b]$ and converge to the same value as the maximum size of the intervals in the subdivision tends to $0$, then we call the function $F$ Riemann-integrable, and we call the common limit of the two sums the Riemann integral of $F(x)$ over $[a,b]$. Otherwise, we say that $F$ is not Riemann-integrable.
Side note: If a function $F$ is Riemann-integrable on $[a,b]$, this automatically means that the method of keeping $F(x)$ constant on the intervals $[x_1,x_{i+1}]$ also works! Namely, as long as you pick $F(x)$ on each interval equal to some value of it on that interval (e.g. keep it equal to $F(x_i)$ or to $F(x_{i+1})$ or even to $\frac{1}{2}(F(x_i)+F(x_{i+1})$) - the sum you're getting is between the lower and the upper Darboux sums. That means that, having both the upper and lower Darboux sums converge to $\int_a^b F(x)dx$, your sum (squeeze theorem!) converges to the same value!
Back to the integration: why does it work in physics so well that you don't ever get to ask the question about Riemann integrability? I believe this mostly comes from the fact that (unlike some very hairy functions that we study in maths), in physics most functions are assumed to be well-behaved: normally continuous, often more than that (smooth - i.e. differentiable, often infinitely many times). So a theorem comes handy:
Theorem: Every continuous real function $F$ on $[a,b]$ is Riemann-integrable.
Proof (sketch): I won't go down to show the full proof here, but there is a bunch of prerequisites:
- Every continuous real function $F$ on a closed interval is bounded.
- This means, for every subdivision $a=x_0<x_1<x_2\cdots<x_n=b$ of $[a,b]$ both the lower and upper Darboux sum make sense.
- Every continuous real function $F$ on a closed interval is uniformly continuous.
- This is a condition often stronger than "just" being continuous, and it means that, if you pick a small $\epsilon>0$, you can always find the size $\delta>0$ of a small interval so that, whenever $|x-y|<\delta$ you have $F(x)-F(y)<\epsilon$. In other words, the values of $F$ come close "enough" to each other across the whole interval as the points on which you calculate $F$ come close "enough".
Because of the above uniform continuity, all of $\max_{x_i\le x\le x_{i+1}}F(x)$ and $\min_{x_i\le x\le x_{i+1}}F(x)$ are also smaller (or equal) than $\epsilon$ if we set $|x_i{+1}-x_i|<\delta$, so one can see that the upper and lower Darboux sums differ by at most $\epsilon(b-a)$. You see that you can now make that difference arbitrarily small by choosing $\epsilon$ arbitrarily small and then choosing the subdivision to be as fine as the corresponding $\delta$ (or finer).$\,\blacksquare$
This theorem is then extended in different directions:
- If a bounded function has finitely many discontinuities, it can still be proven to be Riemann-integrable.
- If a function is not bounded on $[a,b)$ but is Riemann-integrable on all $[a,c]$ with $a<c<b$, we may still try to define $\int_a^b F(x)dx:=\lim_{c\to b-}\int_a^c F(x)dx$ (if the limit exists) and we call that value an improper integral of $F$ on $[a,b)$. $b$ could be taken to be a finite real number or $+\infty$.
- There is a different definition of something called Lebesgue integral, which is a generalization of the previous two, although in fact it is more than "just" a generalization and covers many more pathological cases.
Anyways - the bottom line is that, as long as your force $F(x)$ is continuous (or has finitely many discontinuities), it is Riemann-integrable, and so your method works, and it produces the value of the Riemann integral $\int_a^b F(x)dx$, which (as we concluded above) is the only sensible number to call the "work done by this function".
Further reading: