Skip to main content

Section 6.10 Flow of execution

In order to ensure that a function is defined before its first use, you have to know the order in which statements are executed, which is called the flow of execution.
Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom.

Checkpoint 6.10.1.

Q-1: The order in which statements are executed is called the __________________.
Function definitions do not alter the flow of execution of the program. The def creates a function object, but the statements inside the function are not executed until the function is called.
A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the body of the function, executes all the statements there, and then comes back to pick up where it left off.
At this point, it is very important for you to use codelens to go through the price comparison program. (We are setting the price and quantity variables so you don’t need to input them.) You will be able to see:
  • The statements in the body of the calculate_total function do not get executed until the function is called.
  • The “detour” when the function is called.
  • How the values of arguments are copied into the parameters.
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. But while executing that new function, the program might have to execute yet another function!
Observe below, where one function calls another function. Here, we will use our average function to help calculate the area of a trapezoid, given the dimensions of the top, bottom, and height. (This is a good example of an example—most professional programmers wouldn’t use a separate function for the average of the top and bottom.)
Fortunately, Python is good 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, the program is finished.
So what’s the moral of the story? When you read a program, you don’t always want to read strictly from top to bottom. Sometimes, it makes more sense if you follow the flow of execution and read functions as they are called.

Checkpoint 6.10.2.

    Q-3: Where does the execution of a program always begin?
  • The line where the first function is called.
  • Incorrect! The flow of execution begins at the very top of the program. Try again.
  • The first statement of the program.
  • The execution of a program begins at the first statement of the program.
  • The first function declaration.
  • Incorrect! The flow of execution begins at the very top of the program. Try again.

Checkpoint 6.10.3.

    Q-4: When you read a program, you…
  • ...don’t always want to read from top to bottom
  • Incorrect! This is important, but so is B. Try again.
  • ...should follow the flow of execution.
  • Incorrect! This is important, but so is A. Try again.
  • Both a and b
  • Correct! It is important to start reading from the beginning, but also to follow the flow of execution as it continues.