Star Project --- python crystal ball (function) (2)

☺The author of this article: CSDN--Xiao Cai Xinghang, a 15-year-old small code farmer.

♥♥♥ Personal homepage: Xiaocai Xinghang--blog homepage ♥♥♥

✔Like + ✔Favorite + ✔Comment = (one-click three times)

Selected as the 17th in the list of new writers

The author of this article is still a rookie. If there are any mistakes, please give me some advice.

Hope you guys will support us a lot! work hard together!

I believe that code can change the world!

Finally, thank you all for your continued support! ! !
 

content

pass arguments

position argument

keyword arguments

Definition of function parameters .

A variable number of keyword parameters


pass arguments

A function definition may contain many formal parameters. Therefore, the process of the function call can also contain multiple arguments. There are many ways to pass arguments to functions. Positional arguments can be used if the order of the arguments is the same as the order of the formal parameters. You can also use keyword arguments, where each argument consists of a variable name and a value.

position argument

When calling a function, python must associate each actual parameter in the function call with a formal parameter in the function definition, that is, the actual parameter is passed according to the name of the formal parameter. The simplest way to associate is the order of the actual parameters. This type of association is a positional argument.

def cale(a,b):
    c=a+b
    return c

result=cale(6,8)
print(result)

In the above code block, we first use the def statement to create a function, the function name is cale, and the parentheses operators after the function name define a and b occupy the positions of two actual parameters. Then a and b occupy the position of the actual value, so he is called a formal parameter. a and b are called formal parameters for short. The location of the formal parameters is generally at the definition of the function, so we add two formal parameters as a and b in the parentheses after the parentheses after the def function. In the following result=cale(6,8), 6 and 8 are called the value of the actual parameter, referred to as the actual parameter, and the position of the actual parameter appears at the call of the function.

14

As shown above we can see that 6 is passed to a and 8 is passed to b. This is passed by position. Because 6 is in the first position of the actual parameter. And a is in the first position of the formal parameter, so 6 is passed to a, that is, a=6. Similarly, because 8 is in the second position of the actual parameter and b is in the second position of the definition, b=8. So c=a+b, which is 14.


keyword arguments

The name key-value pair passed to the function as a keyword argument. Associate the name and value directly in the actual parameter. So there is no confusion when passing arguments to functions. The keyword arguments can be ignored, and the order of the arguments in the function call can clearly indicate the value of each use in the function call.
 

def cale(a,b):
    c=a+b
    return c


result=cale(b=6,a=8)
print(result
14

Because the value of b in the actual parameter is 6, the value of a is 8, and the value of b at the definition is 6 because the value of b 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

When a function is defined, a default value is set for the formal parameter, and the actual parameter needs to be passed only when it does not match the default value.

Definition of function parameters

A variable number of positional parameters

 (1) When defining a function, use variable positional parameters when the number of positional arguments to be passed may not be determined in advance.

  (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, when the number of keyword arguments to be passed cannot be determined in advance, use variable keyword parameters.

(2) ·Use ** to define 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}

Guess you like

Origin blog.csdn.net/m0_62069409/article/details/122147385