这一章,学习函数
定义函数关键字 def,代码
>>> def printMyAddress(): print "Warren Sande" print "123 Main Street" print "Ottawa, Ontario, Canada" print "K2M 2E9" print
调用函数
>>> printMyAddress()Warren Sande123 Main StreetOttawa, Ontario, CanadaK2M 2E9>>>
嗯,就是这样,很简单。使用函数的主要原因,就是可以通过调用反复的使用,比如下面要求把上面的地址打印5次,如果没有函数就会很麻烦。
如果用循环呢,是不是也可以完成上面的任务,是的,可以。不过如果这儿调用一次,可以用循环,如果在不同的代码片段,反复调用呢,循环就做不到这么方便了。
调用函数后面的括号,可以用来传递参数(argument)。
def printMyAddress(myName): print myName print "123 Main Street" print "Ottawa, Ontario, Canada" print "K2M 2E9" printprintMyAddress("Carter Sande")
运行结果
>>> ================================ RESTART ================================>>> Carter Sande123 Main StreetOttawa, Ontario, CanadaK2M 2E9>>>
这看上去和第一个没多大区别,不过如我们这样呢
def printMyAddress(myName): print myName print "123 Main Street" print "Ottawa, Ontario, Canada" print "K2M 2E9" printprintMyAddress("Carter Sande")printMyAddress("Warren Sande")printMyAddress("Kyra Sande")printMyAddress("Patricia Sande")
运行试试看
>>> ================================ RESTART ================================>>> Carter Sande123 Main StreetOttawa, Ontario, CanadaK2M 2E9Warren Sande123 Main StreetOttawa, Ontario, CanadaK2M 2E9Kyra Sande123 Main StreetOttawa, Ontario, CanadaK2M 2E9Patricia Sande123 Main StreetOttawa, Ontario, CanadaK2M 2E9>>>
有多个参数的函数
def printMyAddress(someName, houseNum): print someName print houseNum, print "Main Street" print "Ottawa, Ontario, Canada" print "K2M 2E9" printprintMyAddress("Carter Sande", "45")printMyAddress("Warren Sande", "64")printMyAddress("Kyra Sande", "22")printMyAddress("Patricia Sande", "36")
运行结果
>>> ================================ RESTART ================================>>> Carter Sande45 Main StreetOttawa, Ontario, CanadaK2M 2E9Warren Sande64 Main StreetOttawa, Ontario, CanadaK2M 2E9Kyra Sande22 Main StreetOttawa, Ontario, CanadaK2M 2E9Patricia Sande36 Main StreetOttawa, Ontario, CanadaK2M 2E9>>>
除了可以向函数传递参数,函数还可以有返回值,在函数用关键字return
>>> def calculateTax(price, tax_rate): taxTotal = price + (price * tax_rate) return taxTotal
这样,以后调用
>>> totalPrice = calculateTax(7.99, 0.06)
函数把返回值赋值给totalPrice,以后可以打印或调用
>>> print totalPrice8.4694>>>
也可以这样
>>> print calculateTax(7.99, 0.06)8.4694
下面用一个有返回值的函数建立程序
def calculateTax(price, tax_rate): total = price + (price * tax_rate) return totalmy_price = float(raw_input ("Enter a price: "))totalPrice = calculateTax(my_price, 0.06)print "price = ", my_price, " Total price = ", totalPrice
运行结果,先询问输入price
>>> Enter a price:
给出一个数字后,就是
>>> Enter a price: 13.56price = 13.56 Total price = 14.3736>>>
注意:程序中使用(或可以使用)变量的部分称为这个变量的作用域(scope)。
局部变量,全局变量,在合法的作用域中使用变量
def calculateTax(price, tax_rate): total = price + (price * tax_rate) print my_price return totalmy_price = float(raw_input ("Enter a price: "))totalPrice = calculateTax(my_price, 0.06)print "price = ", my_price, " Total price = ", totalPrice
这段代码里,函数里有一个调用全局变量的句子,注意这样可以吗,试试看,为什么
>>> Enter a price: 5.565.56price = 5.56 Total price = 5.8936>>>
python允许你使用这个变量,但不要试图改变它
下面,试试这段在函数里修改一个全局变量的代码
def calculateTax(price, tax_rate): total = price + (price * tax_rate) my_price = 10000 print "my_price (inside function) = ", my_price return totalmy_price = float(raw_input ("Enter a price: "))totalPrice = calculateTax(my_price, 0.06)print "price = ", my_price, " Total price = ", totalPriceprint "my_price (outside function) = ", my_price
运行以后,这样
>>> ================================ RESTART ================================>>> Enter a price: 12.56my_price (inside function) = 10000price = 12.56 Total price = 13.3136my_price (outside function) = 12.56>>>
函数内外定义的变量值是否不同,知道为什么吗
强制为全局变量,关键字global。上面的代码,可以这样修改
def calculateTax(price, tax_rate): total = price + (price * tax_rate) global my_price
总结,变量命名,强烈建议不要重复使用变量名,使用不同的变量命名,可以避免混乱以及好多麻烦。