SoFunction
Updated on 2024-11-14

python crystal ball (function) in detail

pass an actual parameter

A function definition can contain many formal parameters. Therefore function calls can also contain multiple real parameters. There are many ways to pass real parameters to a function.Positional real parameters can be used if the order of the real parameters is the same as the order of the formal parameters. It is also possible to use keyword real parameters, where each real parameter consists of a variable name and a value.

positional real parameter

When calling a function, python must associate each real parameter in the function call to a formal parameter in the function definition, that is, pass the real parameter according to the name of the formal parameter, the simplest way to associate the real parameters is the order of the real parameter, and this way of association is the positional real parameter.

def cale(a,b):
    c=a+b
    return c
 
result=cale(6,8)
print(result)

In the above code block we first create a function using def statement, the function name is cale, the parenthesis operator after the name of the function defines a, b occupies the position of the two actual parameters. Then a and b occupy the positions of the actual values so they are called formal parameters. a and b are called formal parameters for short. The location of the formal parameter is usually in the definition of the function, so we add the two formal parameters a and b in the parentheses after the def function. in the result=cale(6,8) below, 6 and 8 are called the values of the actual parameters, or real parameters for short, and the location of the real parameters is found in the call to the function.

14

As shown above we can see that 6 is passed to a and 8 is passed to b. This is a positional pass. Because 6 is in the first position of the real parameter, and a is in the first position of the formal parameter, 6 is passed to a. Since 6 is in the first position of the real parameter and a is in the first position of the formal parameter, 6 is passed to a which means a=6. Similarly since 8 is in the second position of the real parameter and b is in the second position of the definition, b=8. Hence c=a+b, which is 14.

Keyword parameters

The name key-value pair passed to the function when the keyword real parameter. The name and value are associated directly in the real parameter. Therefore, there is no confusion when passing real parameters to a function. Keyword real parameters can be used without regard to the order of the real parameters in the function call, and can clearly indicate the use of each value in the function call.

def cale(a,b):
    c=a+b
    return c
 
 
result=cale(b=6,a=8)
print(result

14

Because of the assignment of 6 to b in the real parameter and 8 to a at the definition of b because of the assignment of 6 to b so the value of b at the definition is 6 and the value of a is 9 so the value of a at the definition is 8.

Default parameter values for function definitions

Default values are set for formal parameters when the function is defined, and real parameters need to be passed only if they don't match the default values.

Definition of function parameters

-Variable number of positional parameters

 (1)When defining a function, variable positional parameters are used when it may not be possible to determine in advance the number of positional real parameters to be passed.

(2) Use * to define a variable number of positional parameters.

(3) The result is a tuple.

def new(*args):
    print(args)
 
new(10)
new(11,22,33)
(10,)
(11, 22, 33)

Variable number of keyword parameters

(1)·When defining a function, there is no way to determine in advance the number of keyword real parameters to be passed, so that the variable keyword form parameter.

(2) - Use ** to define a variable number of keyword parameters.

(3) - The result is a dictionary.

def new(**args):
    print(args)
 
new(a=10)
new(a=11,b=22,c=33)
{'a': 10}
{'a': 11, 'b': 22, 'c': 33}

summarize

to this article on the python crystal ball (function) explains the article is introduced to this, more related python function content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!