Functions
Functions
Section titled βFunctionsβWhat it means
Section titled βWhat it meansβA function is a reusable, named block of code defined with def. It can accept parameters (with optional default values), and returns a value with return (or None implicitly if thereβs no return). Functions are the primary tool for avoiding repeated code and giving logic a clear name.
Examples
Section titled βExamplesβdef greet(name, greeting="Hello"): return f"{greeting}, {name}!"
print(greet("Alice")) # Hello, Alice!print(greet("Bob", greeting="Hi")) # Hi, Bob!
def add(*numbers): # accepts any number of positional args return sum(numbers)
print(add(1, 2, 3, 4)) # 10
def describe(**details): # accepts any number of keyword args for key, value in details.items(): print(f"{key}: {value}")
describe(name="Alice", age=30)Common mistake
Section titled βCommon mistakeβUsing a mutable default argument (like a list or dict) β the default is created once when the function is defined, not each time itβs called, so it silently persists and accumulates state across calls.
def add_item(item, items=[]): # BUG: same list reused every call items.append(item) return items
print(add_item("a")) # ['a']print(add_item("b")) # ['a', 'b'] <- unexpected! carried over from last call
# Fixed:def add_item(item, items=None): if items is None: items = [] items.append(item) return itemsQuick practice
Section titled βQuick practiceβ-
What does a function return if it has no explicit
returnstatement?Answer
None. -
Whatβs the danger of
def f(items=[]):?Answer
The default list is created once at function-definition time and shared across every call that doesn't pass its ownitems, so mutations accumulate unexpectedly between calls. -
Whatβs the difference between
*argsand**kwargs?Answer
*argscollects extra positional arguments into a tuple;**kwargscollects extra keyword arguments into a dict.