Positional parameters, sometimes called mandatory parameters, mean that the actual parameters must be passed to the function in the correct order. In other words, the number and position of the actual parameters passed in when calling the function must be the same as when defining the function
When calling a function, the number of actual parameters specified must be the same as the number of formal parameters (passing more or less is not sufficient), otherwise the Python interpreter will throw a TypeError exception and prompt for the missing required positional parameters.
Example:
def girl(width, height): return 2 * (width + height) #When calling the function, 2 parameters must be passed, otherwise an error will be thrown print(girl(3))
The output is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[ipython-input-60-e234f2482b4f] in [module]
2 return 2 * (width + height)
3 #When calling the function, 2 parameters must be passed, otherwise an error will be thrown
----> 4 print (girl(3))
TypeError: girl() missing 1 required positional argument: 'height'
It can be seen that the type of exception thrown is TypeError, which specifically refers to the girl() function to burn a necessary height parameter.
Similarly, multi-pass parameters also throw exceptions:
def girl (width, height): return 2 * (width + height) #When calling the function, 2 parameters must be passed, otherwise an error will be thrown print (girl(3,2,4))
The output is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[ipython-input-61-b846438cdbe5] in [module]
2 return 2 * (width + height)
3 #When calling the function, 2 parameters must be passed, otherwise an error will be thrown
----> 4 print (girl(3,2,4))
TypeError: girl() takes 2 positional arguments but 3 were given
From the TypeErroe exception information, you can know that the girth () function only required 2 parameters, but passed in 3 parameters.
When calling a function, the position of the actual parameter passed in must correspond to the position of the formal parameter, otherwise the following 2 results will be produced:
When the actual parameter type and the formal parameter type are inconsistent, and the two types cannot be properly converted between functions, a TypeError exception is thrown.
Example:
def area (height, width): return height * width / 2 print (area ("Python Language Website", 3))
The output is:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
[ipython-input-65-39911434f48f] in [module]
1 def area(height, width):
2 return height * width / 2
----> 3 print (area ("Python Language Website", 3))
[ipython-input-65-39911434f48f] in area(height, width)
1 def area(height, width):
----> 2 return height * width / 2
3 print (area ("Python Language Website", 3))
TypeError: unsupported operand type(s) for /: 'str' and 'int'
The exception message shown above is because the string type and integer value are divided.
When calling a function, if the positions of the specified actual and formal parameters are inconsistent, but their data types are the same, the program will not throw an exception, but it will cause the running result to be different from expected.
For example, design a function to find the area of a trapezoid, and use this function to find the area of a trapezoid with an upper base of 4cm, a lower base of 3cm, and a height of 5cm. But if the incoming positions of the high and low parameters are interacted, the calculation result will cause an error:
def area (upper_base, lower_bottom, height): return (upper_base + lower_bottom) * height / 2 print ("The correct result is:", area (4,3,5)) print ("Error result is:", area (4,5,3))
The output is:
The correct result is: 17.5
Error result is: 13.5
Therefore, when calling the function, be sure to determine the good position, otherwise it is likely to produce such errors in the example, which is not easy to find.
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises