What happens to Pokémon you leave in Pokémon Go Gyms?

I have just recently joined Pokémon Go. I was just wondering: when I leave a Pokémon in a gym and it gets overtaken, what happens to the Pokémon I put in?


Your Pokémon will get returned to you, fainted, when the gym is defeated. You must then heal it before using it again.

You will also receive any coins your Pokémon has accrued during its stay in the gym.

source: personal experience