function is what function refers to the collection of a set of statements through a name (function name) to encapsulate, in order to execute this function, just call its function name. function is a kind of programming method of logical structure and process. Features of the function:
- Improve Code reusability
- Increase the extensibility of the program
- Is that the program is easier to maintain
Syntax format
1 def hello (): # function name 2 print ("Hello, I ' m baylor! " )345 Hello () # Call function
Formal parameters
- Parametric the memory unit is allocated only when called, releasing the allocated memory unit immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. Function call ends when you return to the keynote function, you can no longer use the shape parametric
Actual parameters
- Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter.
1 def func (name): #形参 2 Print (name)3 func ('Baylor') #实参
The four types of parameters for a function:
1 def calc (x, y): 2 res = x * * y 3return res # return function execution Result- function return value
4 5 6 c = Calc (5,6 7 8 9 d = Calc (y=6, x= 5 Ten print (d)
To get the result of the function execution, you can return the result to the note using the return statement:
- The function stops executing and returns the result as soon as it encounters a return statement, so can also be understood as a return statement that represents the end of the function
- If return is not specified in the function, the return value of this function is None
Position parameters:
- Positional parameters, which are literally meant to be passed as parameters, such as the Calc function above, where x and y are positional parameters, the positional parameters must be passed,
- There are several positional parameters at the time of the call to pass a few, otherwise it will be error, if there are multiple positional parameters, you can not remember which location to pass what to do, you may use the name of the positional parameter to specify the call
- For example, the Calc function above can also be called by using Calc (y=1,x=2), which is called the keyword argument.
Default parameters:
- When you do not have to pass parameters, you can set the default parameters when the function is defined, and the call needs to be changed when you re-specify key parameter overrides
- Formal parameters in the previous default parameters after
keyword parameters:
- Key parameter cannot be written in position parameter front & position not fixed
- The keyword parameter can be passed in the form parameter order when the parameter is passed, and the argument is specified by the keyword parameter.
1 A ,): #默认参数age2 print (name,sex,age)3 func (sex=' male ' , name='Baylor') #关键字参数
Non-fixed parameter-parameter group
- *args will change multiple incoming parameters into a tuple form.
- **kwargs will turn multiple incoming parameters into a dictionary form.
1def func (*args,name,**kwargs): Receive parameters in #*args tuple mode, * *Kwargs k,v mode receive parameter2 print (Args,name,kwargs)3 4Func1,2,3, name='Baylor', corporation='MLS', age=' -')5#输出结果 (1,2,3) Baylor {'Corporation':'MLS',' Age':' -'}
Global variables
- Written at the beginning of the program, the global variable scope is the entire program
- A variable defined in a subroutine is called a local variable, and a variable defined at the beginning of the program is called a global variable.
- 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 in sub-programs that define local variables; global variables work elsewhere
- Modifying global variables through global modification, it is not recommended to modify global variables in subroutines when the code size is large, it is inconvenient to maintain global variables
Local variables
- A local variable scope is a subroutine that defines the variable
- strings and individual integers cannot be changed locally.
- Dictionaries, lists, collections, can be changed locally directly global
1Test ='besttest'# Global Variables2temp={1:1}3tag=['Lee Two dog','Zhang San']4 def func (name):5 print (name)6#GlobalTest #GlobalTest #修改全局变量7test='Baylor'# Local Variables-only takes effect in the function.8 print (test)9#temp ={2:2}Ten Print (temp) Onetag[0]='Dog Eggs' A print (TAG) - - theName ='Niuhanyang' - func (name) -Print (test)
Recursive functions can call other functions inside a function. If a function calls itself internally, this function is the recursive function of recursive functions:
- Must have a definite end condition
- Each time you enter a deeper level of recursion, the problem size should be reduced compared to the last recursion
- 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, whenever entering a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames. Because the stack size is not infinite, there are too many recursive calls, which can cause the stack to overflow
Python function Day04