Parameters and Arguments
Unit ID: M05-U01 Estimated active time: 22-30 minutes
Parameters describe required inputs
def clean_course_name(raw_name):
return raw_name.strip().title().replace("Ai", "AI")
raw_name is a parameter: a name in the function definition.
clean_course_name(" python foundations for ai ")
The supplied string is an argument: the actual value passed during the call.
Several parameters
def calculate_total_hours(module_hours, extension_hours):
return module_hours + extension_hours
Call by position:
total = calculate_total_hours(32, 2)
Call with keywords:
total = calculate_total_hours(module_hours=32, extension_hours=2)
Keyword arguments make meaning visible, especially when several values have the same type.
Default values
def format_course_label(course_name, status="planned"):
return f"{course_name} | {status}"
The caller may omit status or provide it explicitly. Put required parameters before parameters with defaults.
Use defaults only when one value is genuinely appropriate in most calls. Do not hide required information behind a convenient guess.
Avoid mutable default values
Do not use a list as a beginner function default:
def add_tag(tag, tags=[]):
tags.append(tag)
return tags
That list is created once and can be shared across calls. Use None and create a fresh list inside when this pattern is needed. Full defensive design comes later; for now, avoid mutable defaults.
Check the call against the contract
These calls fail:
calculate_total_hours(32)
calculate_total_hours(32, 2, 1)
calculate_total_hours(hours=32, extension_hours=2)
They have missing, extra, or unexpected arguments. Read the TypeError and compare the call with the definition.
Practice
Write a function build_module_label(module_number, title, status="planned"). Return an f-string. Call it once with the default and once with status="available".
Takeaway
Parameters define a function's inputs; arguments supply values. Use clear names, deliberate defaults, and keyword calls when they improve meaning. Next, we will separate returned values from printed output.
