Functions can be used to define reusable code, organize and simplify Code.
A function is a set of statements that are assembled together to implement an Operation.
function definitions include function names, formal parameters, and function bodies.
def functionname (list of Parameters)
#Function body
For example, a function to find out which of the two numbers is larger?
def Max (num1,num2) if num1>num2:result=num1 else:result=num2 return result
Functions include function heads and function bodies. The function header begins with a def keyword followed by the function name and the formal parameter and ends with a colon.
The parameters in the function header are referred to as formal parameters or simply as formal arguments. A parameter is like a placeholder, and when the function is called, a value is passed to the Parameter. This value is called an actual parameter and an Argument. Parameters are optional, that is, the function can contain no arguments. Some functions have return values, and some other functions complete the required operation without returning a Value. If the function is returned by a value, it is called a parameter with a return Value. The body of a function contains a set of statements that define what the function Does.
In the definition of a function, define what the function will do. In order to use a function, it must be called. The program that invokes the function is called the Caller. There are two ways to call a function, depending on whether the function is returned by Value.
If a function has a return value, a call to such a function is usually treated as a value, for example, Large=max (3,4)
Call Max (3,4) and assign the return value to Large.
Exception A call example that treats it as a value is the return value of the print (3,4) statement after the output of the call function Max (3,4).
If the function does not return a value, the calling function must be a statement, for example, the function print has no return Value. When a function is called, program control is transferred to the function being Called. When the Function's return statement is executed or the function ends, the function will give the program control to the Caller.
Note: The function does not necessarily have a return value
A function with no return value
def printgrade (score) if Score>=90.0:print (' A ') elif score>=80.0:print (' B ') elif score>= 70.0:print (' C ') elif score>=60.0:print (' D ') else:print (' F ')
The return value is not required, and if there is no return statement, Python returns the value none by Default.
Naming rules for function names:
The function name must begin with an underscore or letter and can contain any combination of letters, numbers, or Underscores. Cannot use any punctuation marks;
Function names are Case-sensitive.
The function name cannot be a reserved word.
Python uses the concept of namespaces to store objects, which are the areas where objects are scoped and different objects exist in different scopes. The following are the scope rules for different objects:
Each module has its own global scope.
The object defined by the function is a local scope, valid only within the function, and does not affect objects in the global Scope.
An Assignment object is a local scope unless declared using the global Keyword.
The LGB rule is Python's rule for finding a name, and here's the LGB rule:
1. Most names are referred to in three scopes: first local, second global (global), and again built-in (build-in).
>>> a=2
>>> b=2
>>> def Test (b):
... test=a*b
... return test
>>>print Test (10)
20
B is found in the local scope, and a is found in the global Scope.
2. If you want to change the global scope object in a local scope, you must use the global Keyword.
The code is as Follows:
#没用global时的情况
>>> name= "jims"
>>> def set ():
... name= "ringkee"
...
>>> Set ()
>>> Print Name
Jims
#使用global后的情况
>>> name= "jims"
>>> def Set1 ():
... global name
... name= "ringkee"
...
>>> Set1 ()
>>> Print Name
Ringkee
3. The ' Global ' declaration maps the name of the assignment to the scope of a module containing it.
The parameter of a function is the bridge between the function and the external communication, which can receive the value passed by the Outside.
4. Assigning a value to a parameter name in a function does not affect the Caller.
>>> a=1
>>> def Test (a):
... a=a+1
.. print a
...
>>> Test (a)
2
>>> A
1 # The value of a does not change
5. Changing a variable object parameter in a function affects the Caller.
The code is as Follows:
>>> a=1
>>> b=[1,2]
>>> def Test (a, b):
... a=5
... b[0]=4
.. print a, b
...
>>> Test (a, B)
5 [4, 2]
>>> A
1
>>> b
[4, 2] # b value has been changed
Parameters are object pointers and do not need to define the type of object being Passed. Such as:
The code is as Follows:
>>> def Test (a, b):
... return a+b
...
>>> Test #数值型
3
>>> Test ("a", "b") #字符型
' AB '
>>> Test ([12],[11]) #列表
[12, 11]
This article is from the "small stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1864047
The function of Python learning