First, what is the function
Function is similar to a tool that can be called repeatedly.
Second, why should function
Preventing code redundancy; readability is poor.
Third, the use
3.1
1 defined function -----> make a wapen
2 calls the function -----> use it
## naming function and variable names, it requires meaningful.
Three categories IV function
4.1 no-argument function
DEF FUNC (): # () has no parameters Print ( ' Hello World ' ) FUNC ()
4.2 empty function
DEF func2 (): pass # code blocks with the first pass in place, empty function func2 ()
4.3 There are function parameters
DEF func1 (A, B): # () with a parameter with a reference function Print (A, B) func1 ( 1,2) # A = B = 2. 1
4.4 function name assigned to the variable
DEF func3 (): Print ( ' pig eggs ' ) f = func3 # , by assigning the function name to the variable f, f call can also call the function f ()
Fifth, the function's return value
5.1 does not write return: the return none
def u(): print(1) # u() # 1 print(u()) #None
5.2 Write only return: only the end effect of the code, or none
def u(): print(1) return # u() # 1 print(u()) #None
5.3 write return none: and the same effect on a
def u(): print(1) return None # u() # 1 print(u()) #None
5.4 return a return value: returns the results can be used as a variable name use
DEF S (A, B): return A, B S ( 1,2) # invisible result d = S (1,2) # The result of the function to the variable d can see the results Print (d) # (. 1, 2)
5.5 return return multiple values: 5.5.1 return multiple values into default Ganso return
5.5.2 function returns not want to be modified
5.5.3 can specify your own type of return
DEF func1 (): return . 1, " 2 " Print (func1 ()) # (. 1, '2') returns ancestral
A =. 1 B = 2 C = ' . 3 ' D = [. 4,. 5 ] E = { ' name ' : ' Sean ' } DEF FUNC (A, B, C, D, E): return A, B, C, D, E Print (FUNC (A, B, C, D, E)) # (. 1, 2, '. 3', [. 4,. 5], { 'name': 'Sean'}) # returns the specified type
## return is a function marks the end of the function body of code, as long as the implementation of return, the end of the function is executed.
Six function parameters
Parameter Type 6.1
Parameter: the parameter to a predetermined function definition phase, corresponding to the variable name, if the definition of the variable.
Argument: function parameters passed in the call phase, the equivalent value of a variable defined variables.
DEF foo (A): # A is a parameter Print (A) foo ( . 1) # . 1 is the argument
6.2 parameter passing mode
6.2.1 positional parameters: passaged through reference position.
def s(a,b): return a,b print(s(1,2)) #(1, 2) a=1 b=2
6.2.2 keyword arguments: Specifies the parameters of mass participation.
def s(a,b): return a,b print(s(b=1,a=2)) #(2, 1) b=1,a=2
6.2.3 default parameters: function definition phase has passed parameter, if passed a new parameter in the argument, it will use the new parameters. *** When the type of the variable parameters can not be passed.
DEF S (A, B, c = 3 ): return A, B, C S ( 1,2 ) Print (S (1,2)) # (. 1, 2,. 3) The default is also coming out c = 3
DEF S (A, B, C =. 3 ): return A, B, C # S (1,2, C =. 4) Print (S (1,2, C =. 4)) # (. 1, 2,. 4) specify c = 4, the given values put out
6.2.4 vararg
6.2.4.1 * args : receiving parameters for all positions of the overflow, the received values are stored in one yuan ancestors.
demonstration:
Solution:
# * Args DEF F (A, B, * args): # receiving overflow location parameter Print (A, B, args) # args parameter will overflow into one yuan progenitor f (1,2,3,4,5) # 12 (3, 4, 5)
DEF F (A, B, * args): # receiving overflow location parameter Print (A, B, * args) # args parameter will overflow broken F (l, 2,3) # . 1 2. 3
6.2.4.2 ** kwargs : receiving so overflowing keyword arguments, the received values are stored in a dictionary.
demonstration:
Solution:
# ** kwargs DEF F (A, B, ** kwargs): Print (A, B, kwargs) # kwargs: a dictionary will return overflow parameter F (. 1, 2, C =. 3) # . 1 {2 ' c ': 3}
DEF F (A, B, kwargs **): # Receive Overrun keyword parameter Print (A, B, * kwargs) # * kwargs will overflow parameter dictionary key return f (b = 1, a = 2 , C =. 3) # 2. 1 C
## effect * of : break up the type of container you pass