1. Function Description:
A function in a programming language differs from a function in mathematics: The function in mathematics has a parameter (input), and there is a corresponding result (output). Functions in a programming language have input and do not necessarily return results.
A function in a programming language is actually a piece of code that is used to complete the relevant code for a particular function.
The function of a programming language is to realize the reusability of code, and to improve the maintainability, extensibility and readability of code.
Syntax for defining functions in 2.Python:
def function name (parameter): """ function Usage description, Parameter introduction and other document information " " " code block return [expression]
Instance:
Def func1 ():
"' Testing '
Print (' In the Func1 '),
return 0
3. Invocation of functions
def Func2 (x, y , z): Print (x) Print (y) Print (z) Test (+/-)
4. Function parameter description
① parameter: The formal argument, the acceptable parameter specified when the function definition is a formal parameter, such as the Add (a, b) function defined above is a and B is the formal parameter;
② argument: The actual parameter, the actual value that is passed to the function argument when the function is called is an argument, for example, 1 and 9 in the Add (1, 9) function above are the arguments;
③ positional parameters: As the name implies, is related to the order position and quantity of the parameters. When a function is called, the position and number of the argument correspond to the formal parameter, or it will be an error.
Example: two positional parameters
deffunc3 (name, age):Print('NAME:%s'%name)Print('Age :%d'%Age ) func3 ('Peng', 25)#called correctlyfunc3 (05n'Peng')#Wrong Callfunc3 ('Peng', 26,27)#wrong number of parameters
④ Default Parameters
defFUNC4 (Name, age=20): Print('NAME:%s'%name)Print('Age :%d'%Age ) Func4 ('Peng')#correct invocation: The default function must not be passed when the function is called with default argumentsFunc4 ('Peng', 25)#call correctly, age takes a new valueFUNC4 (30)#Note: This value is passed to name and is not passed to age.
⑤ Variable length parameters
Variable (long) parameter: As the name implies, it refers to the parameters that can be changed in length. The popular point is that you can pass any parameter (including 0).
Application Scenarios for variable (long) parameters: usually when writing a method that needs to provide services externally, in order to avoid adding or reducing any new parameters in the future so that all code points calling the method are modified, a variable-length form parameter can be used.
#*args: Receive n positional parameters, convert narimoto Group mode
def Func5 (*args):
Print (args) Func5(1,2,3,4,5,5) Func5 (#args=tuple ([1,2,4,5,6])def func6 (x, *args): Print(x) print(args) Func6 ( 1,2,3,4,5,6,7,8,9)
⑥ keyword Parameters
As the name implies, refers to the function when called by the keyword to specify which parameters are specified for which parameter, such as Name= "Tom", age=10,x=6 and so on
defFunc7 (x, Y, z):Print(x)Print(y)Print(z) func7 ()#correct invocation: corresponds to formal parameter one by oneFunc7 (y=2,x=3,z=5)#correct invocation: not related to keyword parameter orderFunc7 (1,3,y=3)#error: Positional argument follows keyword argumentFunc7 (1,y=3,6)#error: Positional argument follows keyword argumentFunc7 (1,x=3,y=6)#error: Func7 () got multiple values for argument ' x 'Func7 (1,z=3,y=6)#called correctlyFunc7 (z=3,y=6,7)#报错: positional argument follows keyword argument
⑦n keyword Parameters
#**kwargs: The way to convert multiple key parameters into a dictionarydefFUNC8 (* *Kwargs):Print(Kwargs)#return: {' age ': ', ' name ': ' Peng ', ' sex ': ' F '} Print(kwargs['name']) Print(kwargs[' Age']) Print(kwargs['Sex']) Func8 (name='Peng', Age =18,sex ='F')defFUNC9 (name,**Kwargs):Print(name)Print(Kwargs)#return: {' age ': +, ' sex ': ' F '}FUNC9 ('Peng', age=18,sex='F')
Python Functions and variables