Why use a function:
1, solve the problem of code reuse
2, improve the maintainability of the code, unified maintenance
3, the organization structure of the program is clear, the readability is strong
Defining functions
First define after use!!!
def funcname (Arg1,arg2,.....) #funcname = function Body
"Description Information"
function body
return value
Define no parameter function no return value
def foo ()
Print ("foo")
Defines an argument function that accepts an externally passed-in value, and returns a value with return
def my_sum (x, y)
Return X+y
Defining an empty function pass
Calling functions
Statement form: foo ()
Expression form: res=10*my_sum
Function call as a parameter of another function: print (my_sum)
function return value
Do not write return----->none
Return a value----> this value
Return multiple values-----> tuples consisting of these multiple values
Parameters of the function
Parameter: parameters to define the function (name)
Arguments: parameters (real numbers) used when calling a function can also define variable values and call functions
My_sum (ON)
a=1,b=2
My_sum (A, B)
The argument is valid in the process of a function call, and it is not valid after it!
Angle of argument:
Value My_sum by location
Value by keyword my_sum (y=1,x=2)
Mixed transfer value my_sum (1,y=2)
From the formal parameter angle:
Positional parameters: def foo (x, Y, z) (more function changes)
Default parameters: Def foo (x,y=1) (the function is not changed much, the number of calls is not many)
When do you use it?
Where are they arranged?
The characteristics of the two?
*args def foo (X,*args)
Value by position, extra value is received by *, form a tuple
**kwargs def foo (X,**kwargs)
By the keyword, the extra value is received by the * *, form the dictionary
def foo (X,*args,**kwargs)
Foo (1,2,3,4,5,6,a=1,b=2,c=3)
X=1
Args= (2,3,4,5,6)
Kwargs={"A": 1, "B": 2, "C": 3}
Python full stack python basic (eight) function (change at any time)