Declaring a function
Def checkname (): a = if a%2 ==0: print '%s is even '% (a) return a else: print '%s is odd '% (a) return Aprint CheckName ()
DECLARE keyword def
Then a space.
And then the function name CheckName.
Then the parameter () #参数可以为空, that is, no parameter
return value #见下面介绍
-----------------------Split Line
Just a function, two places.
1. Parameters
Optional parameters: No parameter, there are 1 fixed parameters, there are many fixed parameters, fixed parameters have default values, not fixed parameters
def checkname () #无参
def checkname (b) #固定1个参数
def checkname (b = ' test ') #固定1个参数, there is a default value, that is, you can provide parameters when using, or do not provide, when not provided with the default value
def checkname (b,c) #固定2个参数
def checkname (*b) #不定长参数, which is the incoming tuple
def checkname (**b) #不定长参数, which is the incoming dictionary
2. Return value
Returns none by default when a return statement is not used
Return a returns a single member
Return (A,B.C) returns multiple members
Variables declared within a function are local variables by default and do not affect variables outside the function
If you want to influence it, you need to declare it as a global variable.
b = Onedef values (): global b # declared as a globals variable b =values () print b# printed here b=15= onedef values (): =values () print b# b=11 printed here
In general, it is not recommended to use the global variable
Add function description
def values (): ' valuetest ' # function Description b =print values. __doc__ # View Description
Functions of Python