Quick Knowledge Check: Calling Procedures

# Question Answer
1 What is a procedure (function) in Python? B: a named set of reusable code instructions
2 Defining vs. calling a procedure B: defining creates the procedure, calling executes it
3 What is location in def report_incident(location):? B: a parameter
4 What is an argument? B: the actual value passed in when the procedure is called
5 Why are parameters useful? A: they let the same procedure work with different data

Popcorn Hack 1: Simple Procedure Call & Parameters

report_status(location, status) prints one line using both parameters. It is called twice with different arguments, so the same code runs on different data.

Code Runner Challenge

Procedures Popcorn #1

View IPYNB Source
# CODE_RUNNER: Procedures Popcorn #1

def report_status(location, status):
    # Print a status update using both parameters
    print("Status update from " + location + ": " + status)

# Call report_status() two times with different arguments
report_status("Poway", "Roads clear")
report_status("Rancho Bernardo", "Power restored")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Actual output:

Code Runner Challenge

Popcorn Hack 2 - Decomposition & Helper Procedures

View IPYNB Source
Status update from Poway: Roads clear
Status update from Rancho Bernardo: Power restored
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

location and status are parameters (placeholders in the definition). "Poway" and "Roads clear" are arguments (the real values passed in the call).

Popcorn Hack 2: Decomposition & Helper Procedures

is_priority() is the helper (returns a Boolean). process_report() is the outer procedure: it calls the helper, stores the returned value, and prints a different message with an if/else.

Code Runner Challenge

Homework Hack - Emergency Response Dispatcher

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Decomposition & Helper Procedures

# Helper Procedure
def is_priority(incident_type):
    # Return True when the incident is a "Medical Emergency"
    return incident_type == "Medical Emergency"

# Outer Procedure
def process_report(location, incident_type):
    # Call is_priority() and store the returned value
    priority = is_priority(incident_type)

    # Different message depending on the helper's answer
    if priority:
        print("PRIORITY: " + incident_type + " reported in " + location)
    else:
        print("STANDARD REPORT: " + incident_type + " reported in " + location)

# Call process_report() two times with different incident types
process_report("Poway", "Medical Emergency")
process_report("Poway", "Power Outage")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Actual output:

PRIORITY: Medical Emergency reported in Poway
STANDARD REPORT: Power Outage reported in Poway

Homework Hack: Emergency Response Dispatcher

check_priority() returns a Boolean, and dispatch_response() calls it and prints a different response depending on the value. Three calls with different incident types and two locations.

# CODE_RUNNER: Homework Hack - Emergency Response Dispatcher

# Helper procedure: decides whether an incident needs priority handling
def check_priority(incident_type):
    # Return True when the incident requires priority handling
    return incident_type == "Medical Emergency" or incident_type == "Wildfire"

# Outer procedure: uses the helper's Boolean to pick the response
def dispatch_response(location, incident_type):
    # Call check_priority() and store the returned Boolean
    priority = check_priority(incident_type)

    # Print the appropriate response
    if priority:
        print("DISPATCH NOW: crew sent to " + location + " for " + incident_type)
    else:
        print("LOGGED: " + incident_type + " in " + location + " added to the queue")

# Three calls, different incident types and two locations
dispatch_response("Poway", "Medical Emergency")
dispatch_response("Poway", "Power Outage")
dispatch_response("Rancho Bernardo", "Wildfire")

Actual output:

DISPATCH NOW: crew sent to Poway for Medical Emergency
LOGGED: Power Outage in Poway added to the queue
DISPATCH NOW: crew sent to Rancho Bernardo for Wildfire

Checklist: both procedures use def and parameters, dispatch_response() calls check_priority(), check_priority() returns a Boolean, three calls use different arguments, and no code is repeated for each incident (decomposition).