First, the function
function definition: A function refers to the collection of a set of statements by a name (function name) to encapsulate, to execute this function, just call their function name to
In different machine languages, the names of functions vary. In basic it is called subroutine (sub-process or subroutine), in Pascal is called procedure (process) and function, in C only function, in Java is called method.
function form:
def func (Kwargs,*kwargs,**kwargs):# function name func ()... # Operations within a function Pass # operations within a function
Call Function: Func ().
Kwargs: For single-variable delivery
*kwargs: Used for the delivery of tuples, and can pass multiple parameters, can be used to pass the parameter number of indeterminate
**kwargs: Used for dictionary delivery, and can pass multiple parameters, can be used to pass the parameter number of indeterminate
Like what:
defTest (kwargs,*kwargs1,**kwargs2):Print(Kwargs)Print(KWARGS1)Print(KWARGS2) test ("Long") Test ("Long"," Love"," You") Test ("Long"," Love"," You", who ="Long", what =" Love", whom =" You")
Test ("long", who = ' long ', what = ' love ', whom = "You")
The results are printed as follows:
long () {}#passed the Kwargs,*kwargs and **kwargs the other emptyLong (' Love',' You') {}#passed the Kwargs and *kwargs,**kwargs emptyLong (' Love',' You'){'W.H.O.':'Long','whom':' You',' What':' Love'}#passed Kwargs,*kwargs and **kwargs.Long () {'W.H.O.':'Long','whom':' You',' What':' Love'}#passed the Kwargs,**kwargs,kwargs to null
Second, function parameter transfer
The general calling function will pass the arguments to the variables and complete the execution of the program.
For a function of a single variable, it is necessary to keep the actual participation argument one by one corresponding and pass it. or specify a corresponding parameter to manipulate. Like what:
def Test (x, y ): Print (x) Print (y) test(2,3) test (y=3,x=4)
2343
Of course, the two can be combined, but it is important to ensure that the parameters of the keyword are passed after other types are passed.
Test (4,y=3) test (y=3,4)#error
Second, the system will error: positional argument follows keyword argument
Third, local variables and global variables
Variables defined in subroutines are called local variables, and variables defined at the beginning of a program (first level) are called global variables. The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable. When a global variable has the same name as a local variable: Local variables work within subroutines that define local variables, and global variables work in other places.Iv. Recursive FunctionsRecursive functions can be understood as their own functions calling themselves in the function body, using self-invocation.
def Calc (n): Print (n) if int (N/2) > 0: return calc (int (n/2)) print(" ---->", N) Calc () Results:10521----> 1
When using recursive functions, the following points need to be noted:
1. There must be a clear end condition
2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion
3. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow.
Python function description (i)