You know that work done is
Hence if the force is not constant you have to split the interval up into many pieces. Basically, the work done by moving the object from 1m to 2m is equal to the work done by moving it from 1m to 1.1 plus work done from 1.1 to 1.2. etc. And for that interval of 0.1m u can assume the force is constant (the numbers are an unrealistic example, but i hope u get the idea). Basically you want to take the limit as the interval approaches zero, and you can see that this is analogous to finding an integral(which i know that you are aware of judging from a post u made when u presented that definition).
Consider this question as an example:
What is the work done when u stretch a spring from x=1 to x=2. (where x is the extension beyond natural length).
You know that initially the force is is
, then it is
then in the next interval
and finally on the last intervral it is
.
Hence to get the total work we need to multiply each force by each distance, (i have factorised the distance and put
at the end because I am lazy and forgot to put it in earlier):
Which you can solve using the formula for an arithmetic sequence, or just recognize that this is the integral:
(if lower terminal was 0 you can see that it is simply the formula for elastic potential energy).
This should give you a clue as to how to approach the gravity problem.
This may be beyond the course, but I think it's a pretty good way to deepen your understanding of integration and appreciate how Newton saw it as necessary to calculate stuff.