Python Basics — Functions

less than 1 minute read

Chapter 13 — Functions

Functions are reusable blocks of code that perform a specific task. They are the building blocks of organized, maintainable programs.

What You’ll Learn

Topic Page
Defining Functions Defining Functions →
Parameters & Return Parameters & Return →
Default & Keyword Args Default & Keyword Args →
*args & **kwargs *args & **kwargs →
Docstrings & Annotations Docstrings & Annotations →
Practice Exercises Practice Exercises →

Quick Preview

def greet(name, greeting="Hello"):
    """Return a personalized greeting."""
    return f"{greeting}, {name}!"

print(greet("Alice"))             # Hello, Alice!
print(greet("Bob", "Good morning"))  # Good morning, Bob!

Why Functions Matter

Without Functions With Functions
Code repetition Write once, use many times
Hard to debug Isolate and test each piece
Difficult to read Self-documenting names
Fragile changes Change one place, fix everywhere

Start Learning: Defining Functions →

Updated: