Function arguments
Function arguments are the values a call passes in, by position or by name, with defaults that make some of them optional.
Last updated: 30 Sep, 2026 · Python 3.14
The functions so far took one value. A cost estimate needs the token count every time, but the price only when it differs from the usual one.
.format(); the run below uses an f-string from Strings, which gives the same text. It also calls age=29 in the definition a keyword argument: in a definition it is a default value, and positional or keyword describes how a call passes a value.def hello(name, age=29):
print(f"My name is {name} and age is {age}")
hello("Krish")
def hello(*args, **kwargs):
print(args)
print(kwargs)
hello("Krish", "Naik", age=29, dob=1990)My name is Krish and age is 29
('Krish', 'Naik')
{'age': 29, 'dob': 1990}hello("Krish") left out age, so the default 29 was used. In the second version, *args collected the values passed by position into a tuple and **kwargs collected the named ones into a dictionary. The cost function below uses a default the same way.
Syntax:
def name(required, optional=default):
...
name(1) # by position
name(1, optional=2) # by keywordGiving a parameter a default
def cost(tokens, price_per_1000=0.15):
return round(tokens / 1000 * price_per_1000, 4)
print(cost(1200))
print(cost(1200, 2.5))
print(cost(1200, price_per_1000=2.5))0.18 3.0 3.0
The value passed in a call is an argument. price_per_1000=0.15 in the definition means: use 0.15 when the caller does not pass one.
The last call names the argument. A keyword argument says what the value is for, which reads better than a bare 2.5 and lets you pass arguments in any order.
Leaving out a required argument
print(cost())Traceback (most recent call last):
File "main.py", line 1, in <module>
print(cost())
TypeError: cost() missing 1 required positional argument: 'tokens'tokens has no default, so a call must give it. The message names the function and the argument that is missing. Delete that line before you add the next example: an error stops the file where it happens.
Collecting extra settings with **
Model calls take many optional settings, and a function that wraps one often passes them on without knowing their names. Two stars in front of a parameter collect every extra keyword argument into a dictionary.
def call_model(prompt, **settings):
print("prompt:", prompt)
print("settings:", settings)
call_model("Sort this ticket")
call_model("Sort this ticket", temperature=0.2, max_tokens=50)prompt: Sort this ticket
settings: {}
prompt: Sort this ticket
settings: {'temperature': 0.2, 'max_tokens': 50}temperature and max_tokens are two settings most model APIs accept: how varied the answer is, and how long it may be. Here they only land in settings, but you will see **kwargs, the usual name, in nearly every framework's source.
Positional vs keyword arguments
| Positional | Keyword | |
|---|---|---|
| Call | cost(1200, 2.5) | cost(1200, price_per_1000=2.5) |
| Order matters | Yes | No |
| Reads as | Numbers you must look up | Says what each value is for |
Where arguments show up in AI code
- Model calls with many optional settings, passed by keyword:
temperature=0,max_tokens=50. - Wrapper functions that pass
**settingsstraight on to the library they wrap.
cost(price_per_1000=2.5, 1200) is a SyntaxError; put the positional values first.Related
- Previous: Functions
- Next: Functions as values
- Reference: More on Defining Functions in the Python tutorial
- Give
costa third parametercurrency="USD"and return an f-string with it. - Call
cost(price_per_1000=2.5, tokens=1200). - Print
settings.get("temperature", 1.0)insidecall_model.
Slow is fine. Stopping is the only problem.