Formatting the memory card on the D40 is very easy.
Go in to the menu, by pressing the button MENU (obviously). Scroll down until you get to the little spanner icon, which is the SETUP MENU.
Formatting the memory card is the second option on this menu, scroll over to it and click ok to be greeted with the warning:
Confirm to format the memory card.
And then 2 seconds later: Voila!
Empty memory card.