IPhone 4 contract without purchase of phone?

So, my friend gave me her old iPhone 4, and I don't want to pay 800$ to get started. I was wondering, if I have the phone, can I buy a SIM and a contract and use that?

Yes, just make sure that the SIM lock matches your carrier. For example, if the iPhone was purchased on AT&T, it will only work on AT&T, unless it is SIM Unlocked, in which case you can use T-Mobile or AT&T (or any other GSM network). Same applies in reverse if it originated from T-Mobile.

However, if the phone were originally from Verizon or Sprint (or any other CDMA carrier), you will most likely not be able to use it with any other carrier as the phones are specifically designed to use those networks.

In short: If the phone was originally from the same carrier you wish to sign up with, then everything should be fine, otherwise it may be more complicated.