Function Basics in Python
From the Python cheat sheet ยท Functions ยท verified Jul 2026
Function Basics
Define functions with parameters, defaults, and return values.
python
def greet(name, greeting="Hello"):
"""Return a greeting string."""
return f"{greeting}, {name}!"
result = greet("Alice")
result = greet("Bob", greeting="Hi")๐ก Use / to mark positional-only params, * to mark keyword-only params
โก Functions can return multiple values as a tuple โ unpack with a, b = func()
๐ Default arguments are evaluated once โ never use mutable defaults like def f(x=[])
๐ข Add docstrings as the first line in a function for documentation
functionsparametersreturn