>>>
With this definition, the width parameter is required, but the height parameter is optional. If you
call the area2() function with just one argument, Python assigns it to the width variable, as
shown in this example:
>>> area2(10)
The width is: 10
The height is: 20
The area is: 200
>>>
If you call the area2() function with no parameters, you get an error message for the missing
required parameter, as shown here:
Click here to view code image
>>> area2()
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
area2()
TypeError: area2() missing 1 required positional argument: 'width'
>>>
Now you have more control over how to use functions in other Python scripts.
Watch Out!: Ordering of Parameters
When you list the parameters used in your functions, make sure you place any required
parameters first, before parameters that have default values. Otherwise, Python gets
confused and does not know which arguments match with which parameters!
Dealing with a Variable Number of Arguments
In some situations, you might not have a set number of parameters for a function. Instead, a function
may require a variable number of parameters. You can accommodate this by using the following
special format to define the parameters:
def func3(*args):
When you place an asterisk in front of the variable name, the variable becomes a tuple value,
containing all the values passed as arguments when the function is called.
You can retrieve the individual parameter values by using indexes of the variable. In this example,
Python assigns the first parameter to the args[0] variable, the second parameter to the args[1]
variable, and so on for all the arguments passed to the function.
Listing 12.5 shows the script1205.py script, which demonstrates using this method to retrieve
multiple parameter values.
LISTING 12.5 Retrieving Multiple Parameters
Click here to view code image
#!/usr/bin/python3