Functions and Function Arguments
Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the function, we supply the values in the same way. Note the terminology used - the names given in the function definition are called parameters whereas the values you supply in the function call are called arguments .
What Parameters and Arguments Actually Are
When you write a function, you need a way to let it receive data from the outside world. That is where parameters and arguments come in. A parameter is a named placeholder that you write inside the function definition. An argument is the actual value you supply when you call the function. These two terms sound similar, but they refer to different things at different times: parameters exist in the definition, arguments exist in the call. Understanding this distinction is crucial because it clarifies exactly how data flows from your code into the function.
Parameter: A named variable listed in the parentheses of a function definition that acts as a placeholder for a value the function will receive.
Argument: The actual value you provide when calling a function, which gets assigned to the corresponding parameter.
How Function Definitions Structure Parameters
Parameters are specified within the pair of parentheses in the function definition, separated by commas. The order matters: the first parameter will receive the first argument, the second parameter will receive the second argument, and so on.
Mapping Arguments to Parameters
When you call a function, the arguments you provide are assigned to the parameters in order. The first argument you pass becomes the value of the first parameter, the second argument becomes the value of the second parameter, and so on. This assignment happens automatically when the function is called, and it is the mechanism that lets the function receive and work with your data.
The number of arguments you provide must match the number of parameters in the function definition, unless the function uses default values. If you provide too few or too many arguments, you will get an error.
Arguments Can Be Literals or Variables
You can pass arguments to a function in two ways: as literal values directly in the function call, or as variables that hold values. In both cases, it is the value that matters, not whether it came from a literal or a variable. The function receives the value the same way regardless of its source.
Calling a Function with Literals and Variables
Imagine a function print_max(a, b) that finds and prints the larger of two numbers. Show how to call it with literal numbers, and then with variables.
Call with literal arguments: You write print_max(5, 8). The value 5 is assigned to parameter a, and the value 8 is assigned to parameter b. The function executes with these values and prints 8.
Call with variable arguments: You write x = 5 and y = 8, then call print_max(x, y). The function looks up the value of x (which is 5) and assigns it to parameter a. It looks up the value of y (which is 8) and assigns it to parameter b. The function executes exactly the same way as before.
Key insight: The function does not care whether the arguments came from literals or variables. It only cares about the values. In both cases, parameter a receives 5 and parameter b receives 8, so the function behaves identically.
Both calls produce the same result because the values passed to the parameters are the same.
Default Argument Values
For some functions, you may want to make some parameters optional and use default values in case the user does not want to provide values for them. This is done by appending to the parameter name in the function definition the assignment operator (=) followed by the default value. When you call the function without providing an argument for that parameter, the default value is used instead.
Parameters with default values must come after parameters without default values in the function definition. You cannot have a required parameter after an optional one.
Using Default Argument Values
Write a function say(text, times) that prints a string a certain number of times. Make times optional with a default value of 1, so if the user does not specify how many times, the string prints just once.
Define the function with a default value: In the function definition, you write def say(text, times=1). The parameter times has a default value of 1. The parameter text has no default, so it is required.
Call with both arguments: You call say('Hello', 3). The argument 'Hello' is assigned to text, and the argument 3 is assigned to times. The function prints 'Hello' three times.
Call with only the required argument: You call say('Hello'). The argument 'Hello' is assigned to text. Since you did not provide a value for times, it uses the default value of 1. The function prints 'Hello' once.
Default values let you call the function with fewer arguments when you want to use the default behavior.
Common Mistakes with Parameters and Arguments
Confusing the terms parameter and argument
Parameters are names in the definition; arguments are values in the call. Using the wrong term makes it harder to communicate clearly about your code.
Fix:
Say 'I passed the argument 5' or 'the argument 5 was assigned to the parameter x'. Remember: parameters are in the definition, arguments are in the call.Providing the wrong number of arguments
The function expects two arguments but only received one. There is no value to assign to parameter b, so the function cannot run.
Fix:
Count the parameters in the definition and provide exactly that many arguments in the call, unless some parameters have default values.Forgetting that argument order matters
Arguments are assigned to parameters by position, not by name. The first argument goes to the first parameter, regardless of what you intended.
Fix:
Always provide arguments in the same order as the parameters appear in the function definition.Placing a required parameter after an optional one
Python cannot determine which arguments are optional and which are required if a required parameter comes after an optional one.
Fix:
Always put parameters with default values at the end of the parameter list, after all required parameters.
How Arguments Flow Into Function Execution
Understanding the flow of data from a function call into the function body is essential. When you call a function, the arguments you provide are evaluated, and their values are assigned to the parameters in order. Then the function body executes with those parameter values available. This happens every time you call the function, and each call gets its own fresh set of parameter assignments.
Practice: Parameters and Arguments
Write a function called multiply(x, y) that takes two parameters. Then write three separate function calls: one that passes two literal numbers, one that passes two variables, and one that passes a mix of a literal and a variable. For each call, identify which argument is assigned to which parameter.
Hints
- Remember that the first argument goes to the first parameter, the second argument goes to the second parameter.
- You can use any numbers you like for the literal arguments.
- Create variables with any names you like, assign them values, and then use those variable names as arguments.
Define a function greet(name, greeting='Hello') with one required parameter and one optional parameter with a default value. Write two function calls: one that provides both arguments, and one that provides only the required argument. Explain what happens in each case.
Hints
- The parameter with a default value must come after the required parameter.
- When you do not provide an argument for a parameter with a default value, that default is used.
- Think about what value the greeting parameter will have in each call.
Summary
You now understand the distinction between parameters and arguments and how they work together to pass data into functions. Parameters are the named placeholders in the function definition. Arguments are the actual values you provide when you call the function. Arguments are assigned to parameters by position: the first argument goes to the first parameter, the second to the second, and so on. You can pass arguments as literal values or as variables. For flexibility, you can give parameters default values, making them optional. Understanding this mechanism is fundamental to writing and calling functions effectively.
Key Takeaways
- Parameters are named placeholders in a function definition; arguments are the actual values you provide when calling the function.
- Arguments are assigned to parameters by position: the first argument to the first parameter, the second to the second, and so on.
- You can pass arguments as literal values or as variables; the function receives the value either way.
- Default argument values make parameters optional by providing a fallback value if no argument is supplied.
- Parameters with default values must appear after required parameters in the function definition.