3.12 Calling Procedures

Source lesson: python/calling-procedures

Learning Objective: Define Python procedures with parameters, pass argument values into them, and call helper procedures inside larger procedures to organize an emergency-response program.

Popcorn Hack 1: Simple Procedure Call & Parameters

location and status are parameters: placeholders in the definition. "Poway High" and "All clear" are arguments: the actual values sent in when the procedure is called. The same procedure runs twice on different data without rewriting it.

Code Runner Challenge

Procedures Popcorn #1

View IPYNB Source
# CODE_RUNNER: Procedures Popcorn #1
def report_status(location, status):
    print("Status update from " + location + ": " + status)

report_status("Poway High", "All clear")
report_status("Community Park", "Needs assistance")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Decomposition & Helper Procedures

is_priority is the helper. It handles one sub-task (is this a medical emergency?) and returns a Boolean. process_report is the outer procedure: it calls the helper, stores the result, and picks which message to print.

Code Runner Challenge

Popcorn Hack 2 - Decomposition & Helper Procedures

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Decomposition & Helper Procedures
# Helper Procedure
def is_priority(incident_type):
    return incident_type == "Medical Emergency"

# Outer Procedure
def process_report(location, incident_type):
    priority = is_priority(incident_type)
    if priority:
        print("PRIORITY: " + incident_type + " reported in " + location)
    else:
        print("STANDARD REPORT: " + incident_type + " reported in " + location)

process_report("Poway", "Medical Emergency")
process_report("Poway", "Power Outage")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack: Emergency Response Program

check_priority is the helper: it returns True for incidents that put people in immediate danger (medical emergencies, fires, gas leaks) and False otherwise. dispatch_response is the outer procedure: it calls check_priority, stores the Boolean, and prints a different response for each value. It’s called four times with different locations and incident types.

Code Runner Challenge

Homework Hack - Poway Neighborhood Emergency Corps dispatch

View IPYNB Source
# CODE_RUNNER: Homework Hack - Poway Neighborhood Emergency Corps dispatch
# Helper Procedure: decides whether an incident needs priority handling
def check_priority(incident_type):
    return incident_type == "Medical Emergency" or incident_type == "Fire" or incident_type == "Gas Leak"

# Outer Procedure: calls the helper, then prints the matching response
def dispatch_response(location, incident_type):
    priority = check_priority(incident_type)
    if priority:
        print("PRIORITY DISPATCH: Sending responders to " + location + " for " + incident_type)
    else:
        print("STANDARD RESPONSE: Logging " + incident_type + " at " + location + " for volunteer follow-up")

dispatch_response("Poway High", "Medical Emergency")
dispatch_response("Community Park", "Downed Tree")
dispatch_response("Oak Street", "Fire")
dispatch_response("Poway Library", "Power Outage")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Why decomposition helps: the priority rule lives in one place. Adding a new priority incident type means editing one line in check_priority, and every call to dispatch_response picks it up without repeating any code.