Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Identify the control flow of a program.
  • Describe how control flow moves between statements and function calls.

Control flow and functions

Control flow is the sequence of program execution. A program's control flow begins at the main program but rarely follows a strict sequence. Ex: Control flow skips over lines when a conditional statement isn't executed.

When execution reaches a function call, control flow moves to where the function is defined and executes the function statements. Then, control flow moves back to where the function was called and continues the sequence.

Checkpoint

Calling a brunch menu function

Concepts in Practice

Following the control flow

1.
Which line is executed first?
1
def park_greet():
2
  """Output greeting."""
3
  print("Welcome. Open sunrise to sunset.")
4
5
car_count = 1
6
park_greet()
7
if car_count > 50:
8
  # Direct to extra parking lot
  1. 1
  2. 3
  3. 5
2.
Control flow moves to line 9, and park_greet() is called. Which line does control flow move to next?
1
def extra_lot():
2
  # Function definition
3
4
def park_greet():
5
  """Output greeting."""
6
  print("Welcome. Open sunrise to sunset.")
7
8
car_count = 1
9
park_greet()
10
if car_count > 50:
11
  extra_lot()
  1. 1
  2. 4
  3. 10
3.
Control flow moves to line 12, and extra_lot() is called. Which line does control flow move to after line 3 is executed?
1
def extra_lot():
2
  """Output extra parking lot info."""
3
  print("Take the second right to park.")
4
5
def park_greet():
6
  """Output greeting."""
7
  print("Welcome. Open sunrise to sunset.")
8
9
car_count = 1
10
park_greet()
11
if car_count > 50:
12
  extra_lot()
  1. 5
  2. 8
  3. 12
4.
What is the output?
def park_greet():
  """Output greeting."""
  print("Welcome to the park")
print("Open sunrise to sunset")

park_greet()
  1. Welcome to the park
  2. Welcome to the park
    Open sunrise to sunset
  3. Open sunrise to sunset
    Welcome to the park

Functions calling functions

Functions frequently call other functions to keep the modularity of each function performing one task. Ex: A function that calculates an order total may call a function that calculates sales tax. When a function called from another function finishes execution, control flow returns to the calling function.

Checkpoint

Example: Book club email messages

Concepts in Practice

Functions calling functions

Consider the book club example above.

5.
How many function calls occur during the execution of the program?
  1. 2
  2. 3
  3. 6
6.
When line 3 is reached and executed, which line does control flow return to?
  1. 1
  2. 11
  3. 16

Try It

Updated terms and conditions prompt

Write an updated function, terms(), that asks the user to accept the terms and conditions, reads in Y/N, and outputs a response by calling accepted() or rejected(). accepted() prints "Thank you for accepting the terms." and rejected() prints "You have rejected the terms. Thank you.".

Given inputs 2, "Y" and "N", the output is:

    Do you accept the terms and conditions?
    Y
    Thank you for accepting the terms.
    

Given a function call to terms() and input "N", the output is:

    Do you accept the terms and conditions?
    N
    You have rejected the terms. Thank you.
    

Try It

Laundromat information

Write a program that uses three functions to print information about a laundromat, Liam's Laundry:

  • laundromat_info(): Prints the name, Liam's Laundry, and hours of operation, 7a - 11p, and calls washers_open() and dryers_open()
  • washers_open(): Reads an integer, assigns washer_count with the value, and prints washer_count
  • dryers_open(): Reads an integer, assigns dryer_count with the value, and prints dryer_count

The main program should just call laundromat_info().

Given inputs 50 and 40, the output is:

    Liam's Laundry
    7a - 11p
     Open washers: 50
     Open dryers: 40
    
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.