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
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
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
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
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
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
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
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
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
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
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
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.
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.
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.
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.
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.
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.
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.
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.
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?
Q2:What are positional arguments?
Q3:What are keyword arguments?
Q4:Can positional and keyword arguments be used in the same call?
Q5:What happens if the same parameter is supplied twice?
Q6:What happens if a required argument is missing?
Q7:When should keyword arguments be preferred?
Q8:What is the difference between SyntaxError and TypeError in these examples?
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.
Related Topics & Next Steps
4 Recommended LessonsStrengthen your Python foundation with these complementary core lessons: