When you are working with functions it is really important to know the order in which statements are executed. This is called the flow of execution. Weβve already talked about it a number of times in this chapter and youβve practiced every time youβve traced the steps of an algorithm.
Execution always begins at the first statement of the program. Statements are executed one at a time, in order, from top to bottom. Function definitions do not alter the flow of execution of the program, but remember that statements inside a function are not executed until the function is called. Function calls are like a detour in the flow of execution. Instead of going to the next statement, the flow jumps into the scope of that called function, to the functionβs first line, executes all the statements there, and then comes back to pick up where it left off.
That sounds simple enough, until you remember that one function can call another. While in the middle of one function, the program might have to execute the statements in another function. And while executing that new function, the program might have to execute yet another function!
Fortunately, the Python interperter is adept at keeping track of where it is, so each time a function completes, the program picks up where it left off in the function that called it. When it gets to the end of the program, it terminates.
What does all that mean for us when we try to understand a program? Donβt just read from top to bottom. Instead, follow the flow of execution. This means that you will read the def statements as you are scanning from top to bottom, but you should skip the body of the function until you reach a point where that function is called.