Skip to main content
Functions are the primary tool for avoiding repetition and structuring your code into logical units. Once you define a function, you can call it anywhere — with different inputs each time — and get consistent, predictable results. This page teaches you everything from basic function definitions to scope rules, lambdas, and practical patterns for processing data.

Defining and Calling a Function

Use the def keyword, a name, parentheses, and a colon. The function body must be indented.
By convention, function names use snake_case — lowercase words separated by underscores. Choose names that describe what the function does, not how.

Parameters and Arguments

Parameters are the variable names listed in the function definition. Arguments are the actual values you pass when calling the function.

Positional Arguments

By default, Python matches arguments to parameters by their position (left to right):

Default Parameter Values

Give a parameter a default value to make it optional at call time. Parameters with defaults must come after parameters without defaults:

Keyword Arguments

Pass arguments by name to improve readability and ignore order:
Use keyword arguments when calling functions with many parameters or when parameter names are descriptive enough to clarify the call without reading the definition.

Return Values

Use return to send a value back to the caller. Python exits the function immediately when it hits return.
A function without an explicit return returns None.

Returning Multiple Values

Separate multiple return values with commas — Python packs them into a tuple, and you can unpack them on the calling side:

Docstrings

A docstring is a string literal on the first line of the function body. It documents what the function does, its parameters, and its return value. IDEs and help() display it automatically.

Variable Scope

A variable’s scope is the region of code where it is visible. Python follows the LEGB rule — it searches for a name in this order:
  1. Local — inside the current function
  2. Enclosing — inside any outer (enclosing) function
  3. Global — at the top level of the module
  4. Built-in — Python’s built-in names (len, print, …)

Modifying a Global Variable

Use the global keyword inside a function to write to a module-level variable:

Modifying an Enclosing Variable

Use nonlocal in a nested function to write to a variable in the enclosing function’s scope:
Avoid modifying global state inside functions when possible. Pass values in as arguments and return new values instead — this keeps functions predictable and easy to test.

Lambda Functions

A lambda is a nameless function written as a single expression. Use it for short, throwaway logic — especially as arguments to sorted(), map(), or filter().

Common Lambda Patterns

Use lambdas only for simple one-line expressions. If your logic needs more than one line, write a regular def function instead.

*args and **kwargs

Accept a variable number of arguments using *args (positional) and **kwargs (keyword):

Practical Example: Processing a List of Numbers

The following example brings together parameters, defaults, return values, docstrings, and lambdas to build a small number-crunching utility:

Key Takeaways

One responsibility

Each function should do one thing. If you find yourself writing a function that does three unrelated things, split it into three functions.

Parameters over globals

Pass data in through parameters and return new data. Avoid reading or writing global variables inside functions.

Defaults for convenience

Use default parameter values to make common cases easy without removing flexibility for advanced callers.

Docstrings always

Write a docstring for every function that isn’t completely obvious. Future you will be grateful.