Functions and Modularity
Outcome 5: Functions and Modularity · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx
Objectives
- Define a function with def, parameters, and a return statement.
- Call a function with arguments and use its return value.
- Distinguish between local and global scope.
- Apply modular design by breaking a program into single-purpose functions.
- Import and use functions from Python's standard library.
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
Common mistakes
- Forgetting to call the function after defining it. def greet(): print('Hi') never runs on its own — you must write greet() to execute it.
- Expecting a return value when there is no return statement. A function without return returns None. result = greet() where greet only prints will give result = None.
- Using a local variable outside the function. score = calculate() + bonus fails if bonus is defined only inside calculate — parameters and returns are the correct way to pass values.
Self-check
Try each one before you look. A miss here costs nothing and tells you exactly what to reread.
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.