Skip to content
PythonFunctionsbeginner18 min read

Parameters and Arguments

Understand positional and keyword arguments, how Python matches them to parameters, and how to avoid common function-call errors.

In this lesson, you will learn

  • ✓Distinguish parameters in function definitions from arguments in function calls.
  • ✓Explain how positional arguments are matched to parameters.
  • ✓Use keyword arguments to specify parameter names explicitly.
  • ✓Combine positional and keyword arguments using Python's calling rules.
  • ✓Predict the output of functions called with different argument arrangements.
  • ✓Recognize common argument errors, including missing, duplicate, and unexpected arguments.

1. Parameters and arguments: the difference

A parameter is a name written inside a function definition. It acts as a local name that receives a value when the function is called.

An argument is a value or expression supplied in a function call. For example, in def greet(name):, name is a parameter. In greet('Sam'), the string 'Sam' is an argument.

Arguments provide the information a function needs to perform its task. A function can have one parameter, several parameters, or no parameters. The arguments supplied by a call must follow the function's calling rules.

Python commonly supports positional arguments and keyword arguments. These describe how values are associated with parameters, rather than different kinds of values.

Identify the parameter and argument

Identify the parameter and argument
python
def greet(name):
print(f'Hello, {name}!')
 
greet('Sam')
Output
Hello, Sam!

name is the parameter because it appears in the function definition. 'Sam' is the argument because it is supplied in the function call. Python associates the argument 'Sam' with the parameter name. The function body uses name in the f-string. The print() statement displays Hello, Sam!.

Note

Parameters belong to a function definition; arguments are supplied when the function is called. Keeping these terms distinct makes function declarations and calls easier to understand.

2. Positional arguments

A positional argument is matched to a parameter according to its position in the function call. The first positional argument is matched to the first eligible parameter, the second to the second, and so on.

For example, if a function is defined as def subtract(a, b):, then subtract(9, 4) binds 9 to a and 4 to b. The function calculates 9 - 4 and returns 5.

The order matters when the parameters have different meanings. Calling subtract(4, 9) reverses the values assigned to a and b and therefore changes the result.

Positional arguments are concise, but a call with many parameters may be harder to read when the purpose of each value is not obvious.

Pass two positional arguments

Pass two positional arguments
python
def subtract(a, b):
return a - b
 
print(subtract(9, 4))
print(subtract(4, 9))
Output
5 -5

In subtract(9, 4), a receives 9 and b receives 4. The expression a - b evaluates to 9 - 4, which is 5. In subtract(4, 9), a receives 4 and b receives 9. The expression now evaluates to 4 - 9, which is -5. The arguments have the same values in both calls but appear in different positions, so the results differ.

3. Keyword arguments

A keyword argument supplies a value by naming the parameter in the function call. The general syntax is parameter_name=value.

For example, describe(city='Delhi', name='Ira') explicitly associates 'Delhi' with city and 'Ira' with name. Python does not need to infer these associations from the order in which those keyword arguments appear.

Keyword arguments can make calls easier to read, especially when a function has several parameters. They also allow keyword-capable parameters to be supplied in a different order from their definition.

The name in a keyword argument must match a parameter name that the function accepts. An unknown keyword name can cause TypeError.

Supply arguments by parameter name

Supply arguments by parameter name
python
def describe(name, city):
print(f'{name} lives in {city}.')
 
describe(city='Delhi', name='Ira')
Output
Ira lives in Delhi.

The function defines two parameters: name and city. The call supplies city='Delhi', so the city parameter receives 'Delhi'. The call supplies name='Ira', so the name parameter receives 'Ira'. Although city appears first in the call, Python matches each keyword argument using its name. The function displays Ira lives in Delhi.

Note

A keyword argument uses the parameter's name, not a variable's descriptive label or a string containing the name. For example, name='Ira' is valid when the function has a name parameter.

4. Combining positional and keyword arguments

Python allows a function call to combine positional and keyword arguments. However, positional arguments must come before keyword arguments in a regular function call.

For example, describe('Ira', city='Delhi') first matches 'Ira' positionally to name, then matches 'Delhi' by keyword to city.

A call such as describe(name='Ira', 'Delhi') is invalid because a positional argument follows a keyword argument. Python reports a SyntaxError before executing the call.

Do not provide the same parameter twice. For example, describe('Ira', name='Mina') attempts to assign a value to name positionally and another value by keyword, so Python raises TypeError.

