When a program ends playing,which function is the last one executed?

When a program ends playing,which function is the last one executed?

If this is a general programming question then I’m sure a program starts and ends from the ‘main’ function. I could be totally wrong