Why do I have a function?
Without differentiating the code of all functions together, the question is:
Poor code readability
Code redundancy
Code can be extended to poor
How to solve?
A function is a tool, the process of preparing a tool beforehand is to define a function, and to refer to it is a function call
Conclusion: The function must be used: defined first, then called
Two: Classification of functions
1. Built-in function: built-in
2. Custom functions:
def function name (parameter 1, parameter 2, ...):
"' Notes '
function body
Let's take a look at an example.
"' ************************************hello world************************************" def print_tag (Tag,count , Line_num): For I in range (line_num): print (Tag*count) def print_msg (msg): #msg = ' Hello World ' print (msg) Print_tag (' * ', 20,3) print_msg (' Hello World ') print_tag (' * ', 20, 3)
1. Definition phase
def foo (): print (' from foo ') bra () def bra (): print (' from bra ')
def func (): #语法没问题, logic is problematic, referencing a non-existent variable name ADFKGJKLGNLFIJG
2. Call Stage
Foo () #执行函数语句形式
================================
Res=foo () #执行函数表达式形式
Print (RES)
3. Definition phase: Only the syntax is detected and no code is executed
def func (): if 1>2 print (' Hahahaha ')
4, three forms of the definition function
No parameter function
def func ():
Pass
Parameter function
def func (x):
Print (x)
Null function
def func ()
Pass
Use of functions: first defined, then called
How to define three forms of function definition function
1. Define the parameterless function: the execution of a function is not dependent on the parameters passed in by the caller, and it needs to be defined as an parameterless function.
Def print_tag (): print (' ************************* ') Print_tag ()
2. Define parameters: The execution of a function needs to be defined as an argument function when it relies on the arguments passed in by the caller to execute
def func (X,y,*args): print (x, y) print (args) func (1,2,3,4,5,6)
def max2 (x, y): if x > y: return x else: return y res=10*max2 (20,2) #取大值乘10 Print (res)
def max2 (x, y): if x > y: #如果x值比y大 return x #返回x值给函数max2 else: return y #否则返回y值 #11,23,100res=m Ax2 (Max2 (11,23), max), value of #比较x, Y, large value then compare print (RES)
3 Define an empty function:
#3 defining an empty function: The function body is pass# def func (x, Y, z): # Pass
def auth (): ' user authentication ' ' passdef Get (): ' download file ' ' passdef put (): ' ' upload file ' Passdef Check_hash (): ' Check file hash value ' pass
Summarize
The use of the function must follow: the principle of first defining after use
The definition of a function is similar to the definition of a variable, and it is equivalent to referencing a non-existent variable name if it is not directly referenced without defining the function beforehand.
Add: no return in function returns the value of None by default
Contains multiple values, default recognition return value Narimoto group format
Return value: can return any type
Return effect: Only one value can be returned, terminating the execution of the function
Python (DAY6)-Functions