What does butter do in cooking? When cooks use butter in cooking for whatever recipe, what is its function? For example, if I add butter to a chocolate sauce which is made from heavy cream and milk chocolate, what do people expect the butter to do?

It's not used to sweeten the product. It's not used to make it thick because with the heavy cream and chocolate its already thick. It's not used for flavor, or is it? What does it do?

Is there a general rule as to what function butter is supposed to do in cooking?

And please don't tell me it's just for the obvious point of being salty or buttery in flavor, surely there is more to it, some chemical process and purpose?
Butter serves multiple purposes.  Butter does = flavor, particularly when the milk solids are caramelized.  Butter is also used as an emulsifier.  Butter + water or stock, can easily become a delicious sauce.  An emulsifier is also the role it plays in your example of a chocolate sauce.  It contributes to the smoothness of the chocolate sauce.  Butter plays many other roles as well.  For example in laminated dough (the dough that a croissant is made from) the butter plays the role of keeping layers of dough separate and trapping steam during baking, making a flaky pastry.