Home / Python Fundamentals / Functions
Lesson 4 of 8

Functions

Concept

A function is a reusable block of code. Define it with def, give it a name, optionally accept inputs (parameters), and optionally return a result. Every route handler in your FastAPI apps is a function.
m3shdup/app/intrinsic.py
def idle_minutes() -> int:
    """How long before an agent is considered idle."""
    try:
        return int(os.environ.get("M3SHDUP_INTRINSIC_IDLE_MINUTES", "10"))
    except ValueError:
        return 10

# Calling the function
minutes = idle_minutes()  # returns 10 (or whatever the env var says)

What's happening

def starts the definition. idle_minutes is the name. The -> int is a type hint — it tells you (and tools) this returns an integer, but Python doesn't enforce it. return sends the value back to whoever called the function.

Quick Check — 3 questions

1. What keyword starts a function definition?

2. What does return 10 do inside a function?

3. What does -> int mean in def f() -> int:?