When we call a function, the transfer of data is always inseparable between the main function and the calling function. With the transfer of data, it is the transfer of parameters . The purpose of parameters is to pass data to functions.
For example, we bought a juicer. When we add apples, apple juice will come out. When we add watermelon, watermelon juice will come out. The parameters play such a role. The parameters are passed to the target through the main function. Call a function, and the called function will return us a result.
Next let's learn about parameter passing.
To learn function parameter passing, we first need to understand what formal parameters and actual parameters are. Let's continue with the above example. We have a container to hold fruit in the juicer. We put the fruit in this container, and then the juicer starts running. The formal parameters and actual parameters have a similar relationship.
The formal parameters are the parameters defined in parentheses when we define the function. We will use this parameter to write code inside the function, and the actual parameters are the parameters passed in when the function is called. The result returned by the function is based on this actual parameter. parameters instead of formal parameters.
Look at the following example:
>>>A=30>>>defget_salary(days):...salary=days*300...print(salary)...>>>get_salary(A)9000
In the above example, the parameter days used when the function was created is a formal parameter. When we call the function, A is the actual parameter, which is actually the parameter that is to be brought into the function.
When we create a function, we can define multiple formal parameters in parentheses. When we call it, the number and position of the parameters need to be consistent with the ones created.
What will happen if the function we create has 2 formal parameters, but we only use 1 actual parameter when calling it? See the example below.
>>>defget_message(address,number):...print(address,number)...>>>get_message('Suzhou')Traceback(mostrecentcalllast):File<stdin>,line1,in<module>TypeError:get_message ()missing1requiredpositionalargument:'number'
We can find that when the number of actual parameters is not equal to the number of formal parameters, an exception will be thrown, which specifically means that a necessary position parameter number is missing.
When the positions of parameters are inconsistent, two errors will occur. One is to report an error directly, because we will define different types according to the nature of the parameters when passing parameters, so data type errors will throw exceptions.
Another error is that the data type of the parameter passed in is correct, but the position is wrong, which will cause wrong output results.
In order to improve the readability of the program, keyword arguments can also be used when calling functions.
We learn about keyword arguments by calculating the volume of the container.
>>>defvolume(length,width,height):...volume=length*width*height...print('The volume is: ',volume)...>>>volume(length=20,width=30 ,height=3) volume is: 1800
Using keyword parameters can save the process of defining variables, assign values to parameters directly when the function is called, then pass them to the function, and finally return the result. In this transfer method, the difference in parameter position does not affect the output result.
When we define a function, we can define an initial value for the function's parameters, so that if no actual parameters are given when we call the function, the function will use the default parameters.
Look at the following example:
>>>defvolume(length=100,width=100,height=10):...volume=length*width*height...print(volume)...>>>volume()#Does not give actual parameters When the actual parameters are given, the default parameters 100000>>>volume(10,10,10)# will be used. The actual parameters will be passed and the output result will be 1000.
Through the above example, if we have given a default value when creating the function, then the default parameters will be automatically used if we do not give actual parameters when using the function.
In Python, the number of parameters of a function can be changed, which means that the number of parameters can be uncertain. Such parameters are called variable parameters. There are two types of variable parameters. One is to add * before the parameters. In this way, the variable parameters are passed in the form of tuples when passing them. The other is to add ** before the parameters. In this way, the variable parameters are passed in the form of tuples. The parameters are passed in the form of a dictionary when passing them. We mainly introduce the first method.
Look at the following example:
>>>defadd_number(*number):...add_num=0...foriinnumber:...add_num+=i...print(add_num)...>>>add_number(1,2,3,4,5 )15
When using variable parameters, these parameters will be stored in a tuple and passed to the function, so when we want to use these parameters, we can use traversal or index values.
The parameter passing of functions is roughly the same. If you want to learn functions well, you must master the passing of parameters. The correct passing of parameters is crucial in the use of functions.