Positional and Keywords arguments
Last updated
Last updated
In programming, calling a function is the act of executing the function with specific parameters. In this section, we will explore two methods of calling a function: positional arguments and keyword arguments.
Consider the function feet_to_meters()
, which takes two integer parameters: one for the number of feet
and one for the number of inches
. The function returns the measurement in meters.
The code defines two constants INCHES_TO_METERS
and INCHES_PER_FEET
. INCHES_TO_METERS
is a conversion factor that will be used to convert inches to meters. INCHES_PER_FOOT
is the number of inches in one foot. The function calculates the total number of inches by multiplying the number of feet by the constant INCHES_PER_FOOT
and adding the number of inches. The function then multiplies the total number of inches by the constant INCHES_TO_METERS
to convert inches to meters. The function returns the result of the calculation in meters. Using constants in this way can make the code easier to read and maintain, especially if the conversion factors are used in multiple places throughout the code.
When calling the function with two integer values for arguments, the given arguments are matched to the formal arguments in the function definition based on their position. The first argument 5
is matched to the first formal argument, which is feet
, and the second argument 9
is matched to the second formal argument, which is inches
. The formal arguments are then assigned the values passed in the parameters, and the body of the function is executed with these two values. Once the expression is evaluated, the result 1.7526
is returned and displayed on the console.
However, if we call the same function but change the order of the values passed in the parameters, we obtain a different result. This is because the position of the arguments matters in positional arguments.
Alternatively, we can call a function using keyword arguments, where we use the names of the arguments in the function call. In this case, we use the keywords feet
and inches
and assign them a value within the call.
When executing the function, the value 5
will be assigned to the formal parameter feet
, and the value 9
will be assigned to the formal parameter inches
. If we reverse the order of the arguments in the function call, 9
is matched to inches
, and 5
is matched to feet
. We will still obtain the same result as the previous function call. However, it is important to remember that using a keyword that is not part of the formal arguments will raise an error. For example, trying to call the function with the keyword yard
will raise an error.
It is also possible to mix positional and keyword arguments.
In this case, 5
is matched to the first formal argument, and then the keyword parameter inches
is matched to the formal argument inches
.
However, it is essential to be careful when mixing positional and keyword arguments. Positional parameters must be declared first, followed by keyword arguments; failing to do so will result in a syntax error.
Overall, both positional and keyword arguments are useful for calling functions with specific parameters. Knowing how to use both methods correctly can make your code more efficient and easier to read.
In Python functions, you can specify default values for parameters. This means that if the function is called without providing a value for that parameter, the default value will be used. Default parameters are a convenient feature that can make your code more convenient to reuse and more readable.
Defining a default parameter is simple. You just need to include the default value as part of the parameter definition in the function header. Here's an example:
In this function, the parameter name
has a default value of "World"
. So, if we call the function without providing a value for name
, it will use the default value:
However, if we provide a value for name
, the default value will be overridden:
Default parameters can also be used in conjunction with other parameters. Here's an example:
In this function, the base
parameter is required, but the exponent
parameter has a default value of 2. So, we can call the function with only the base
parameter:
Or we can provide a value for exponent
:
It's important to note that default parameter values are only evaluated once, when the function is defined. This means that if you use a mutable object as a default parameter value (like a list or dictionary), you need to be careful, as changes made to that object will persist across function calls.
Here's an example to illustrate this:
In this function, the lst
parameter has a default value of an empty list. If we call the function and don't provide a value for lst
, it will use the default value of an empty list:
If we call the function again without providing a value for lst
, it will use the same list object as before:
This behavior can be surprising and lead to hard-to-find bugs. To avoid this, you can use None
as a default value and create a new object in the function if the parameter is None
:
Now, if we call the function and don't provide a value for lst
, it will create a new list object:
And if we call the function again without providing a value for lst
, it will create a new list object again:
Default parameters are a useful feature in Python functions that allow you to specify default values for parameters. However, you need to be careful when using mutable objects as default parameter values, as changes made to those objects will persist across function calls.
Functions can take a variable number of arguments. A parameter name that begins with *
gathers arguments into a tuple.
In Python, functions can accept a variable number of arguments using the special parameter *args
. This is a powerful feature that allows developers to write functions that can accept any number of arguments without needing to specify them in the function definition. In this section, we will explore what *args
is, how it works, and how to use it effectively in Python functions.
*args
is a special syntax that allows a function to accept an arbitrary number of arguments. It is a shorthand for variable-length arguments and is denoted by an asterisk (*
) followed by the parameter name (args is a common convention, but any name can be used).
The *args
parameter is typically used when you don't know how many arguments a function will receive at the time of definition. Instead, the arguments are passed at runtime, and the function will handle them accordingly. This makes it a useful tool for functions that need to work with an unknown number of inputs, such as mathematical operations, aggregations, or sorting algorithms.
Here's an example of a function that takes a variable number of arguments using *args:
In this example, the function concatenate
accepts any number of string arguments, and concatenates them into a single string. The parameter *args
collects all arguments passed to the function into a tuple, which can be iterated over using a loop or other sequence operations.
To call the concatenate
function, you can pass any number of string arguments:
As you can see, the function accepts any number of arguments, and the result is concatenated into a single string.
In addition to accepting any number of arguments, *args
can be combined with other parameters in a function definition. For example, a function can accept a fixed number of arguments as well as a variable number of arguments:
In this example, the function print_args
accepts a mandatory parameter name
and a variable number of arguments using *args
. The arguments passed to *args
will be collected into a tuple and printed to the console.
To call the print_args
function, you can pass any number of arguments:
As you can see, the function accepts any number of arguments, and the output shows the name of the function followed by a tuple of arguments.
One thing to note when using *args
is that the arguments passed to it must be of the same data type. If the arguments are of different types, the function may raise an error or return unexpected results.
The complement of gather is scatter. If you have a sequence of values and you want to pass it to a function as multiple arguments, you can use the *
operator. For example, divmod
takes exactly two arguments; it doesn't work with a tuple:
But if you scatter the tuple, it works:
But sum
does not.
Exercise: Many of the built-in functions use variable-length argument tuples. For example, print
, max
and min
can take any number of arguments but sum
does not.
Write a function called sum_all
that takes any number of arguments and returns their sum.
In conclusion, the *args
parameter is a useful tool for Python developers to handle functions that accept an unknown number of arguments at runtime. By using this feature, developers can write more flexible and dynamic functions that can accept a variable number of arguments without having to specify them in the function definition.