You define a function in Python with the def keyword, a name, a parenthesised parameter list, a colon and an indented body. The function runs when you call it by name, and it hands a result back with return; without return it gives back None.
This article covers parameters of every kind, return values, type hints, docstrings, scope and the mistakes that cause the most bugs. Examples target Python 3.12 and later, and they also run on earlier 3.x releases unless noted.
A function definition has a fixed shape:
| Part | Purpose |
|---|---|
def | Keyword that starts the definition |
function_name | Lowercase name with underscores (snake_case, per PEP 8) |
| Parameters | Names that receive the values passed by the caller |
| Docstring | Optional string that documents the function; available as function_name.__doc__ |
| Body | Indented block that runs on each call |
return | Sends a value back to the caller and ends the function |
A function is an ordinary object. You can store it in a variable, put it in a list, or pass it to another function. The body does not run at definition time, only when the function is called.
A parameter is the name in the definition. An argument is the value you pass in the call.
Keyword arguments make calls easier to read, especially when a function takes several values of the same type.
Default values are evaluated once, when the function is defined. Never use a mutable object such as a list as a default:
With items=[] in the signature, every call that omits the argument would share the same list and keep growing it.
*args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dictionary.
A / in the signature makes everything before it positional-only. A * makes everything after it keyword-only.
Use keyword-only parameters for options and flags so call sites stay self-explanatory.
A function can return one value, several values, or nothing.
Returning several values really returns one tuple, which the caller unpacks. A function that reaches the end without return, or uses a bare return, gives back None. Code after a return in the same branch never runs.
Type hints do not change how the code runs, but editors and checkers such as mypy and pyright use them to catch mistakes.
Built-in generics such as list[float] and the X | None union syntax work on current Python versions; the old typing.List and typing.Optional forms are no longer needed in new code.
Names assigned inside a function are local to it. Reading a module-level name works, but assigning to it creates a new local variable unless you declare global.
Prefer passing values in and returning results over global; it keeps functions easy to test. For state that must live between calls, use a class or a closure with nonlocal.
A lambda is a single-expression anonymous function, best kept for short callbacks:
If the logic needs more than one expression, or a name, define it with def.
calculate_area or parse_config."Cannot divide by zero." from a numeric function forces every caller to check the type of the result.Yes. Separate the values with commas and Python returns them as a tuple, which you can unpack into several variables in one line.
A parameter is the variable name in the function definition. An argument is the actual value you supply when you call the function.
Either the function has no return statement, or the return sits in a branch that did not run. Check that every path through the function returns the value you expect.
The def statement must have run before the call executes, but not necessarily before the calling code is written. A function can call another one defined later in the file, as long as both exist when the call happens.
Define functions with def name(parameters):, document them with a docstring, and return results with return. Use defaults, *args, **kwargs and the / and * markers to shape the signature, avoid mutable defaults, add type hints, and raise exceptions instead of returning error strings.