As pointed out in the comments by Niel_UK, this can be done with a 555 timer in the 'Astable' configuration. Although there are better ways to produce a square wave generator, a 555 timer is an excellent IC for a beginner (which it sounds like you are) so making circuits with this IC is a great idea.
A Google search on the Astable 555 timer will show you how to connect the external components:
Replacing R2 with a Potentiomenter will enable you to change the frequency by changing the voltage threshold on the discharge pin.
The next thing you need to do is work out the components you need to give yourself the frequency range you need. There are a few ways to do it, there are many online calculators you can use such as this one HERE that will let you put in the parameters you require and it will calculate the values needed. Or, if you are feeling adventurous, you could try and work it out yourself by using the formula f=1.44/(R1+2R2)C
If you rearrange the formula for R2, you can work out what values you will need to get 40Hz, then what value will be needed for 40kHz.
I would definitely recommend trying to solve it yourself first, as this is a part of electronics, being able to do the required maths so it is good practise! Use the online tool if your results either don't work out or you get too frustrated (in which case I would do some more research)!