Function
A function is a function-specific, encapsulated, reusable program snippet. Give the program a name, and you can run the block arbitrarily from anywhere else in the program by this name.
-
definition and invocation of functions
For the definition of Python functions, different from the well-known C,java and other programming language definitions, in Java, for example, when defining a function, to indicate the function of the return value type, either void, or for his data type, while defining the function can also indicate that the function is private, common, static, etc. The definition of the Python function is very simple, as follows:
Format: def function_name ([parameter]):
For a function call directly in the main function weight using the method name + parameter on the line
-
Local variables: variables that work in the body of the function, while other function bodies cannot be used.
global variable: A variable within a function that defines a value that can be assigned outside the function.
Definition of global variable:
Format: global variable 1, variable 2 ....
(1). The mode one by one variables are defined outside the function and are defined using the Global keyword inside the function to replicate inside the function.!/usr/pythondef sum (): global x print xx=4sum () (2). Mode two defines a global variable directly inside the function, There is no declaration outside the function.!/usr/pythondef func (): global x x=4 print xfunc () print ' x= ', x
-
(1). The default parameter is
Default parameter is when you define a function parameter, you set a default value for the parameter, you can pass a value to the parameter when you call the function, or you can pass it.
Format: def function (parameter 1, parameter 2=value ...):
!/bin/pythondef function (i,c=2): print ' i=?c=? ' i,cfunction (1) function (3,5)
Note: For the definition of the default parameter, place it after the parameter.
(2). Key parameters
A key parameter is when you have multiple arguments in a function, and you can assign values by naming them when the function is called in the order in which you do not want the parameters to be installed.
!/bin/pythondef func (i,a=2,c=4): print ' 1=?,a=?,c=? ' The value of func (1,c=6) //i is 1,a=2,c=6func (3,5) //i =3,b=5func (c=8,a=4) //c=8,a=4
(3). Variable length parameters
You may need a function that can handle more arguments than was originally declared. These parameters are called indeterminate length parameters, and are not named when declared, unlike the above 2 parameters. The basic syntax is as follows:
def func (*PA):
!/bin/pythondef func (*PA): Print Pafunc (1,2,3,4,5)
Using the identifier ' * ' indicates that the length of the parameter can vary, when the parameter appears as a tuple.
So what if we want to pass a dict to the function when we pass the parameters?
Use the ' * * ' identifier at this time to implement passing a dictionary
!/bin/pythondef func (**PA): Print Pafunc (x=1,y=3,z=5) Run result: {' x ': 1, ' Y ': 3, ' Z ': 5}
(4). Sequence parameters
In Python, the default use of the sequence as a parameter, can only achieve the transfer of a single variable, but for the parameter number does not match the problem, before the argument can be implemented in the actual parameter decomposition to the various parameters, to achieve the pass list.
!/bin/pythondef func (a,b,c,d): Print A+b+c+dli=[1,2,3]func (1,*li)
!/bin/python def run (x): if (x%4==0 and x%100!=0) or x%400==0: print ' y ' else: print ' N ' a=input (' Please enter a year: ') run (a)
(2). Write a function to determine whether a number is a narcissus number.
!/bin/pythondef flower (x): a=int (x/100) //returns the integer part b=int ((x-a*100)/10) c=x-a*100-b*10 print a print b Print c if pow (a,3) *pow (b,3) *pow (c,3) ==x: print x, ' The number of daffodils ' else: print x, ' not Narcissus number ' a=int (input (' Please enter a number: ')) flower (a)
But when we use ABS () directly in the program, the call to the function >>>abs (-10) 10 Then we assign the function itself to the variable >>>s=abs () >>>s<built-in function abs> at this point, the variable points to the function, and we call this variable >>>s ( -10)
For example:
!/bin/pythondef func (a,c,f): print f (a) print f (c) f=abs () func ( -39,-2,f)
-
returns either a value or no return for a general function. Since you can pass in a function when you pass in a parameter, you can return the function as well.
For example: We call a function in front of the code, but we do not need the result of the function at this time, but in the later code, as needed to calculate what to do? Instead of returning a result, you can return a function that sums it.
!/bin/pythondef func (): def che (x, y): return x*y return chef=func () f ()
Note: Do not use circular statements when defining a function inside a function.
[Python] Learning Basics: Functions