# Python 作用域

## Python 作用域

#!/usr/bin/python

def f(x,y):
print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y))
print('x * y = ' + str(x*y))
z = 4 # cannot reach z, so THIS WON'T WORK

z = 3
f(3,2)


#!/usr/bin/python

def f(x,y):
z = 3
print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y))
print('x * y = ' + str(x*y))
print(z) # can reach because variable z is defined in the function

f(3,2)


#!/usr/bin/python

def f(x,y,z):
return x+y+z # this will return the sum because all variables are passed as parameters

sum = f(3,2,1)
print(sum)


## 在函数中调用函数

#!/usr/bin/python

def highFive():
return 5

def f(x,y):
z = highFive() # we get the variable contents from highFive()
return x+y+z # returns x+y+z. z is reachable becaue it is defined above

result = f(3,2)
print(result)