Function
First, Introduction
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.
Characteristics:
- Reduce duplicate code
- To make the program extensible
- Make programs easier to maintain
Syntax definitions
def func (): #函数名 print ("Hello,world") func () #调用函数
With parameters
def add (A, b): c = A+breturn c #返回函数执行结果f = add
Second, function parameters
2.1 Formal parameters and arguments
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
def add (b): # A, parameter
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. It is therefore necessary to use the assignment, input and other methods to get the parameters to determine the value
f = Add (1, 2 arguments)
2.2 Default Parameters
python--function