Functions (P2)¶
This page introduces functions: named blocks of code that perform a specific task.
Functions are essential because they let you:
- organize code into logical units
- reuse logic without copying
- make programs easier to read, test, and maintain
- describe what the program does at a higher level
You have already been using functions. This page gives you the vocabulary to talk about them clearly.
What Is a Function?¶
A function is a named sequence of statements that runs when it is called.
Example:
defstarts a function definitiongreetis the function name- the indented block is the function body
The function does nothing until it is called.
Calling a Function¶
To run a function, use its name followed by parentheses:
This executes the code inside the function body.
Why Functions Matter¶
Without functions:
- programs become long and repetitive
- logic is harder to understand
- changes must be made in many places
With functions:
- each function has a clear purpose
- complex programs are built from small pieces
- code reads more like a plan
Example:
This is how pipelines are expressed in Python.
Functions with Parameters¶
Parameters allow functions to accept input values.
Calling the function:
nameis a parameter"Alice"is an argument
Functions with Return Values¶
Some functions return a result.
Calling the function:
returnsends a value back to the caller- execution stops at
return
Functions That Return Nothing¶
Many functions exist only to create side effects:
- writing files
- logging
- printing
- modifying external state
Example:
These functions return None.
Type Hints (Why We Use Them)¶
Type hints make function inputs and outputs explicit.
Benefits:
- clearer intent
- better editor feedback
- easier collaboration
Type hints do not change how Python runs.
Function Docstrings¶
A docstring explains what a function does.
def compute_stats(values: list[float]) -> dict:
"""Compute summary statistics for numeric values."""
Docstrings:
- appear immediately after
def - use triple quotes
- describe purpose, arguments, and return values
Functions as Building Blocks¶
In your projects, functions typically do one thing:
- read data
- extract values
- compute statistics
- write output
- log activity
Example pattern:
Each function is small and focused.
main() as the Coordinator¶
Most scripts define a main() function.
main():
- coordinates the workflow
- calls other functions
- contains little logic itself
Conditional Execution Guard¶
This pattern appears in every project:
Meaning:
- run
main()if this file is executed as a script - do nothing if the file is imported as a module
This enables reuse.
Common Mistakes¶
Forgetting parentheses when calling¶
Putting too much logic in one function¶
Functions should be focused and readable.
How Functions Fit with Branching and Repetition¶
- Operators produce True/False
- Branching decides which function runs
- Repetition calls functions many times
- Functions organize the work
Together, they form readable, maintainable programs.
Reminders¶
- Functions group related code
- Parameters pass data in
- Return values pass data out
main()organizes the workflow- Small functions lead to clear programs