A valid combination

A valid combination
python
def describe(name, city):
print(f'{name} lives in {city}.')
 
describe('Ira', city='Delhi')
Output
Ira lives in Delhi.

The first argument, 'Ira', is positional and matches name. The keyword argument city='Delhi' supplies the value for city. The positional argument appears before the keyword argument, so the call is valid. Both required parameters receive values. The function displays Ira lives in Delhi.

Caution

In a regular function call, do not place a positional argument after a keyword argument. Also, do not supply the same parameter once positionally and again by keyword. These mistakes cause errors instead of producing a valid function call.

5. Common argument errors

A function call must provide values according to the function's parameter requirements. If a required parameter receives no value, Python raises TypeError.

Too many positional arguments can also raise TypeError when the function does not accept that many. Similarly, a keyword argument whose name is not accepted by the function can cause TypeError.

A parameter cannot normally receive two values in the same call. This happens, for example, when a parameter is assigned by a positional argument and again by a keyword argument.

These errors are different from SyntaxError. SyntaxError occurs when the call itself violates Python's grammar, such as placing a positional argument after a keyword argument.

The examples below are separate programs. Each intentionally demonstrates a call that fails; do not combine the invalid calls into one program if you want to inspect each error independently.

Missing a required argument

Missing a required argument
python
def greet(name):
print(f'Hello, {name}!')
 
greet()
Output
TypeError: greet() missing 1 required positional argument: 'name'

The function requires one argument for its name parameter. The call greet() supplies no arguments. Python cannot bind a value to the required parameter. The call raises TypeError instead of executing the function body. The wording of Python's error message may vary slightly by Python version.

Note

Error messages are useful diagnostic clues. Identify the function's parameter names, count the supplied arguments, and check how each argument is matched before changing the code.

6. Trace argument matching step by step

When you are unsure how a call works, read the function definition first and list its parameters in order. Then inspect the call: match positional arguments by position and keyword arguments by name.

After every required parameter has a value, the function can execute normally. If a required parameter is missing or a parameter is supplied more than once, the call fails.

Tracing this process is particularly helpful when a function has several parameters or when positional and keyword arguments are mixed.

Trace mixed argument matching

Trace mixed argument matching
python
def order(item, quantity, city):
print(f'{quantity} {item} ordered for {city}.')
 
order('notebooks', city='Delhi', quantity=2)
Output
2 notebooks ordered for Delhi.

The definition declares parameters in this order: item, quantity, city. The first positional argument, 'notebooks', is assigned to item. The keyword argument city='Delhi' is assigned to city. The keyword argument quantity=2 is assigned to quantity. All three parameters have values, so the function executes and displays 2 notebooks ordered for Delhi.

Note

For beginner-level function calls, a reliable strategy is to match positional arguments first, then match keyword arguments by name, and finally check that every required parameter has exactly one value.

Common pitfalls to avoid

Real debugging lessons and exact syntax contrasts to avoid runtime bugs

#1 Calling a function with positional arguments in the wrong order.

Why it happens: Positional arguments are matched to parameters by order, so swapping their order can assign values to the wrong parameters.

Incorrect Approach
script.py
python
Calling a function with positional arguments in the wrong order.
Correct Solution
script.py
python
Check the parameter order in the function definition. Use keyword arguments when naming each value would make the call clearer.

Key takeaway: Check the parameter order in the function definition. Use keyword arguments when naming each value would make the call clearer.

#2 Confusing parameters with arguments.

Why it happens: A parameter is a name in the function definition, while an argument is the value or expression supplied by a caller.

Incorrect Approach
script.py
python
Confusing parameters with arguments.
Correct Solution
script.py
python
Parameters are names in the definition; arguments are the values supplied by a call.

Key takeaway: Parameters are names in the definition; arguments are the values supplied by a call.

#3 Assuming keyword arguments must appear in definition order.

Why it happens: Keyword arguments are matched by parameter name and can be provided in a different order from the definition.

Incorrect Approach
script.py
python
Assuming keyword arguments must appear in definition order.
Correct Solution
script.py
python
Keyword arguments can be reordered because Python matches them by parameter name.

Key takeaway: Keyword arguments can be reordered because Python matches them by parameter name.

#4 Placing a positional argument after a keyword argument.

