1.2.27 · D1Introduction to Programming (Python)

Foundations — Functions — def, parameters, return, docstrings

2,823 words13 min readBack to topic

Before you can read a single line of the parent note, you meet a pile of symbols and words: def, the parentheses ( ), the colon :, indentation, return, =, **, //, %, None, triple quotes """, and default parameters like exp=2. This page defines every one of them from absolute zero — plain words, then a picture, then why the topic needs it. Read top to bottom; each item uses only things defined above it.


0. The most basic idea: a name that holds a thing

Before functions, you need variables. A variable is just a label stuck on a value.

Figure s01 (below) — if the image does not load: it shows a blue box holding the value 5 on the right, an orange tag labelled x on the left, and a green arrow from x pointing at 5, with the caption "the = arrow works right → left".

Look at the picture: the box 5 sits in memory, and the name x is a tag pointing at it. This is why the parent note can write result = add(2, 3) — the value falls out of add, then the tag result is stuck onto it.


1. Indentation — the shape is the grammar

In many languages you group lines with { } braces. Python uses spaces: lines pushed to the right by the same amount form one block.

Figure s02 (below) — if the image does not load: it shows def add(a, b): at the left margin (blue), its body return a + b shifted 4 spaces right (green) with an orange arrow labelled "4 spaces = inside the function", and a de-indented line result = add(2, 3) marked red "OUTSIDE".

The picture shows the def add(a, b): header at the left margin, and its body return a + b pushed inward. The indentation is what tells Python "this line is inside the function." De-indent and the line escapes the function.


2. The colon : — "a block starts here"

Picture it as a hinge: the line before the colon is the header (who owns the block), and the indented lines after are the body (what they do). The parent's def area(r): uses exactly this hinge.


3. Parentheses ( ) — three jobs, not two

This trips up beginners because the same symbol does several things.

Figure s03 (below) — if the image does not load: the left half shows two dashed blue boxes labelled a and b ("empty slots"); the right half shows two solid orange boxes holding 2 and 3 ("real values drop in"), with a green "fill" arrow between them, and the caption "parameter = slot, argument = filling".

The picture contrasts definition and call: on the left, empty labelled slots waiting; on the right, real values dropping into those slots. This is the whole "slot vs filling" distinction the parent calls parameter vs argument. (The grouping job — (a + b) * c — is a fourth place you'll meet parentheses; it has nothing to do with functions but you should recognise it.)


4. The word def — "build a box, don't run it yet"

Picture a factory building a vending machine and bolting a nameplate on it. Building ≠ vending. The machine only dispenses when someone presses a button later (the call). This is why the parent stresses defining ≠ running.


5. Default parameters — a slot with a fallback value

Picture a vending machine with a pre-set dial: if you leave the dial alone, it uses its factory setting (2); if you turn it, your setting wins.

  • power(5) → only base given, so exp falls back to 2 → computes 5 ** 2.
  • power(2, 3) → both given, so exp becomes 3 → computes 2 ** 3.

6. return — the output chute

Figure s04 (below) — if the image does not load: it shows a blue box labelled "function body (the work)"; an orange arrow enters the top ("inputs in"); a green arrow leaves the bottom ("return: result drops out"); and a red arrow to the side saying "then the machine STOPS".

The picture shows a machine: inputs enter the top, the body churns, and return is the chute at the bottom where the finished result drops out. The moment something drops out, the machine switches off.


7. None — the "nothing came out" token

Picture pressing the vending button but the chute is empty — you did get a response ("nothing"), and that "nothing" has a name: None. This is why the parent warns that print (which returns None) is not return.

Recall Why is

None not the same as an error? None is a perfectly valid value ::: it just means "no meaningful result," so the program keeps running; it does not crash.


8. Triple quotes """...""" — the docstring wrapper

Picture the sticker on the vending machine saying "this gives you chips." It is attached to the machine but is not part of the mechanism. The parent's area docstring is exactly this sticker.


9. The little maths operators the parent uses

The parent's examples quietly use symbols you must know:

Figure s05 (below) — if the image does not load: it shows 17 small squares in a row, coloured as three groups of five (blue, green, orange) plus two red squares left over, with the caption "3 whole groups (//) + 2 left over (%)".

The picture lays out : three whole fives (// gives 3) with 2 left over (% gives 2). Together they explain the parent's divmod2 example, which returns (quotient, remainder) = (3, 2).


10. Tuples ( , ) and unpacking — catching two things at once

Picture a two-compartment lunchbox: one call hands you the whole box, and q, r = ... opens both compartments in one motion. See Lists and Tuples for the full story.


Prerequisite map

The diagram below reads top-to-bottom: each foundation feeds into the box below it, and all of them flow into the topic Functions. If the diagram fails to render, the same structure is spelled out in the checklist that follows.

Assignment: label on a value

Indentation: marks the body

Colon: a block starts here

Parentheses: group, slots, and GO button

def: builds the box

Default parameters: a slot with a fallback

return: the output chute

None: nothing came out

Triple-quote docstring

Operators: plus star power floordiv mod

Tuples and unpacking

FUNCTIONS the topic


Equipment checklist

Cover the right side and test yourself. If any answer surprises you, re-read that section.

What does a single = do?
Sticks a label (name) on a value — assignment, right to left. It does NOT ask a question.
What does == do instead?
Asks a yes/no question — are these two things equal?
What do editorial double equals around a word mean on this page?
Just study-note highlighting; it is NOT Python syntax.
How does Python know which lines are inside a function?
By indentation — the lines shifted right under the def header form the body.
What does the colon : at the end of a def line signal?
That an indented block belongs to that line.
Name the three jobs of parentheses.
Grouping in expressions (a+b)*c; holding parameter slots in a def; holding arguments and meaning "run now" in a call.
In add(2, 3), what do the parentheses do?
Hold the argument values AND mean "run the function now."
What is the difference between greet and greet()?
greet is the function object (the box); greet() runs it.
What does exp=2 mean in a def line?
A default: if the caller gives no exp, use 2 as a fallback.
What two things does return do at once?
Hands a value back to the caller, and immediately stops the function.
What does a function with no return give back?
None — the "nothing here" value.
Where does a docstring go and what wraps it?
The first line inside the function body, wrapped in triple quotes """...""".
What does 2 ** 3 equal and what does ** mean?
8; ** is "raise to the power".
What do // and % give for 17 and 5, and how do you type %?
17 // 5 is 3 (whole divisions); 17 % 5 is 2 (remainder); type a plain percent sign, no backslash.
What does return a, b build, and how do you catch it?
A tuple (a, b); catch it with unpacking x, y = f().

Return to the parent: parent topic.