Python functions of Python ensemble

Time:2021-6-22

1. Formal parameters and actual parameters

When using functions, formal parameters and actual parameters are often used. They are both called parameters. The difference between them will be explained through the function of formal parameters and actual parameters.

The differences between formal parameters and actual parameters are as follows

Formal parameters: when defining a function, the parameters in brackets after the function name are “formal parameters”.

Actual parameters: when a function is called, the parameters in brackets after the function name are regarded as “actual parameters”, that is, the parameters provided by the function caller to the function are called actual parameters.

According to the different types of actual parameters, it can be divided into two cases: passing the value of actual parameters to formal parameters and passing the reference of actual parameters to formal parameters. When the actual parameter is an unchangeable object, the value is passed. When the actual parameter is a variable object, the reference is passed. In fact, the basic difference between value passing and reference passing is that after value passing, if the value of formal parameter is changed, the value of actual parameter will not change. After reference passing, if the value of formal parameter is changed, the value of actual parameter will also change.

For example: def demo (obj)

*Print (“original value, obj)”

  obj+=obj

Print (“value passing call”)

Mot = “only when you are chased can you really run”

Print (“before function call, MOT)”

demo(mot)

Print (“after function call, MOT)”

print(“Reference passing call“)

List = * [“Qimeng”, “Xiangning”]

Print (“before function call):*list**)

*demo(mot)

Print (“after function call):,list)

Results after implementation:

Value passing call

Before function call: only when you are chased can you really run

Original value: only when you are chased can you really run

After function call: only when you are chased can you really run

Reference passing call

Before function call: [Qimeng, Xiangning]

Original value: [Qimeng, Xiangning]

After function call: [Qimeng, Xiangning, Qimeng, Xiangning]

2. Position parameters

Location parameters, also known as required parameters, must be passed to functions in the correct order, and the number and location of calls must be the same as when they are defined.

Note:

1. When calling the function, the number of specified actual parameters must be consistent with the number of formal parameters, otherwise, an exception will be thrown, indicating that the necessary positional parameters are missing.

2. When calling a function, if the position of the specified actual parameter is inconsistent with that of the formal parameter, but their data types are consistent, the exception will not be thrown, but the result is inconsistent with the expectation.

3. Keyword parameters

Keyword parameter refers to using the name of formal parameter to determine the input parameter value. When specifying the actual parameter in this way, it does not need to be exactly the same as the position of formal parameter, as long as the parameter name is written correctly, which can avoid the trouble that users need to remember the parameter position, and make the function call and parameter transfer more flexible and convenient.

For example, there is a function fun_ BMI () contains three parameters: weight, height and person

Using keyword parameters, you can call functions as follows:

fun_ BMI (height = 1.83, weight = 60, person =’zhang San ‘)

This work adoptsCC agreementReprint must indicate the author and the link of this article