1.2.27 · D3Introduction to Programming (Python)

Worked examples — Functions — def, parameters, return, docstrings

2,748 words12 min readBack to topic

The scenario matrix

A function can be poked in only so many fundamentally different ways. Below is every "cell" — every distinct behaviour class this topic hides. The examples that follow are each tagged with the cell they cover, so together they leave no gap.

Cell What makes it different Example that covers it
A. Basic value return one input, one clean returned value Ex 1
B. No return body runs, but nothing handed back → None Ex 2
C. print vs return confusion looks the same on screen, behaves differently Ex 2
D. Multiple return values tuple packing / unpacking Ex 3
E. Default parameter used / overridden argument omitted vs supplied Ex 4
F. Keyword args reorder names beat position Ex 4
G. Degenerate / edge input zero, empty, boundary values Ex 5
H. Early return (branching exit) return inside if, code after skipped Ex 6
I. Mutable default trap the shared-list bug Ex 7
J. Real-world word problem translate a story into a function Ex 8
K. Exam-style twist trace a tricky call, predict exact output Ex 9

The three "flavours" of arguments (positional / keyword / default) live in cells E and F; the branching logic borrows from Control Flow — if statements; tuple return borrows from Lists and Tuples; the whole motive is the DRY Principle.


Ex 1 — Cell A: the plain machine


Ex 2 — Cells B & C: print is not return


Ex 3 — Cell D: returning two things at once


Ex 4 — Cells E & F: defaults and keyword reordering


Ex 5 — Cell G: degenerate and boundary inputs


Ex 6 — Cell H: early return skips the rest


Ex 7 — Cell I: the mutable default trap


Ex 8 — Cell J: real-world word problem


Ex 9 — Cell K: the exam twist (trace it exactly)


Recall Cover the answers — did every cell stick?

Basic return square(6)? ::: 36 z = show_square(6) then print(z) prints? ::: 36 then None divmod2(17, 5) gives q, r =? ::: q = 3, r = 2 power(exp=3, base=2)? ::: 8 average([]) in the unguarded version does what? ::: raises ZeroDivisionError add_item(1) then add_item(2) with the buggy shared default print? ::: [1] then [1, 2] share_per_person(800, 4) (10% default tip)? ::: 220.0 f(3, 3) where f returns a-b if a>b else b-a? ::: 0

Connections

  • Parent topic — the anatomy — this page is its "every scenario" workshop.
  • Control Flow — if statements — powers the branching returns in Ex 5, 6, 9.
  • Lists and Tuples — tuple packing/unpacking behind Ex 3's multiple return.
  • Variables and Scope — why the mutable-default list in Ex 7 outlives a single call.
  • Lambda and Higher-order Functions — the same input→output idea, written in one line.
  • Recursion — a function calling itself, built on the very return traced here.
  • DRY Principle — the reason every example above was worth wrapping in a function.

Scenario Map

path A

path B

Call a function

Has a return

No return

One value

Tuple of values

Returns None

Edge input

Early return

Default arg