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.
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.
# 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 ← →.
Variables
Nothing defined yet.
Output
Nothing printed yet.
What to notice
defdefines; 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.- Positional fills left to right; keyword fills by name.
get_weather("Chennai")puts"Chennai"intocity.get_weather(units="F", city="Mumbai")works in any order. Keyword arguments must come after any positional ones. - A default makes a parameter optional.
unitsfalls back to"C"when the caller leaves it out. Defaults are evaluated once, whendefruns, so never use a list or dict as a default; useNoneand create it inside. returnends the call. The caller gets the value; the locals are gone. A function with noreturn(or a barereturn) gives backNone.- 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.
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.