Functions

Trace a get_weather tool function with typed parameters, a default, a docstring and a dict return value, called once with positional arguments and once with keywords.

10 min Free

A function is a named block of code you can run as many times as you like with different inputs. def creates it; the names in the brackets are its parameters; return sends a value back to whoever called it. Everything created inside the function, its local variables, disappears when the call ends.

The function below is written the way an agent tool is written. Each parameter has a type hint (city: str), one has a default (units: str = "C"), the return type is declared (-> dict), and the first line of the body is a docstring explaining what the function does and what the arguments mean. Callers can pass arguments by position, get_weather("Chennai"), or by keyword, get_weather(units="F", city="Mumbai"), where the order no longer matters.

Press Run to execute weather_tool.py. Watch the Variables panel: the locals city, units and temp appear when a call starts and vanish when it returns.

FunctionsReady
# A tool function, written the way ADK expects: typed parameters and a docstring.
TEMPS_C = {"Chennai": 31, "Mumbai": 29}


def get_weather(city: str, units: str = "C") -> dict:
    """Return the current weather for a city.

    Args:
        city: Name of the city, for example "Chennai".
        units: "C" for Celsius or "F" for Fahrenheit.
    """
    temp = TEMPS_C.get(city, 25)
    if units == "F":
        temp = temp * 9 / 5 + 32
    return {"city": city, "temp": temp, "units": units}


report = get_weather("Chennai")
print(report)

report = get_weather(units="F", city="Mumbai")
print(report)

print(get_weather("Kochi")["temp"])
print(get_weather.__doc__.splitlines()[0])

Press Play to run the program line by line, or step with ← →.

00/15

Variables

Nothing defined yet.

Output

Nothing printed yet.

What to notice

  1. def defines; it does not run. Line 5 creates the function object and names it. The body runs only when the function is called, once per call, with fresh locals each time.
  2. Positional fills left to right; keyword fills by name. get_weather("Chennai") puts "Chennai" into city. get_weather(units="F", city="Mumbai") works in any order. Keyword arguments must come after any positional ones.
  3. A default makes a parameter optional. units falls back to "C" when the caller leaves it out. Defaults are evaluated once, when def runs, so never use a list or dict as a default; use None and create it inside.
  4. return ends the call. The caller gets the value; the locals are gone. A function with no return (or a bare return) gives back None.
  5. The docstring is data. Python stores it on the function as __doc__. Write the first line as a short sentence of what the function does, then a blank line, then details such as the arguments.

Tip
Return a dict from tool functions, with a status key. A dict can carry a result, an error message and extra context in one value, and the model reads the keys by name.

Why this matters for AI work

This is the lesson that turns into agents. When you give ADK a function as a tool, it reads the function name, each parameter’s name and type hint, which parameters have defaults, and the docstring, and it builds a schema the model can understand. The model then chooses the arguments and ADK calls your function with keyword arguments. See it happen in Function tools.

Check your understanding

1. What is the value of units inside get_weather("Delhi")?
2. A function runs to the end without a return statement. What does the caller receive?
3. Which call is invalid?