Phase: 4. Functions | Estimated time: 2 hours | Milestone Project: No
- Modules 000-030 (all fundamentals)
By the end of this module, you will be able to:
- Define functions using the
defkeyword - Use the
returnstatement to send values back to the caller - Distinguish between parameters and arguments
- Call functions with positional arguments
- Understand basic function scope (local vs global)
- Write Google-style docstrings for functions
- Follow Python naming conventions for functions
Functions are the building blocks of any non-trivial program. Instead of writing the same code over and over, you wrap it in a function and call it by name. Functions make code reusable, testable, and readable.
Use the def keyword, a name, parentheses, and a colon:
def greet():
"""Print a greeting message."""
print("Hello, world!")Call it by using the name followed by parentheses:
greet() # Hello, world!Functions can send a value back to the caller with return:
def add_one(x):
"""Add one to x and return the result.
Args:
x: The number to increment
Returns:
x + 1
"""
return x + 1
result = add_one(5) # result = 6Without return, a function returns None implicitly.
- Parameter: The variable listed in the function definition.
- Argument: The value you pass when calling the function.
def square(n): # n is a parameter
return n * n
print(square(4)) # 4 is an argumentVariables created inside a function are local — they don't exist outside:
def spam():
eggs = 10 # local variable
print(eggs)
spam() # 10
print(eggs) # NameError!def multiply(a: float, b: float) -> float:
"""Multiply two numbers and return the product.
Args:
a: First factor
b: Second factor
Returns:
Product of a and b
"""
return a * b┌─────────┐ Call greet() ┌──────────────┐
│ Caller │ ──────────────────▶ │ greet() │
│ │ │ │
│ │ ◀────────────────── │ print(...) │
└─────────┘ return None │ return None │
└──────────────┘
- Use
snake_casefor function names - Use descriptive verbs:
calculate_total,get_user_name - Avoid abbreviations:
get_usernotget_usr - Follow PEP 8 — lowercase with underscores
- Forgetting parentheses when calling:
greetvsgreet()— the first refers to the function object, not the result. - Forgetting
return: Without it, the function returnsNone. - Modifying global variables inside a function without
global(covered in Module 033). - Printing instead of returning: Use
returnso callers can use the result.
- Define a simple function
greet(name)that returns a string. - Call it with different names.
- Define
add(a, b)that returns the sum. - Experiment with local variables — try to access one outside the function.
defdefines a function;returnsends a result back.- Parameters are placeholders; arguments are actual values.
- Variables inside functions are local by default.
- Use Google-style docstrings to document every function.
- Name functions with
snake_caseand descriptive verbs.
- Python docs: Defining Functions
- PEP 257 — Docstring Conventions
- Google Python Style Guide — Docstrings
Continue to **Module 032: Function Arguments (Default, Keyword, *args, kwargs).