To swear, except when necessary, is becoming to an honorable man. —Quintilian
1. Pain relief. Swearing activates the so-called 'fight or flight' response, leading to a surge of adrenaline and a corresponding analgesic effect. Richard Stephens of Keele University in England found that people who swear are able to hold their hands in ice-water for twice as long. However, this only holds for people who swear a few times a day, not for so-called 'chain-swearers'. Presumably chain-swearers are densensitized to their swearing, and so not particularly aroused by it. It remains unclear whether some swear words are more effective than others. But it seems very likely.
2. Power and control. Swearing can give us a greater sense of power and control over a bad situation. By swearing we show, if only to ourselves, that we are not passive victims but empowered to react and fight back. This can boost our confidence and self-esteem, and also provide the impetus for further corrective action to be taken. As Mark Twain put it, 'When angry, count to four; when very angry, swear.'
3. Non-violent retribution. Swearing enables us to get back at bad people or situations without having to resort to violence. Instead of punching someone in the face or worse, we channel and disarm our anger by swearing instead. True, swearing can also have hurtful consequences, but better a few sharp words than a sharp dagger. Swearing can also serve as a warning signal or as a marker of rank and authority, a bit like an animal's growl: "Watch out. Stop it. Or you're damn well going to pay the price."