Skip to content

09 Functions

Functions help us organize code and reuse small tasks.

Run in Binder Open in Colab Download notebook

Read here. Practice in a notebook.

This page shows the original notebook. Open Binder or Colab to run and edit cells. Work one cell at a time: some examples are deliberately broken for debugging practice. Download your work before leaving Binder; its sessions are temporary.

View original source

Learning Objectives

By the end, you should be able to:

  • Explain why functions help reduce repeated code.
  • Define a simple function with def.
  • Call a function to run its code.
  • Use one simple parameter in a function.

Concept Introduction (no code first)

A function is a reusable block of code that performs a task. Functions help us organize programs and avoid repeating the same code.

Repetition Problem

Without a function, repeated messages can make code longer than it needs to be. This code repeats the same task multiple times. Is there a better way?

Think Before You Run: What task is repeated in this example?

# Repeated code without a function
print("Running system check")
print("System check complete")

print("Running system check")
print("System check complete")

print("Running system check")
print("System check complete")

Defining a Function

def starts a function. Defining a function does not run it. A function runs only when it is called.

# Define the function first.
def run_system_check():
    print("Running system check")
    print("System check complete")

Think Before You Run: Will anything happen if we define a function but do not call it?

Calling a Function

Calling a function means telling Python to run it. Use the function name followed by parentheses.

# Call the function to run its code.
run_system_check()
run_system_check()

Think Before You Run

What will happen when we call this function three times?

# A short function can be reused many times.
def show_login_message():
    print("Welcome to the course system")

show_login_message()
show_login_message()
show_login_message()

Function with Parameter (simple)

A parameter allows us to pass information into a function. In this notebook, we will usually use only one parameter at a time.

# device_name is one parameter.
def show_device_status(device_name):
    print(device_name, "is ready")

show_device_status("Printer")
show_device_status("Router")

Real-World Examples

Functions are useful for repeated IT tasks, such as system checks, login messages, and simple data processing.

# Example 1: system check
def check_system():
    print("System status: OK")

check_system()

# Example 2: login message with one parameter
def greet_user(user_name):
    print("Welcome,", user_name)

greet_user("Student")

# Example 3: simple data processing with one parameter
def show_next_attempt(attempts):
    print("Next attempt number:", attempts + 1)

show_next_attempt(3)

Optional: Return Values (light introduction)

A return value is a result that the function gives back. We can store that result in a variable.

# Light return example
def add(a, b):
    return a + b

total = add(2, 3)
print("Total:", total)

Common Mistakes

Common function mistakes include forgetting parentheses, wrong indentation, or calling a function before it has been defined. Functions must be defined before they are used.

Forgetting parentheses:

# This shows the function object, but does not run it.
show_login_message

# This runs the function.
show_login_message()

Indentation error:

# Broken
def check_system():
print("System OK")

# Fixed
def check_system():
    print("System OK")

Calling before defining:

# Define the function first, then call it.

Guided Practice

Change the message or value, then run each cell again.

# TODO: Change the message inside the function.
def show_help_message():
    print("Please contact the help desk")

show_help_message()
# TODO: Change the name used when calling the function.
def welcome_user(user_name):
    print("Hello,", user_name)

welcome_user("Ava")

Parsons Problem

The lines below are scrambled. Put them in the correct order in the next cell.

show_status()
    print("Status: ready")
def show_status():
# TODO: Reorder the scrambled lines from above.
# Define the function first, then call it.

def show_status():
    print("Status: ready")

show_status()

Mini Lab

Define and use a function to process or display data. Keep the function short and call it at the end.

# Mini Lab: display a ticket update
# Step 1: Define a function.
# Step 2: Use one parameter for the ticket count.
# Step 3: Call the function with a test value.
# TODO: Change the ticket count and rerun the cell.

def show_ticket_update(ticket_count):
    print("Open support tickets:", ticket_count)
    print("Next ticket number:", ticket_count + 1)

show_ticket_update(7)

Wrap-Up

Functions organize code and reduce repetition. A function runs only when it is called.

Next, you will learn how to test and debug your programs.

Reflection

How could functions help organize repeated tasks in your field or daily work?