What is the function? Functional programming

The parameter can include the position parameter
def test (x, y) // define a function, the name of the function is test There are 2 parameters in the function, one is x and the other is y

    print (x) // Print x 
print (y) // Print Y
test (1,2) // Position parameter: use the test function, the first position x transfer value is 1 The second position y transfer value is 2 When this function is executed, the function defined at that time is to print xy. After execution, it will print out 1, 2
             Keyword parameter: The above is the position parameter, then another way is the keyword parameter if test (y = 1, x = 2) At this time, the print is 2,1 because the x printed in the function is printed first y

test (x, y) X and Y are formal parameters, but only formal parameters but test (1, 2) 1, 2 are actual parameters are actual parameters that actually occupy memory

    Default parameter: The default parameter is to give a specific parameter a default value when defining the parameter. If it is not filled in when calling, the default value is used. If the new value is filled, the new value is used
def test (x, y = 2):

    print (x)
    prnt (y)

test (1) At this time the output is 1, 2 
test (1,3) At this time the output is 1, 3

parameter groups: When you have n actual parameters, do you want to Do you define n formal parameters? The answer is not that
we can use the * way
def test (* args) when defining the formal parameters : * hhh behind is the name taken, this args can be any value
    print (args)

test (1,2,3,4,5 ... n) The final output is (1,2,3,4,5 ... n)

The parameter group can be combined with the default parameter position parameter keyword parameter  
def test (x, y = 2, * args)
print (x)
print (y)
print (args)

test (1, 2, 3, 4, 5, 6 , 7 ..... n) At this time, x-1 y = 2 The rest of the content is put into args. The

keyword parameters are converted into a dictionary
def tst (** kwargs)
print (kwargs)
test (a = 1 ”, b =“ 2 ”) At this time, the output result is“ a ”:“ 1 ”,“ b ”:“ 2 ”

Guess you like

Origin www.cnblogs.com/ztcbug/p/12735110.html