>> Module 01 / Day 12 / Lecture

Parameters, Defaults & Multiple Returns

Day 12 of a 40-day journey
You can already write functions. Today we make them flexible — many inputs, smart defaults, and more than one answer back.
Lecture
75 min · 09:00–10:15
Lab
60 min · Geometry Helpers
Module
M1 · Python Foundations
DAY 12 / 40
>> Today's roadmap

Five stops, then a lab

  1. Multiple parameters — functions that take several inputs
  2. Default values — def f(x, pi=3.14159)
  3. Returning a tuple — handing back more than one answer
  4. Unpacking — a, p = rectangle(3, 4)
  5. Keyword vs positional arguments — then the lab
>> The hook

One input, one answer — that's the easy case.

Real functions often need several inputs and hand back several answers. A rectangle has a width and a height — and you usually want both its area and its perimeter.

first_taste.py
def rectangle(width, height):
    return width * height, 2 * (width + height)

area, perim = rectangle(3, 4)
print(area, perim)   # 12 14
>> 01 / 05 · Many inputs

Multiple parameters

Separate parameters with commas in the def line. When you call the function, you supply one argument per parameter, in order.

many_params.py
def rectangle(width, height):
    area = width * height
    perimeter = 2 * (width + height)
    return area, perimeter

rectangle(3, 4)   # width=3, height=4
Order matters.
The first argument fills the first parameter, the second fills the second. rectangle(3, 4) is not the same as rectangle(4, 3) for perimeter math elsewhere.
>> 02 / 05 · Sensible defaults

Default parameter values

Give a parameter a default with = in the def line. The caller can leave it out — or pass their own to override it.

defaults.py
def circle(radius, pi=3.14159):
    area = pi * radius * radius
    circ = 2 * pi * radius
    return area, circ

circle(2)            # uses pi = 3.14159
circle(2, pi=3.14)   # overrides → pi = 3.14
Defaults come last.
Parameters with a default must appear after those without one. def circle(radius, pi=3.14159) is fine; the reverse is a syntax error.
>> 03 / 05 · More than one answer

Returning multiple values as a tuple

Put commas after return and Python packs the values into a tuple for you — the same tuples you met on Day 3.

return_tuple.py
def rectangle(width, height):
    return width * height, 2 * (width + height)

result = rectangle(3, 4)
print(result)        # (12, 14)  ← a tuple
print(type(result))  # <class 'tuple'>
>> 04 / 05 · Catching the answers

Unpacking the tuple

Put the matching number of names on the left of = and Python hands each tuple slot to a name. Cleaner than digging out result[0].

the_clean_way.py
# Unpack into two names:
a, p = rectangle(3, 4)
print(a)   # 12
print(p)   # 14
the_clunky_way.py
# Without unpacking — works, but ugly:
result = rectangle(3, 4)
print(result[0])  # 12
print(result[1])  # 14
>> 05 / 05 · Two ways to pass arguments

Keyword vs positional arguments

Positional: matched by order. Keyword: matched by name with name=value — order-free and self-documenting.

keyword_vs_positional.py
circle(2)                 # positional: radius=2, pi=default
circle(2, 3.14)           # positional: radius=2, pi=3.14
circle(2, pi=3.14)        # keyword for pi — reads clearly
circle(radius=2, pi=3.14)  # both keyword — order no longer matters
circle(pi=3.14, radius=2)  # same call, swapped order ✓
>> Putting it together

Every idea, in one screen

Multiple parameters, a default, a tuple return, and unpacking — working together.

geometry.py
def circle(radius, pi=3.14159):      # default value
    area = pi * radius * radius
    circ = 2 * pi * radius
    return area, circ                # return a tuple

a, c = circle(2)                    # unpack two names
print(f"area={a:.2f}, circ={c:.2f}")  # area=12.57, circ=12.57

a, c = circle(2, pi=3.14)            # keyword overrides default
print(f"area={a:.2f}, circ={c:.2f}")  # area=12.56, circ=12.56
>> Common failures

Two mistakes you'll make today

positional_after_keyword.py
# A positional argument after a
# keyword argument is illegal:
circle(radius=2, 3.14)
# SyntaxError: positional argument
# follows keyword argument

# Fix — keyword for both, or
# positional for both:
circle(2, 3.14)        # ✓
circle(radius=2, pi=3.14) # ✓
forgot_to_unpack.py
# Forgetting to unpack the tuple:
area = rectangle(3, 4)
print(area)   # (12, 14) — a whole
              # tuple, not just the area!

# Fix — give it both names:
area, perim = rectangle(3, 4)
print(area)   # 12 ✓
>> 11:00 – 12:00 · Lab

Today's lab — Geometry Helpers

PROBLEM 1
rectangle(width, height)

Return a tuple (area, perimeter). Unpack it in the caller: a, p = rectangle(3, 4).

PROBLEM 2
circle(radius, pi=3.14159)

Return (area, circumference) using a default pi. Try circle(2) and circle(2, pi=3.14).

>> End of Day 12

What you can now do

  • ✓ Write functions that take several parameters and give some a default value.
  • ✓ Return more than one value as a tuple, and unpack it in the caller.
  • ✓ Call functions with positional and keyword arguments — and know which order is legal.
01 / 12
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF