18. The function definitions and parameters

Function definition: function refers to a collection of statements by the name of a package together to perform this function, simply call the function name to 
DEF function (the X-, the y-):
"" "
Function Description
: param x: parameter x, int type
: param y: y int parameter type
: return: return value
"" "
RES = X * Y
Print (RES)
return RES # return a return value of the function, not the write default return is none, if multiple return values, in the form of tuples returned, return code is no longer performing the following
function (3,5)
function (45, 56) multiple calls #
x, y is in the form of parameters
3,5 is argument.
# Position parameter, passed 3 x, 5 pass y, according to the position value pass
default parameters #, when a function defined default parameters, country following function
def register (name, age, major , country = "china") : # default parameters must be placed behind the non-default parameter
info = "" "------ registration information -----
name:% S
Age:% S
Major:% S
Country:% S
----- end -----
"" "% (name, Age, Major, Country)
Print (info)
return info

Register (" caona "," 22 is "," Nurse "," JAP ") # pass in accordance with the position values correspond, no more It can not be much less error
register ( "caona", "22 ", major = "nurse") # keyword arguments must be placed behind the position parameter

# non-fixed parameters: the calling function, uncertain how many parameters to be passed, with args *, ** kwargs * argument, argument-Key-value
DEF Register (name, args *, ** kwargs):
Print (name, args, kwargs)
Register ( "caona", 22 is, "Doctor", "China ", Hometown =" shanghai ")
# print args is a tuple, kwargs printed dictionary is

# useful advantages
# reduce code duplication,
# Easy maintenance
# scalable
# function name in lowercase, not to noon and Pinyin

Guess you like

Origin www.cnblogs.com/xh716/p/11565911.html
Recommended