table of Contents
Like usually play with blocks, a function that is a building block to block, you can be combined in any shape you want.
Python function
Simple function call
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def printHello():
print("hello world!")
print("张盛东你来了")
printHello()
printHello()
I can see the above-mentioned two calls to printHello()
the function, save as fun01.py
, implementation of the results is as follows:
G:\Py>python fun01.py
hello world!
张盛东你来了
hello world!
张盛东你来了
Function parameter passing
Here printHello(num)
the contents in the square brackets, used to pass parameters (argument)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def printHello(num):
print("hello world!")
print("张盛东你来了")
print("带了",num,"个苹果")
printHello(3)
A plurality of parameters can be passed, for example, three transmission parameters. as follows:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def printHello(num,name):
print("hello world!")
print("张盛东你来了")
print("带了",num,"个苹果")
print("叫了",name,"这位好朋友一起来吃")
printHello(3,'胡轲')
return value
Definition of a function, there is generally input process, an output. Think of it as a black box, what could not see. Then enter several values, returns a result.
Calculation circular area
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math
def area_circle(r):
print("你传入的半径是",r)
S = math.pi*r**2
return S
r1 = 5
S1 = area_circle(r1)
print("半径为",r1,"的圆面积是",S1)
The results obtained were as follows:
G:\Py>python fun04.py
你传入的半径是 5
半径为 5 的圆面积是 78.53981633974483
Variable Scope
Local variables
What is a local variable, the variable is only used within a function, if you call out, then directly be wrong. Take area_circle()
function sub Examples. as follows:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math
def area_circle(r):
print("你传入的半径是",r)
S = math.pi*r**2
return S
r1 = 5
S1 = area_circle(r1)
print("半径为",r1,"的圆面积是",S1)
####调用局部的变量r
print(r)
Save As fun05.py
, perform the following effects:
G:\Py>python fun05.py
你传入的半径是 5
半径为 5 的圆面积是 78.53981633974483
Traceback (most recent call last):
File "fun05.py", line 13, in <module>
print(r)
NameError: name 'r' is not defined ----->r是局部变量,不能在全局使用。
Global Variables
It may be used in full. Continue to modify area_circle()
functions.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math
###### 这里r1是全局变量。
def area_circle(r):
print(r1,"函数内的全局变量")
print("你传入的半径是",r)
S = math.pi*r**2
return S
r1 = 5
S1 = area_circle(r1)
print(r1,"函数外的全局变量")
print("半径为",r1,"的圆面积是",S1)
Very magical time, within the functions can also be read r1
variables. Results are as follows:
G:\Py>python fun06.py
5 函数内的全局变量
你传入的半径是 5
5 函数外的全局变量
半径为 5 的圆面积是 78.53981633974483
Now let's change this, what effect the global variable functions will appear.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math
###### 这里r1是全局变量。
def area_circle(r):
r1 =10
print("函数内的全局变量", r1)
print("你传入的半径是",r)
S = math.pi*r**2
return S
r1 = 5
S1 = area_circle(r1)
print(r1,"函数外的全局变量")
print("半径为",r1,"的圆面积是",S1)
Save As fun07.py
, to see results
G:\Py>python fun07.py
函数内的全局变量 10 ---->函数内,修改的不能带出
你传入的半径是 5
5 函数外的全局变量 ---->函数外,r1还是5.
半径为 5 的圆面积是 78.53981633974483