VMware ESXi and vSphere Cluster Management
Python Positional Arguments in Functions
Learn how Python matches positional arguments to function parameters, why argument order matters, and how to fix missing or extra argument TypeErrors.
When you call a Python function, you can provide values for it to use. Python must decide which value belongs to which input. With positional arguments, it makes that decision from the order of the values in the call.
Parameters and arguments
A function is a reusable block of code that performs an action or calculates a result. A function definition is the code that creates the function, including its name, parameters, and body. A function call runs the function.
A parameter is a named placeholder in a function definition. An argument is a value supplied when the function is called.
def greet(name):
print("Hello", name)
greet("Mina")
nameis a parameter because it appears in the function definition."Mina"is an argument because it is supplied in the function call.
In this lesson, the arguments are positional arguments: Python assigns each argument to a parameter according to its location in the call.
Defining a function with multiple parameters
A function may need more than one input value. Write multiple parameters inside the parentheses, separating them with commas. Use names that describe the purpose of each value.
def describe_person(name, pronoun):
print("Name:", name)
print("Pronoun:", pronoun)
This function has two parameters: name and pronoun. At the moment Python reads the definition, no particular person or pronoun has been supplied. The parameters receive values later, when the function is called.
How positional argument matching works
Python matches ordinary positional arguments from left to right:
- The first argument is assigned to the first parameter.
- The second argument is assigned to the second parameter.
- Each later argument is assigned to the parameter in the same position.
def describe_person(name, pronoun):
print("Name:", name)
print("Pronoun:", pronoun)
describe_person("Avery", "they")
| Function parameter order | Argument position in the call | Assigned value |
|---|---|---|
name (first parameter) | first argument | "Avery" |
pronoun (second parameter) | second argument | "they" |
The output is:
Name: Avery
Pronoun: they
The values are assigned before the function body runs. The first value does not search for a parameter whose meaning seems appropriate; it goes to the first parameter.
Calling a function with all required positional arguments
A required parameter has no default value, so the caller must provide a corresponding argument. The call below supplies one argument for each required parameter:
def show_coordinates(x, y):
print("x =", x)
print("y =", y)
show_coordinates(4, 9)
The mapping is:
xreceives4.yreceives9.
The output is:
x = 4
y = 9
Because both required parameters received values, the function can run normally.
Why argument order matters
Positional arguments must normally be passed in the same logical order as the parameters in the definition. Python usually does not infer what you intended from the values themselves.
def show_coordinates(x, y):
print("x =", x)
print("y =", y)
show_coordinates(9, 4)
This call is valid, but the assignments have changed:
xreceives9.yreceives4.
The output is:
x = 9
y = 4
If the intended point was (4, 9), the program has produced incorrect meaning without raising an error. Both values are acceptable numbers, so Python has no reason to reject the call.
Common positional argument outcomes
| Call pattern | Outcome | Reason |
|---|---|---|
| Correct number and order of arguments | Function runs as intended | Each parameter receives the corresponding value by position |
| Arguments reversed | Function may run with unintended output | Values are assigned by order, not intended meaning |
| Too few arguments | TypeError | At least one required parameter did not receive a value |
| Too many arguments | TypeError | More positional values were supplied than the function accepts |
Missing required positional arguments
If a function has multiple required parameters and the call supplies too few arguments, Python raises a TypeError.
def send_message(recipient, message):
print("To:", recipient)
print(message)
send_message("Sam")
The first argument fills recipient, but no second argument fills message. A traceback commonly reports a missing required positional argument named message. The parameter name in the error identifies the required parameter that did not receive a value.
Fix the call by supplying every required value:
send_message("Sam", "Your appointment is confirmed.")
Too many positional arguments
The opposite problem occurs when a call supplies more positional values than the definition accepts.
def add_tax(amount):
print(amount)
add_tax(20, 0.2)
add_tax() declares only one parameter, so the second positional argument has nowhere to go. Python raises a TypeError. Remove the extra argument, or revise the function definition if the function truly needs another input:
def add_tax(amount, tax_rate):
print(amount * (1 + tax_rate))
add_tax(20, 0.2)
Reusing one function with different data
One benefit of parameters is that you can reuse the same instructions instead of duplicating the function body for every set of data.
def greet(name, language):
print("Hello", name, "- language:", language)
greet("Mina", "English")
greet("Luis", "Spanish")
greet("Noor", "Arabic")
Each call creates a new assignment of argument values to that call's parameters:
- First call:
nameis"Mina"andlanguageis"English". - Second call:
nameis"Luis"andlanguageis"Spanish". - Third call:
nameis"Noor"andlanguageis"Arabic".
The same function body runs each time, but the parameter values belong to the current call.
Parameters versus arguments: a quick comparison
| Concept | Where it appears | Example |
|---|---|---|
| Parameter | Function definition | def greet(name): |
| Argument | Function call | greet("Mina") |
A useful question is: “Am I looking at the function being created, or at code that runs it?” Names in the definition are parameters; values in the call are arguments.
Brief boundary: keyword arguments
A keyword argument associates a value with a parameter by name, using the form parameter_name=value. For example:
describe_person(name="Avery", pronoun="they")
This lesson focuses on positional arguments, where location determines the assignment. Keyword arguments have additional ordering and mixing rules, so study them separately in the keyword arguments lesson.
Printing versus returning a value
The examples above use print() inside the function. Printing displays information for a user or learner. A return statement sends a value from the function back to the code that called it.
def add(first, second):
return first + second
result = add(3, 4)
print(result)
Here, add(3, 4) returns 7, which is stored in result. Return statements are the next step when you want a function's result to be used by other code.
Troubleshooting positional argument errors
Missing argument
def book_room(guest, nights):
print(guest, nights)
book_room("Kai")
Cause: the definition has two required parameters, but the call supplies one argument. Fix: provide a value for every required parameter:
book_room("Kai", 2)
Values appear under the wrong labels
def display_item(product, price):
print("Product:", product)
print("Price:", price)
display_item(19.99, "Notebook")
Cause: the arguments are in the wrong order. Fix: pass the product first and the price second:
display_item("Notebook", 19.99)
Exam-relevant summary
- A parameter is a named placeholder in a function definition.
- An argument is a value supplied during a function call.
- Positional arguments are assigned from left to right.
- The first argument goes to the first parameter, the second goes to the second, and so on.
- A required parameter without a default value must receive an argument.
- Too few or too many positional arguments commonly cause a
TypeError. - Reversing valid values can produce incorrect output without an error.
- Keyword arguments use parameter names and are a related alternative to positional arguments.
print()displays a value;returnsends a value back to the caller.