1) Definition: function refers to the collection of a set of statements by a name (function name) encapsulated, in order to execute this function, just call its function name.
2) The benefits of using the function: 1, simplify the code, 2, improve the reusability of the Code; 3. Extensible Code
3) Give an example:
Def sayhello (): #使用关键字def定义函数, followed by a function name
Print (' Hello World ') #函数体
SayHello () #调用函数
4) Parameter passing
Def calc (b): #a, B is formal parameter, formal parameters
Res=a*b
Print ('%s *%s=%s '% (a,b,res))
Calc (7,8) #实参, actual parameters
Note: The parametric only allocates memory units when called, releasing the allocated memory units immediately at the end of the call. Therefore, the formal parameter is only valid inside the function.
5) Four types of formal parameters
1. Positional parameters: The literal meaning is to follow the parameters of the location of the parameter, such as the Calc function above, A and B is positional parameters, positional parameters are required to pass, there are several positional parameters in the call to pass a few, otherwise it will error.
2. Default parameter, the default parameter is to assign a value to the function by default when defining formal parameters. For example:
def op_file (file_name,content=none): #content为默认值参数, not required
F = open (file_name, ' A + ', encoding= ' utf-8 ')
F.seek (0)
if content: #不为空代表写
F.write (CO ntent)
F.flush ()
else: Br> All_user= (F.read ()) #all_user local variables and can only be used within functions
return all_user #调用完函数之后, return result
F.close ()
3. Variable parameters, variable parameters with * to receive, non-mandatory parameters, do not limit the number of parameters, the parameters passed over to the meta-zanzuri, each corresponding.
def syz (*args): #可变参数, which is the parameter group
Print (args)
# Username=args[0] #多个参数时, follow the subscript to fetch the data
Syz ()
Syz (' Zhangsan ', ' 35435345 ')
4. Keyword parameters, with * * to receive, non-mandatory parameters, unlimited number, pass over the parameters, put in the dictionary
def syz2 (**kwargs): #关键字参数
Print (Kwargs)
SYZ2 ()
SYZ2 (name= ' Zhangsan ', age=38)
SYZ2 (name= ' Zhangsan ', age=38,addr= ' Huilongguan ', home= ' Sichuan ')
6) return value of function
Each function has a return value, if it is not specified in the function of the return value, after the function is executed in Python, the default will return a none, the function can have more than one return value, if there are multiple return values, the return value will be placed in a tuple, the return is a tuple.
Why should there be a return value, because after this function is finished, its result will need to be used in the later program.
The return value in the function uses return, and the function ends immediately when it encounters a return.
Def calc (A, B):
C=a*b
return c,a,b #函数返回值
Res=calc (5,6) #把函数的返回值赋给res
Print (RES)
7) Local variables and global variables
Local variable meaning is in the local effect, out of the scope of the variable, this variable is invalid, such as the above C is a local variable, after this function, there is no C this value
Global variables mean that the whole program is in effect, in the first definition of the program is the global variables, global variables if you want to modify in the function, you need to add the Global keyword declaration, if it is a list, dictionary and collection, you do not need to add the global keyword, directly can be modified.
name = ' Zhangsan ' #全局变量
Def sayname ():
Global name #如果要修改全局变量, you need to declare that you modify the value to be a global variable
Name= ' Lisi '
Print (name)
Novice Learning Python (v) function