1: Define function
1. Concept of function
A function is the encapsulation of code that implements a specific function - different functions have different functions
2. Classification of functions
Functions are divided into system functions and user-defined functions according to who defines (creates) them
1) System functions - functions built by the system (functions created by the person who created the Python language) (machines built by others), for example: max,min,sum,print,type
2) Custom functions - functions created by programmers themselves (self built machines)
3. Define function (create function) - build machine
def Function name(parameter list ): Function description document Function body explain: def - keyword:Fixed writing Function name - Named by the programmer himself; requirement:Identifier, not keyword standard:See the name and know the meaning(See the function name and know the function of the function)All letters are lowercase,Multiple words are separated by underscores The function name, class name and template name of the system are not used () - Fixed writing parameter list - with'Variable name 1,Variable 2,Variable 3,...'The form of existence,A variable here represents a formal parameter(You can also have no formal parameters) Formal parameters can transfer data outside the function to the function. Whether formal parameters are needed when defining a function depends on whether additional data is needed to implement the function. Function description document - The essence is multiline annotation(Equivalent to the instructions of the machine) Function body - and def One or more statements that maintain an indentation The function body is the code that implements the function of the function
4. Basic steps for beginners to define functions
Step 1: determine function
Step 2: determine the function name according to the function
Step 3: determine the parameters (see whether additional data is required to realize the function function, how many are required?)
Step 4: implement function function (use formal parameters as data)
Step 5: write function description document
#Exercise 1: define a function to find the factorial of 10 (no formal parameters required) #10!=1*2*3*...*9*10 def factorial_10(): ''' (Function description)Ask 10! :return: (Return to the description area)None ''' sum1=1 for x in range(1,11): sum1*=x print(sum1) #Exercise 2: define a function to find the factorial of any number (formal parameters required) def factorial(num): ''' Find the factorial of a number :param num: (Parameter description)Number of factorials required :return: None ''' sum1=1 for x in range(1,num+1): sum1*=x print(sum1) #Define a function to sum any two numbers def sum_yt(num1,num2): '''Find the sum of any two numbers''' print(num1+num2)
2: Function call
1. Call function - use machine
Syntax:
Function name (argument list)
explain:
Function name - the function name of the function to be called (must be the function name of the function already defined)
() - Fixed writing
Argument list - exists in the form of 'data 1, data 2, data 3,...'
Arguments are used to assign values to formal parameters. Arguments are data that is really passed from outside the function to inside the function
Note: the same function can be called repeatedly when its function is needed
2. Function calling process
1) The defined function will not execute the function body, and the function body will be executed only when the function is called
2) Execution process
Step 1: return to the position defined by the function
Step 2: pass parameters (assign values to formal parameters with actual parameters)
Step 3: execute the function body
Step 4: determine the return value of the function
Step 5: return to the location of the function call, and then execute it later
3. Parameters of function
1. Location parameter and keyword parameter - actual parameter
1) Position parameters
It exists in the form of 'data 1, data 2, data 3,...' Make the arguments and formal parameters correspond to each other in position
(the first argument assigns a value to the first formal parameter, and the second argument assigns a value to the second formal parameter,...)
2) Keyword parameters
Exists in the form of 'formal parameter 1 = argument 1, formal parameter 2 = argument 2,...'
3) Mixed use
If a location parameter is used with a keyword parameter, the location parameter must precede the keyword parameter
def func1(x,y,z): print(f'x:{x},y:{y},z:{z}') #Position parameters func1(10,20,30) #Keyword parameters func1(x=100,y=200,z=300) #Positional parameters are used with keyword parameters func1(10,y=20,z=30) func1(10,20,z=30) #func1(x=10,20,z=30) #SyntaxError: positional argument follows keyword argument
2. Parameter default value - formal parameter
When defining a function, you can assign default values to parameters in the way of 'formal parameter = value'
When calling a function, parameters with default values can be passed without parameters
When defining a function, if some parameters have default values and some parameters have no default values, the parameters without default values must be in front of the default value parameters
3. Parameter type description - formal parameter
Case 1: for parameters without default value, add ': type name' after the parameter
Case 2: is there a parameter with default value? The type of value is the description type of the parameter
def fun5(str1:str,list=[]): pass fun5('1',[1,2,3])
4. Indefinite length parameter - formal parameter
1) Variable length parameter with
When defining a function, you can add a parameter before it. Then the parameter with this band is an indefinite length parameter and can accept multiple arguments at the same time
The parameter with will automatically become an ancestor, and the received argument is an element in the ancestor
When calling, the variable length parameter with * must be passed as a position parameter
2) Variable length parameter with * * (understand)
def func6(*x): print(x) func6(1,2,3,4) #The function of * here is to force b and c to use keyword parameters when calling func8 def fuc8(a,*,b,c): pass
Function return value:
1. Return value
1) Meaning of return value:
The return value is the data passed from inside the function to outside the function
2) How to determine the return value of a function:
The return value is the value of the expression after the return keyword (when you encounter a return when executing the function body, what is behind the return, and what is the return value of the function)
If return is not encountered, the return value is None
3) How to use a function return value outside a function
The value of the function call expression is the return value of the function, so the function call expression can do whatever the return value can do
Function call expression - the statement that calls the function
def func1(): print('abc') if 100>20: return 100 result=func1() print(f'result:{result}') print(100,func1()) func1()
2. Function of return
Function 1: determine the return value of the function
Function 2: end the function in advance - if you encounter a return when executing the function body, the function ends directly
def func4(num): print('++++') if num==0: return 0 print('----') print('====') func4(0) result=func4(0) print(result)