Python theorytheory 0/50 · 0%
Functions · easy

12. Functions and default arguments

def, parameters, defaults, and return values.

Functions are declared with `def`. Parameters can have default values, and calls can use positional or keyword arguments.

python
def mint(amount, decimals=18):
    return amount * 10 ** decimals

mint(2)             # uses default decimals
mint(2, decimals=6) # keyword override

A function without an explicit `return` returns `None`. Mutable default arguments (like `[]` or `{}`) are a classic pitfall because they are created once and shared across calls — prefer `None` and create the mutable object inside the function.

Check your understanding

  1. 1. What does a function return if it has no `return` statement?

  2. 2. Why are mutable default arguments risky?

  3. 3. What does `mint(2, decimals=6)` demonstrate?