StayTalentReady

Functions and Modularity

Outcome 5: Functions and Modularity · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx

Objectives

Key terms

def
Python keyword that begins a function definition: def function_name(parameters):.
parameter
A variable listed in a function definition that receives the caller's argument value.
argument
The actual value passed to a function at the point of the call.
return
A statement that sends a value back to the caller and immediately exits the function.
local scope
Variables defined inside a function exist only during that function's execution.
global scope
Variables defined outside all functions; accessible throughout the module file.
function call
The expression that invokes a function: function_name(arguments).
modular design
Decomposing a program into small, single-purpose functions each independently testable and reusable.
import
A statement that loads a module (import math) to access its functions and constants.

The concept

A function is a named block of code that performs one specific task. Once defined, it can be called as many times as needed without rewriting the code. This is the principle of Don't Repeat Yourself (DRY) — one of the most important rules in software development.

Defining a function uses the def keyword: def calculate_area(length, width): return length * width. The function name follows def. Parameters (length, width) are local variable names that receive the caller's arguments. The body is indented. return sends a value back. When Python reaches return, the function exits immediately.

Calling a function: area = calculate_area(5, 3) passes the arguments 5 and 3. Inside the function, length=5 and width=3. The function returns 15, which is assigned to area. You can call it as many times as you need — calculate_area(10, 4), calculate_area(2, 7) — with different arguments each time.

Scope defines where a variable is accessible. A variable created inside a function (local variable) exists only during that function call — it disappears when the function returns. A variable created at the module level (outside all functions) is global and can be read anywhere in the file. Modifying a global inside a function requires the global keyword, but this is generally discouraged. Pass values in through parameters and return results — don't rely on global variables.

Modular design applies to larger programs. Each function should do one thing well. A grade-book program might have: get_scores() to read input, calculate_average(scores) to compute, assign_grade(average) to classify, and print_report(name, average, grade) to display. Each function is small, independently testable, and reusable. If the grading scale changes, you only change one function.

Blueprint Pillar 3 — Technology and Society: Every function in a smartphone app, website, or medical device was written once and called many times. The reliability of modern software depends on functions that do one thing correctly. A 40-line function with ten responsibilities is harder to test, audit for security, and maintain than ten 4-line functions with one responsibility each.

Worked examples

Example 1: Converting temperature: def celsius_to_fahrenheit(celsius): return (celsius * 9/5) + 32. Call: print(celsius_to_fahrenheit(0)) → 32.0. print(celsius_to_fahrenheit(100)) → 212.0. The formula runs once in the definition; the function is called twice with different inputs.
Example 2: Scope demonstration: x = 10. def change_x(): x = 99. print(x). change_x(). print(x). Output: 99 then 10. Inside change_x(), x = 99 creates a LOCAL x that shadows the global. The global x is unchanged. Use return to pass values out of functions rather than relying on global state.

Common mistakes

Self-check

Try each one before you look. A miss here costs nothing and tells you exactly what to reread.

1. Which keyword begins a function definition in Python?
2. A variable defined inside a function is accessible from:
3. What does a function without a return statement return?
4. Which principle does modular design apply to avoid code repetition?
5. Given: def square(x): return x**2. What does square(4) return?

Canvas is the official record. This companion enhances the PGCC curriculum; it does not replace it. Last name and class year only. Students with a 504 plan or IEP: your accommodations apply.

← Iteration and LoopsLists and Data Structures →

↑ Back to top