Function
Also: 函式 · 函數 · method · def · 函式定義
A named, reusable piece of code: you give it inputs and it hands back an output.
When you will meet it
An AI 'tool' is a function in code, and the list of tools a model may use is essentially a list of function descriptions. Being able to read 'what is this function called, what parameters does it take, what does it return' is how you tell what an agent can actually do — and whether what it just asked to do is reasonable.
An analogy
Like a vending machine: a fixed coin slot (inputs), fixed buttons (parameters), a fixed dispenser (return value). You need not know how it works inside, only what goes in and what comes out.
Minimal example
def get_weather(city):
"""回傳某個城市的天氣(這裡假裝查過氣象 API)。"""
return f"{city}:晴,26 度"
print(get_weather("台北")) # 台北:晴,26 度
print(get_weather("香港")) # 香港:晴,26 度The name follows def, the inputs go in brackets, and return is what comes back. A 'tool definition' handed to a model is just those three things described in words — name, parameters, return value. That is why how a function is named and documented directly affects whether the model picks the right tool.
What people get wrong
- Thinking a function is merely 'code folded up'. Its real value is a stable interface: as long as the name and the inputs and outputs do not change, the inside can be rewritten without touching anything that calls it.
- Assuming that the model 'calling' a tool means the work happened. The model only returns 'I would like to call get_weather with city = Taipei'; the layer of code around it is what actually runs the function.
Related terms
Next
- Function Calling Basics: Making the LLM Actually Use Tools26 minChinese only
- 你的第一個 Agent:一個會用工具的 Hello World28 minChinese only