What is a function?
The term function is derived from mathematics, but the concept of "function" in programming is very different from the function in mathematics, and the function in programming has many different names in English. 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.
Definition: A function is a collection of a set of statements by a name (function name) encapsulated, in order to execute this function, just call its function name.
II. Benefits of using functions:
1. Simplified code
2, improve the reusability of code
3, code can be extended
Third, the definition of functions in Python:
The definition function uses the DEF keyword, followed by the function name, and the function name cannot be duplicated
12 |
def Fun():#定义一个函数, followed by a function name Print("Hello World")#函数体 |
Of course, the above function does not have any eggs, just write a function definition example.
Iv. parameters of the function
When a function is called, it can pass in parameters, tangible parameters and arguments
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.
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. The parameter variable can no longer be used after the function call finishes returning the keynote function.
123 |
def calc (xy) : #定义一个函数 with x and y parameters , X and y are formal parameters print ( x*y) #输出x乘以y的值 calc (5,2) #调用上面定义的函数, 5 and 2 are arguments |
Simply put, the parameter is the argument that the function receives, and the argument is the one you actually passed in.
The four types of parameters for a function:
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:
The default parameter is when you define formal parameters, assign a value to the function by default, such as the port of the database, the default is to give it a value, so that even if you do not pass this parameter in the call, it is also a value
So, the default parameter is not required, and if you pass the value to the default parameter, it will use the value you passed in. If a default value parameter is used, it must be defined after the position parameter.
1234 |
def conn_mysql(user,passwd,port=3306):#定义一个连接mysql的方法, Although this method is not connected to MySQL, I just give an example of a default value parameter, port is a default value parameter Print(user,passwd,port) coon_mysql(' root ',' 123456 ')#没指定默认值 coon_mysql(' root ',' 123456 ',port=3307)#指定默认值参数的值 |
Non-fixed parameters:
The above two positional parameters and the default parameters are the number of parameters is fixed, if I am a function, the parameters are not fixed, I do not know the future of this function will expand into what kind of, may be more and more parameters, this time if the fixed parameters, then the program is not good expansion, then you can use non-fixed parameters, There are two types of non-fixed parameters, one is a variable parameter and one is a keyword parameter.
Variable parameters:
Variable parameters are received with *, the number of arguments to pass after the number of passes, if the positional parameters, default parameters, variable parameters used together, the variable parameter must be in the position parameter and the default value parameter. Variable parameters are also non-mandatory.
1234 |
def more_arg(name,age,sex=' nan ',*agrs): #位置参数, default parameter, variable parameter, variable parameter will put the parameters of the subsequent passes to the args this tuple #当然args名字是随便写的, but in general we all write args. Print(name,age,agrs) more_arg(' Marry ', ', 'NV ',' python ', ' China')#调用 |
Keyword parameters:
Keyword parameter use * * to receive, the following parameters are not fixed, want to write how many write how many, of course, can also be used with the above several, if you want to use the keyword parameter must be in the last side.
With keyword arguments, you must use the keyword argument when calling. Keyword parameters are also non-mandatory.
123 |
def kw_arg(name,**Kwargs):#位置参数, keyword parameter, when called will put the passed keyword parameter into the Kwargs dictionary Print(name,kwargs) kw_arg(' Sriba ',sex=' Male ',age=)#调用, sex and age are the keyword calls |
V. Return value of a 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.
12345 |
def Calc(x,y):#这个就是定义了一个有返回值的函数 c = x*y return C,x,y res = Calc(5,6)#把函数的返回结果赋值给res Print(res) |
Vi. 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.
123456789101112 |
name = ' Marry '#字符串全局变量 names = []#list全局变量 Print(name) Print(names) def Test(): Global name #修改name的值就需要用global关键字 name = ' Sriba ' names. Append(name)#修改全局变量names的值 return names Test() Call function Print(' modified ',name) Print(' modified names ',names) |
Python function operation