Review
Key Ideas
defdefines a function.- A function does not run until it is called.
- Arguments are values passed into a call.
- Parameters are local names that receive arguments.
returnsends a value back to the caller.print()shows a value but does not return it.- Names created inside a function are local.
- Defaults supply values when arguments are omitted.
- Small tests use known inputs and expected outputs.
- Docstrings help readers understand the function's contract.
Key Syntax
| Syntax | Meaning |
|---|---|
def name(): | define a function |
name() | call a function |
def f(x): | define one parameter |
return value | send a value back |
factor=1 | default parameter value |
f(value=8) | keyword argument |
assert f(2) == 4 | small executable check |
"""Return ...""" | docstring |
Common Mistakes
| Mistake | Fix |
|---|---|
| Defining a function but never calling it | add a call |
| Printing when later code needs the value | use return |
| Calling a function before defining it | put the definition first |
| Depending on hidden outer names | pass values as parameters |
| Expecting local assignments to change outer names | return the new value |
| Using a broad docstring to hide unclear names | improve the names |
Checklist
You are ready to move on if you can:
- define and call a function
- explain argument versus parameter
- predict a returned value
- tell
print()apart fromreturn - trace a local name
- use a simple default argument
- write two small
assertchecks - write a one-sentence docstring
If You Feel Lost
Start with this:
Then change the input and predict the new output before running it.