How I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
It depends upon your local laws and your contract. In the U.S., the default laws tend to vary by state. The last time I checked with my attorney, he told me that here in NY, all work is considered to be work-for-hire unless specified in writing. This means that the source code is automatically the property of the client, unless I get a contract stating otherwise. Which I do sometimes, but not that often.
Things get stickier if you use other people's libraries or even open source software within your project.
I've found that it's easiest to avoid problems if you simply discuss it with your client beforehand, and be as transparent as possible in your methods and expectations.
Better drink a whole bunch really fast. Next week, researchers will tell us it's bad again.