It tells an organism that something is wrong so that the organism can take action to avoid harm. Without pain, a hand stays too close to fire and is burned. Without pain, a person continues to walk on sharp rocks so that the flesh of the feet is cut and bleeding. Without pain, a person might make an injury worse and prevent healing. Lepers destroy their extremities not because the disease eats them away, but because the disease robs them of a sense of touch and pain.
Lepers have no way to tell when they are hurting themselves.
Some pain can be harmful. Pain can last beyond the initial cause of the pain. Far too many people think that the way to happiness is to inflict pain upon others. When pain does serve this role, it is a friend. When we get ourselves in trouble because we are not acting the way we should, the discomfort indicates to us that we need to make some changes.