How do you return a specific amount of bitcoin to an address? I am trying to write a program to receive bitcoin, and then after some time, return the same amount of bitcoin to the same address.

How would you do this? Does bitcoin have the ability to send and receive coins through the same address, or would I have to ask for a return address and somehow link the sent amount address to the return address?
You should ask for a return address. Bitcoin transactions don't contain any "from address", it only references another (previous) transaction's output. An output can be a P2PKH (Pay to public key hash), but it can just as well be P2SH (Pay to script hash) and some other may be implemented in the future.

There's also a large possibility of getting a transaction referencing many previous outputs, as happens when someone paying you 1 BTC received many smaller transaction previously.

It would also be unintuitive to your users, as it's bitcoin best practice not to care about where the bitcoins come from and instead to generate a new address for every transaction received.

On top of that, you shouldn't implement any cryptography by yourself if you're not already a top-level expert, there are simply too many things that can go wrong. I would recommend using a proper API.