I can't think of any reason why you can't sell it them.
You can buy and sell a teapot on a tourist visa, why not some code?
Email it to them from a .co.uk address..
As noted above, it would be normal for the code to become the property of the company paying for it. It's common for intellectual property (IP) to be clearly specified in the contract as to who owns it. If you own the IP, you can resell to others. If they own the IP, they will own it. There is nothing that can stop you redeveloping it again from scratch (which usually takes a fraction of the time, for something you developed in the first place) but you technically should not use any of the existing code.
set up an offshore company.
transfer all your IP rights to offshore company.
either sell shares in offshore company to old employer or enter into a licensing agreement with old company.
don't know though what the tax / visa implications will be.