First, define the function
def function name (argument list): function body
def function (): Pass
Second, formal parameters/actual parameters
X, y is the parameter, the memory unit is allocated only when it is called, and the allocated memory unit is freed immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. The parameter variable can no longer be used after the function call finishes returning the keynote function.
It can be a constant, a variable, an expression, a function, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter. Therefore, the parameter should be determined by the method of assignment, input and so on beforehand.
def Add (x, y ): Print (x+y) Add (+)
Three, parameter type
#required parameters, for arguments, to pass in the function in the correct order. The number of calls must be the same as when declared. defPerson (name,age):Print('name:%s'%name)Print('age:%s'%Age ) Person (Jack,18)#keyword parameters, for arguments, function calls use the keyword argument to determine the value of the passed parameter. The order of arguments is inconsistent with declaration when a function call is alloweddefPerson (name,age):Print('name:%s'%name)Print('age:%s'%Age ) person ( age=18,name='Jack')#The default parameter, for the formal parameter, or the default if no parameters are passed. defPerson (name, age, sex='male'): Print('name:%s'%name)Print('age:%s'%Age )Print('sex:%s'%sex) person ('Jack', 18)
#non-fixed parameter, for formal parameter; If your function is not defined by how many parameters the user wants to pass in, you can use the non-fixed parameter#*args will change multiple incoming parameters into a tuple form.#**kwargs will turn multiple incoming parameters into a dict form.defPerson (*args,**Kwargs):Print(Args,kwargs) person ("Jack", 32,"CN","Python", sex="Male", province="Shandong")
Functions of the "Python Foundation"