Why it happens: After a keyword argument appears in a normal function call, later ordinary positional arguments are not allowed because their matching would be ambiguous.

Incorrect Approach
script.py
python
Placing a positional argument after a keyword argument.
Correct Solution
script.py
python
Place regular positional arguments before keyword arguments in the call.

Key takeaway: Place regular positional arguments before keyword arguments in the call.

#5 Supplying a parameter twice.

Why it happens: Providing the same parameter positionally and by keyword assigns it twice and raises TypeError.

Incorrect Approach
script.py
python
Supplying a parameter twice.
Correct Solution
script.py
python
Check whether a positional argument has already filled a parameter before supplying that parameter by keyword.

Key takeaway: Check whether a positional argument has already filled a parameter before supplying that parameter by keyword.

#6 Using a keyword name that the function does not accept.

Why it happens: Keyword names must match parameters accepted by the function; an unexpected name raises TypeError unless the function accepts arbitrary keyword arguments.

Incorrect Approach
script.py
python
Using a keyword name that the function does not accept.
Correct Solution
script.py
python
Compare the keyword name with the function's parameter names and correct any spelling or naming mismatch.

Key takeaway: Compare the keyword name with the function's parameter names and correct any spelling or naming mismatch.

#7 Ignoring missing required arguments.

Why it happens: Every required parameter must be supplied by position, keyword, or another supported argument mechanism, unless a default value is defined.

Incorrect Approach
script.py
python
Ignoring missing required arguments.
Correct Solution
script.py
python
Check that every required parameter receives a value unless the function definition allows it to be omitted.

Key takeaway: Check that every required parameter receives a value unless the function definition allows it to be omitted.

Ready for hands-on challenges?

Test your skills with shuffled coding challenges in the Practice Arena.

Practice Arena →

Technical and placement interview questions

Frequently tested interview questions with detailed, full answers (click to expand)

Q1:What is the difference between a parameter and an argument?
Answer:
A parameter is a name declared in a function definition. An argument is the value or expression supplied in a function call. For example, in def greet(name):, name is a parameter; in greet('Sam'), 'Sam' is an argument.
Q2:What are positional arguments?
Answer:
Positional arguments are matched to parameters according to their position in the call. For def subtract(a, b):, subtract(9, 4) binds 9 to a and 4 to b.
Q3:What are keyword arguments?
Answer:
Keyword arguments provide values by naming the corresponding parameters. For example, describe(city='Delhi', name='Ira') matches the values by name rather than relying on their order.
Q4:Can positional and keyword arguments be used in the same call?
Answer:
Yes. For example, describe('Ira', city='Delhi') uses a positional argument for name and a keyword argument for city. In a regular function call, positional arguments must come before keyword arguments.
Q5:What happens if the same parameter is supplied twice?
Answer:
Python raises TypeError when a parameter receives a value positionally and another value by keyword. For example, greet('Ira', name='Mina') supplies two values for name.
Q6:What happens if a required argument is missing?
Answer:
Python raises TypeError when a call does not provide a value for a required parameter. Default parameter values and other argument-handling features can change which arguments are required, but those rules must be defined by the function.
Q7:When should keyword arguments be preferred?
Answer:
Keyword arguments are useful when parameter names make the call clearer, particularly when a function has several parameters. They can also allow values to be supplied in an order different from the parameter definition.
Q8:What is the difference between SyntaxError and TypeError in these examples?
Answer:
SyntaxError occurs when the call violates Python's syntax, such as placing a positional argument after a keyword argument. TypeError occurs for calls that are syntactically valid but violate the function's argument requirements, such as omitting a required argument or supplying a parameter twice.

Key takeaways

  • Parameters are names in function definitions; arguments are values supplied in function calls.
  • Positional arguments are matched to parameters by order.
  • Keyword arguments are matched to parameters by name and can be supplied in a different order.
  • In regular function calls, positional arguments must come before keyword arguments.
  • A parameter should receive one value in a call; supplying it twice causes TypeError.
  • Missing required arguments and unexpected keyword names can cause TypeError.
  • A positional argument after a keyword argument causes SyntaxError in a regular function call.
  • Trace each argument to its parameter before trying to predict the function's behavior.
Was this lesson helpful?
Found an error? Report an issue

Related Topics & Next Steps

4 Recommended Lessons

Strengthen your Python foundation with these complementary core lessons: