Lesson 147 of 1455
Python Functions — Writing Your Own Tools
A function is a reusable chunk of code with a name. You'll write three, add type hints, and let AI suggest better names and docstrings.
Builders · AI-Assisted Coding · ~18 min read
def name(): the magic word
A function packages logic so you can call it by name anywhere. Name it clearly and your code reads like a story. Name it 'do_stuff' and your future self will curse your past self.
Type hints (: str, -> str) are optional but massively improve both human and AI understanding.
def greet(name: str, enthusiasm: int = 1) -> str: """Return a friendly greeting. Args: name: the person to greet enthusiasm: number of exclamation marks (default 1) """ bangs = "!" * enthusiasm return f"Hello, {name}{bangs}" print(greet("Maya")) print(greet("Maya", enthusiasm=5))Parameters, arguments, defaults
- A parameter is the name in the definition (name, enthusiasm)
- An argument is the value you pass when calling ("Maya", 5)
- Defaults let callers skip arguments — use them to keep simple calls simple
- Keyword arguments (enthusiasm=5) make calls self-documenting
Return vs. print — the classic beginner mix-up
If a function needs to be usable in another function, it must return, not print.
def add_broken(a, b): print(a + b) # shows the answer — but returns None def add_good(a, b): return a + b # gives the answer back to the caller result = add_broken(2, 3) # prints 5, but result is None total = add_good(2, 3) # total is 5 — usable print(result * 2) # TypeError: NoneType print(total * 2) # 10Mini-exercise: price calculator
- 1Write calc_total(items: list[float], tax_rate: float = 0.08) -> float
- 2Return the sum of items times (1 + tax_rate), rounded to 2 decimals
- 3Add a docstring
- 4Call it with [3.50, 4.25, 1.99] and print the result
- 5Ask your AI assistant to review your code and suggest improvements
Compare the options
| Loose function | Tight function |
|---|---|
| def do(x) | def fetch_user(user_id: int) -> dict |
| No docstring | Docstring with Args and Returns |
| Prints results | Returns results |
| Hard to test | Easy to test |
Big idea: a function is a small contract. Its name says what it does, its signature says how to use it, its docstring says why. AI pair-programming gets dramatically better when you tighten those three things up first.
End-of-lesson quiz
Check what stuck
8 questions · Score saves to your progress.
Lesson help
Questions are best handled with a grown-up here.
For this age range, Tendril keeps freeform AI chat paused until parent/guardian consent and child-safe moderation are fully verified. Use the quiz, notes, and related lessons below, or ask a parent, guardian, teacher, or librarian to work through the question with you.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Builders · 30 min
Your First Git Commit, Explained
Git is a time machine for your code. Before we ship anything, let's learn the three commands that matter and what they actually do under the hood.
Builders · 45 min
Your First Capstone — Ship a Small Project
Bring it all together. Pick one of three starter projects, plan it, build it with AI, and deploy it. You are now a builder who ships.
Builders · 30 min
Python Basics With an AI Pair
Variables, loops, and functions are the atoms of Python. Let an AI help you write them while you learn what each line actually does.
