Control flow is the path a program takes as it executes. Understanding that path helps you predict what a program will do, especially when it uses conditional statements and functions that call other functions.
Control flow is the sequence in which a program's statements are executed. Execution begins in the main program, but it does not always proceed straight down every line. A conditional statement, for example, can cause a block of code to run only when its condition is true.
All flashcards
What is control flow?
Control flow is the sequence in which a program executes statements, including changes caused by conditions and function calls.
What happens when a program reaches a function call?
Execution moves to the function's body and runs its statements. When the function finishes, control returns to the point after the call.
Does defining a function execute its body?
No. Defining a function makes it available; its body executes when the function is called.
What happens when one function calls another?
Control moves into the called function. When that function finishes, control returns to the calling function, which continues from its next statement.
How can you trace a program with nested function calls?
Follow the statements in order, move into each function when it is called, and return to the calling function after the called function finishes.
Quiz questions
What does control flow describe?Answer: The order in which a program executes its statements.
What happens to a function's body when the function is defined but never called?Answer: It does not execute just because the function was defined.
What can happen to a conditional block when its condition is false?Answer: The statements in the block are skipped.
If a function calls two other functions in sequence, what determines which one runs first?Answer: The order in which the calls appear.
Why might a function call another function?Answer: To let each function handle a separate task.
Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.