Review

Key Ideas

  • def defines 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.
  • return sends 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

SyntaxMeaning
def name():define a function
name()call a function
def f(x):define one parameter
return valuesend a value back
factor=1default parameter value
f(value=8)keyword argument
assert f(2) == 4small executable check
"""Return ..."""docstring

Common Mistakes

MistakeFix
Defining a function but never calling itadd a call
Printing when later code needs the valueuse return
Calling a function before defining itput the definition first
Depending on hidden outer namespass values as parameters
Expecting local assignments to change outer namesreturn the new value
Using a broad docstring to hide unclear namesimprove 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 from return
  • trace a local name
  • use a simple default argument
  • write two small assert checks
  • write a one-sentence docstring

If You Feel Lost

Start with this:

Then change the input and predict the new output before running it.