Function fundamentals

Basic syntax, compared to what you know

Function definitions look similar across most languages you’ve likely used — Python’s version:

Try it — edit and run

No return type declaration, no parameter types by default — consistent with Python’s dynamic, runtime-checked typing. return works like you’d expect; a function with no return statement implicitly returns None.

Default arguments

A parameter can have a default value, making it optional to pass:

Try it — edit and run

Keyword arguments

Arguments can be passed by name instead of position — useful once a function has several parameters, since it removes any ambiguity about which value goes where:

Try it — edit and run

Positional and keyword arguments can be mixed, but once you use a keyword argument, everything after it must also be a keyword argument.

Type hints

Python lets you annotate parameter and return types — these aren’t enforced at runtime (nothing crashes if you ignore them), but they matter more than optional style here: later in this course, tool-calling frameworks generate the schema an LLM sees directly from these type hints. Getting used to writing them now pays off directly.

Try it — edit and run

name: str means “name is expected to be a string,” -> str means “this function returns a string.” Nothing stops you from calling create_agent(123, "x") and Python won’t complain until something inside the function breaks on the wrong type — the type hint is documentation and tooling support, not a compiler guarantee. (Pydantic, in Lesson 0.5, is what actually enforces types at runtime.)

Docstrings

A string literal as the first line inside a function becomes its docstring — accessible via help() or .__doc__, and shown by tools (including IDEs and, later in this course, LLM tool-calling frameworks) as the function’s description:

Try it — edit and run

This specific Args: format is a common convention (Google-style docstrings), not required syntax; a plain one-line docstring is equally valid for simple functions. What matters for later in this course is that this text is often what an LLM actually reads to decide when and how to call a tool — a vague docstring means a worse-informed model.

Check your understanding
1/5

What does a function return if it has no explicit return statement?

Exercise · Graded

Add type hints to every parameter and the return type, write a one-line docstring describing what the function does, and implement the formatting logic: if success is True, return '[tool_name] succeeded: result'; if False, return '[tool_name] failed: result'.