Python passing parameters and parameter types
Python Passing Parameters and Parameter Types. We’ve already covered function definitions and usage in detail. In this section, we’ll discuss some details but an important issue: parameters. For functions that have the necessary parameters set at the beginning, we call the function by typing the function name and passing the parameters in the parentheses, that is, just put the parameters in the function parentheses, like this:
fahrenheit_converter(35)
fahrenheit_converter(15)
fahrenheit_converter(0)
fahrenheit_converter(-3)
In fact, there are two ways to pass parameters:
Now let’s start with the mathematical formula of the trapezoid that seems to have been forgotten by us, and first create a function.
We’ll name the function trapezoid_area
, which translates to the area of a trapezoid. We’ll set the parameters to base_up
(upper base), base_down
(lower base), and height
(height), each separated by a comma. These three values are necessary to calculate the area of a trapezoid, so all three parameters are essential for constructing a function that calculates the area of a trapezoid.
def trapezoid_area(base_up, base_down, height):
return 1/2 * (base_up + base_down) * height
Next, we’ll call the function.
trapezoid_area(1,2,3)
As you can see, the parameters 1
, 2
, and 3
correspond to the parameters base_up
, base_down
, and height
, respectively. This method of passing parameters is called positional arguments.
Next is the second method:
trapezoid_area(base_up=1, base_down=2, height=3)
More intuitively, when calling a function, we append the value we want to pass to each parameter name. This method of passing parameters by name is called keyword arguments.
Think of the process of making a reservation and dining at a restaurant: Your reserved table is typically found using your name, which is a parameter. Your reserved seat is passed in by name, which is the keyword parameter. Next, when the food is served, the dishes are delivered to your table by seat number, which is the position parameter.
You might not understand the purpose of this parameter method right now, but that’s okay. We’ll explain it later along with other topics, and by then, you’ll have a deeper understanding of parameter passing.
The best way to avoid confusion is to create it first. Let’s try solving a more complex problem by calling a function and printing the results in the following ways:
trapezoid_area(height=3, base_down=2, base_up=1) # RIGHT!
trapezoid_area(height=3, base_down=2, 1) # WRONG!
trapezoid_area(base_up=1, base_down=2, 3) # RIGHT!
trapezoid_area(1, 2, height=3) # RIGHT!
- The function parameters in the first line are passed in reverse order. Since they are keyword arguments, this does not affect the function’s normal operation.
- The function parameters in the second line are passed in reverse order, but the third parameter is a positional parameter. Unfortunately, this is incorrect syntax because, if passed positionally, the last parameter should be the height parameter. However, height has already been passed the value 3 by name, so there’s a conflict.
- In the third line, the function parameters are passed in order, with the first two passed as keywords and the last passed as a positional argument. This function works correctly.
- In the fourth line, the function parameters are passed in order, with the first two passed as positions and the last passed as a keyword argument. This function works correctly.
Note: The correct result should be 4.5, which is the area of the trapezoid.
Now, we assign values to a set of variables and then call the function:
base_up = 1
base_down = 2
height = 3
trapezoid_area(height, base_down, base_up)
However, the result of this function call should be 2.5. Why?
If you’re confused, it means you’re confused by parameter and variable naming. Let’s clarify the distinction. First, we define parameter names when defining a function. These names provide context for the function, such as what parameters to pass in, where they come from, and what their types are. The following code snippet may help you navigate parameter naming confusion:
def flashlight (battery1, battery2):
return 'Light!'
Let’s define a function called flashlight that takes two parameters: battery1
and battery2
. Now you go to the store to buy batteries and buy two 600mAh Nanfu batteries. So:
nanfu1 = 600
nanfu2 = 600
flashlight(nanfu1, nanfu2)
Did you understand? Nanfu is a type of battery that makes a flashlight glow. Inserting a Nanfu battery into the flashlight function means we’ve inserted the required batteries. In other words, nanfu1
and nanfu2
are variables that also serve as parameters to the flashlight function. Once passed in, they replace the original battery1
and battery2
, and the parameters are still passed in positionally. battery1
and battery2
are merely placeholders, indicating that the function’s required parameters should be variables or objects related to batteries.
The magic you see is the magic of default parameters, which we’ll talk about soon. Default parameters are optional, meaning that even if you don’t pass them anything initially, the function will still work.
You just need to type this code:
def trapezoid_area(base_up, base_down, height=3):
return 1/2 * (base_up + base_down) * height
Setting a default value for a parameter is very simple. We simply assign a value to the parameter when we define it. This may seem similar to how we pass parameters, but don’t confuse them! This happens when we define a function! Now, we only need to pass in two parameters to proceed normally:
trapezoid_area(1, 2)
You might be wondering, if we set a default value, wouldn’t the height of all trapezoids be fixed at 3? However, that’s not the case. The idea behind default values is to make using functions as simple and effortless as possible. Just like when we install software, there’s a default directory, but if you want to install it elsewhere, you can customize it. The trick we saw with the print function earlier is exactly the same. The default value for the optional sep parameter (which means to separate each printed result by…) is a space, but we pass it back in as '/n'
, which means line break. In other words, each printed number is separated by a line break. Let’s call our own parameters:
trapezoid_area(1, 2, height=15)
Just pass in the values we want, it’s that simple.
Default values aren’t essential to understanding parameter usage, but they can be a helpful little trick to save time. In real-world projects, you’ll often see something like this:
requests.get(url, headers=header)
Note: When requesting a website, headers are optional.
img.save(img_new, img_format, quality=100)
Note: When watermarking an image, the default watermark quality is 100.