I'm looking to buy a flashgun for my Nikon D40 and wondered if anyone could recommend any. I'm on a budget and if I had the money I would go for the SB900, but is there a similar one for a smaller price?
Nikon SB-600 is an excellent flash for the price (much cheaper than a SB-900) and I've been very happy with the performance of mine.
If you're looking for TTL capabilties, then I've had decent luck with the Yongnou products and would probably recommend a YN-465 which is extremely reasonably priced (available on Ebay and Amazon typically).
If you're fine with a manual flash, try a YN-462, the more powerful YN-560, or the highly reputed Lumopro 160 (sold only through Midwest Photo Exchange I believe).
Edit: I only have personal experience with the SB-600 and the YN-462.
I have and like the SB-600 that @rfusca mentions, but if you're really on a budget then you should also consider the SB-400 as well. It's smaller, which may be good or bad depending on your circumstances. (Ken Rockwell raves about it, make of that what you will.)
I have the SB-400 and a couple of older SB-24's (won't TTL with any Nikon DSLR, but can be used in manual and A-mode). One thing that I appreciate with the older speedlights is the ability to rotate and tilt the head. The SB-400, while compact, can only tilt up.
So I'd go with a SB-600 (if you can find it, it's been discontinued recently) or the new SB-700 or a used SB-800.
I think it depends if you want an on-camera or off-camera flash. If on-camera then I can recommend the Nissin DI-622 which I was given for Christmas (~£100 from Amazon). I was able to take some lovely casual portraits with my D60 and the flash in i-TTL mode. I believe a new version ("mk II") has just been released with wireless capabilities which will cost about 50% more, but may come in handy in the future should you decide that you want more than one flash.
If you already know you want off-camera flash then I would probably start with http://strobist.blogspot.com/2006/03/lighting-101-start-here.html.