In Python programming, for some programs that require repeated calls, you can use a function to define the basic form:
def function name (parameter 1, Parameter 2, ..., parameter n):
The EXECUTE statement function name is the name of the invocation, and the parameter is the passed-in parameter, which can be defined more or not.
# example 1: simple function using # coding=gb2312# definition function def hello (): print ' Hello python! ' # Call Function Hello () >>> Hello python!
Functions can have parameters and return values, parameters will be left-to-right matching, parameters can be set default values, when the function is not given the corresponding parameters, will be assigned by default values.
# example 2: Cumulative calculated value # coding=gb2312# definition function def myadd (a=1,b=100): result = 0 i = a while i <= B: # default value is 1+2+3+......+100< C8/>result + = i i + = 1 return result# print 1+2+......+10 print myadd (1,10) print myadd () # Use default parameters 1,100print Myadd # A assignment 50,b use default >>> 55>>> 5050>>> 3825
When the parameters of a Python function are passed, it is worth noting that when a parameter is passed in, the variable is treated as a temporary assignment to the parameter variable, and if the object is referenced.
# example 3:# coding=gb2312def Testpara (P1,P2): p1 = ten p2.append (' hello ') L = [] # defines an array pair like a = $ # assigns a value to variable a testpar A (a,l) # variable A with Object array L passed in as parameter to print a # After printing a run parameter for the value of V in L: # Print the members of an array object the A variable is not re-evaluated after calling the function print v >>> >>> Hello # While object L array adds member Hello