065. Parameters And Arguments
Parameters define the interface; arguments provide the data
065. Parameters And Arguments
Aryan extracts his alert logic into a function. How he names the parameters is the API contract for every future caller:
def check_threshold(process_name, rss_mb, warn_mb, crit_mb):
"""Return alert level for a process's memory usage."""
if rss_mb >= crit_mb:
return f"CRITICAL: {process_name} using {rss_mb:.0f} MB"
elif rss_mb >= warn_mb:
return f"WARNING: {process_name} using {rss_mb:.0f} MB"
return None
# Arguments must match the parameter order
msg = check_threshold("Chrome", 1250, 500, 1000)The silent bug: calling check_threshold("Chrome", 500, 1250, 1000) passes warn_mb=1250 and rss_mb=500 — no error, wrong result. Mismatched argument order is one of the hardest bugs to spot because nothing crashes.
This is why Aryan starts using keyword arguments for functions with more than two parameters:
msg = check_threshold("Chrome", rss_mb=1250, warn_mb=500, crit_mb=1000)💡 Fun fact: In C, mixing up parameter order causes the exact same silent wrong-answer bug — but C has no keyword arguments, so the only fix is careful documentation or wrapper structs. Python’s keyword argument syntax was designed specifically to make call sites self-documenting and order-independent.
⚠️ Watch out: The most common beginner mistake is confusing parameters (the variable names in the def line) with arguments (the values passed at the call site). They look identical in simple cases, which tricks people into thinking they’re the same thing — until a wrong-order call silently corrupts data.
🤔 Think about it: If Python matched arguments by name automatically (like keyword args always), would positional arguments still be useful? What would we lose?
Learning objectives
- Define functions with correct parameter names
- Pass arguments to functions positionally
- Match parameter names to the formula being implemented
Key concepts
- parameters
- arguments
- function signature
Try it
Concept detail
Parameters are the placeholders defined in ‘def f(a, b)’. Arguments are the actual values passed when calling ‘f(1, 2)’.
Python binds positionally by default: f(1, 2) → a=1, b=2. Order matters — swapping arguments silently produces wrong results.
Wrong parameter names are a semantic bug: def circle_area(diameter): # wrong name return math.pi * diameter ** 2 # formula is correct FOR radius circle_area(5) # caller thinks they’re passing radius=5 # gets π25, not π25 — lucky accident! But misleading.
Parameters are local variables. Rebinding them doesn’t affect the caller: def f(x): x = 99 # only changes the LOCAL x val = 42 f(val) print(val) # still 42
Exception: mutating a mutable argument (list, dict) DOES affect the caller: def f(lst): lst.append(99) # modifies the caller’s list
Solution
import math
def circle_area(radius):
return math.pi * radius ** 2
def rectangle_area(width, height):
return width * height
def triangle_area(base, height):
return 0.5 * base * height
def box_volume(length, width, height):
return length * width * heightTests
import math
def test_circle_area():
# r=1 → π
assert abs(circle_area(1) - math.pi) < 0.001
def test_circle_area_r2():
# r=2 → 4π
assert abs(circle_area(2) - 4 * math.pi) < 0.001
def test_rectangle_area():
assert rectangle_area(4, 5) == 20
assert rectangle_area(3, 7) == 21
def test_rectangle_not_addition():
# Catches the + instead of * bug
assert rectangle_area(4, 5) != 9
def test_triangle_area():
assert triangle_area(6, 4) == 12.0
assert triangle_area(10, 3) == 15.0
def test_box_volume():
assert box_volume(2, 3, 4) == 24