The function is defined by the def keyword. The def keyword is followed by the identifier name of a function, followed by a pair of parentheses. Variable names can be included in parentheses, which end with a colon. Next is a statement, which is the function body. The following example shows that this is actually very simple:
No parameter:
DefSayhello ():Print'Hello world! '# Block belonging toFunctionSayhello () # CallFunction
With parameters:
DefSayhello (s ):PrintS # block belonging toFunctionSayhello (Raw_input('Type some word') # CallFunction
Remember to useDefTo define the module.:
Global Statement
If you want to assign a value to a variable defined outside the function, you have to tell Python that the variable name is not local, but global. We use the global statement to complete this function. Without a global statement, it is impossible to assign values to variables defined outside the function. You can use the value defined outside the function (assuming there is no variable with the same name in the function ). However, I do not encourage you
In this way, and you should try to avoid doing so, because this makesProgramReaders will not know where the variable is defined.The global statement clearly indicates that the variable is defined in the block outside.
#! /Usr/bin/Python # filename: func_global.pyDefFunc (): Global xPrint'X is ', xx = 2Print'Changed local X to', xx = 50 func ()Print'Value of X is ', X
The global statement is used to declare that X is global. Therefore, when we assign the value to X in the function, this change is also reflected when we use the value of X in the main block. You can use the same global statement to specify multiple global variables. For example, global x, y, and z.
Default parameter value
For some functions, you may want some of its parameters to be optional. If you do not want to provide values for these parameters, these parameters use the default values. This function is completed by default parameter values. You can add the value assignment operator (=) and default value after the parameter name defined by the function to specify the default parameter value for the parameter. Note that the default parameter value is a parameter. More accurately, the default parameter value should be immutable. Remember this from now on.
Use default parameter values
Example 7.5 use the default parameter value
#! /Usr/bin/Python # filename: func_default.pyDefSay (message, Times = 1 ):PrintMessage * Times // here * Times indicates that times are output continuously. Say ('hello') Say ('World', 5)
Key parameters
If a function has many parameters and you only want to specify a part of them, you can useAssign values to these parameters.-- This is called a key parameter -- we use the name (keyword) instead of the position (the method we used earlier) to specify the real parameter for the function.
This has two advantages: 1. Since we do not have to worry about the order of parameters, it is easier to use functions. 2. If other parameters have default values, we can only assign values to those parameters we want.
Use Key Parameters
Example 7.6 use key parameters
#! /Usr/bin/Python
# Filename: func_key.py
Def func (a, B = 5, c = 10 ):
Print 'a is ', A,' and B is ', B,' and C is ', c
Func (3, 7)
Func (25, c = 24)
Func (C = 50, A = 100)
Output
$ Python func_key.py
A is 3 and B is 7 and C is 10 // C is the default value
A is 25 and B is 5 and C is 24 // B is the default value
A is 100 and B is 5 and C is 50 // similar to C //
Return Statement: return. If no return value is returned, the Return Statement is equivalent to return none. None is a special type in Python that indicates nothing. For example, if the value of a variable is none, it indicates that it has no value. Unless you provide your own return statement, each function contains a return none statement at the end. By running print somefunction (), you can understand this. The somefunction does not use the return statement, as shown below:
Def somefunction ():
Pass // After definition, write the code first. Use this
The pass statement indicates an empty statement block in Python.
Docstrings
Python has a wonderful feature called a document string, which is usually referred to as docstrings. Docstrings is an important tool, because it helps your program documentation to be easier to understand, you should try to use it. You can even restore the document string from the function when the program is running!
Use docstrings
#! /Usr/bin/Python # filename: func_doc.pyDefPrintmax (x, y): ''' prints the maximum of two numbers.
The two values must be integers. ''' x = int (x) # convert to integers,IfPossibley = int (y)IfX> Y:PrintX, 'is maximum'Else:PrintY, 'is maximum' printmax (3, 5)PrintPrintmax. _ Doc __
Output
$ Python func_doc.py
5 is maximum
Prints the maximum of two numbers.
The two values must be integers.
the string in the first logical line of the function is the document string of the function. Note that docstrings also applies to modules and classes. A document string is a multi-line string whose first line starts with an uppercase letter and ends with a full stop. The second line is empty, and the detailed description starts from the third line. We strongly recommend that you follow this Convention when using document strings in your functions.
You can Use _ Doc _ (pay attention to double underscores) the document string attribute that calls the printmax function (belongs to the function name ). remember that python regards everything as an object, including this function. All it does is capture the _ Doc _ attribute of the function and display it to you neatly. You can try the above function-just include help (printmax) in your program ). remember to press Q to exit help. Automated tools can also extract documents from your programs in the same way. Therefore, I strongly recommend that you write document strings for any formal functions you write. Use the pydoc command that comes with your python release, similar to help () to use docstrings.