A definition tells Python what the function does and nothing more. The body sits there unused until a call arrives, and only then does it run.
greet says what the call will do.Press run and watch the highlight. Line 1 is read and stored, never executed. When the call on line 4 arrives, control jumps up into the body, and once the body ends it comes back to the line below the call.
Python reads the file from top to bottom. Calling greet before its definition raises NameError, because at that moment the name still means nothing.
total = average(scores) says what happens. Six lines of arithmetic in the same position say how it happens, which is a question for later.A parameter is the name written between the brackets of the definition. An argument is the value handed over in the call. During the call the parameter holds that value, and when the call ends the parameter disappears.
| Form | Meaning | Example | Binding |
|---|---|---|---|
| f(a, b) | positional, matched by order | repeat("ha", 3) | word='ha', times=3 |
| f(x=a, y=b) | keyword, matched by name | repeat(word="ha", times=3) | word='ha', times=3 |
| f(y=b, x=a) | keyword in any order | repeat(times=3, word="ha") | word='ha', times=3 |
| f(a, y=b) | positional first, then keyword | repeat("ha", times=3) | word='ha', times=3 |
The count has to match: two parameters need two arguments, and a call with one or three raises TypeError. And once a keyword argument appears, every argument after it needs a keyword too, so repeat(word="ha", 3) is a syntax error.
Same definition, your choice of values and your choice of form. Watch the binding panel: all three forms fill the parameters with exactly the same values.
A default is a value written into the definition. The call may leave that argument out and the default fills the gap, so one function covers the common case and the unusual one.
A parameter with a default cannot sit before one without, so def tag(currency="EUR", amount) raises a syntax error. Python matches positional arguments by order, and a gap in the middle of that order cannot be filled.
Return ends the call and sends one value to the place that asked for it. The call itself becomes that value, so it can be stored, printed or used in a larger expression.
area(3, 4) + 1 is arithmetic on 12.None.The two definitions differ in one word. Run both and compare the second printed line: the function on the left shows the sum and hands back nothing, the one on the right hands back the sum and shows nothing.
A function may hold more than one return line. The first one reached wins, and the rest of the body is skipped, which removes the need for a long chain of else branches.
Return carries one value, and a tuple is one value holding several. Writing the values with commas builds that tuple, and unpacking it on the other side gives them separate names again.
The scope of a name is the region of the program where that name can be used. A name created inside a function is local: it appears when the call starts, it lives as long as the call lasts, and it is gone the moment the call ends.
text exists only while the call runs, whatever the argument was called outside.Press step and follow the two panels below. The global frame holds the names of the program, the call frame appears when the call starts and vanishes when it ends. The last line asks for a name that no longer exists.
A function can read a global name without any ceremony. Assigning to that name is a different matter: the assignment creates a local name of its own, and the global one is left untouched.
| Question | local name | global name |
|---|---|---|
| Created by | A parameter or an assignment inside a function | An assignment at the left margin |
| Lives for | The length of one call | The length of the program |
| Read from inside a function | Yes, its own function | Yes |
| Written from inside a function | Yes | No, the assignment builds a local name instead |
| Two calls at once | One separate copy per call | One copy, shared |
Python does offer the global keyword, which forces an assignment to reach the global name. Reserve it for rare cases: a function that edits names behind your back is hard to read and harder to correct.
Trace each fragment before pressing Check. The answer button is there for when your trace and the verdict disagree.
def, indent its body, call it from below, and say where control travels on each line.return to hand a value back, predict when None arrives instead, and say how return differs from print.