python基础1--函数
代码
def sayHello(name, age=25, pwd="****0404"):
print "name :", name, "\tage :", age, "\tpassword :", pwd
sayHello("koma", 22, "****0404")
sayHello("koma")
def func(a, b=5, c=10):
print "a =", a, "\tb =", b, "\tc =", c
func(12, 20)
func(7, c = 21)
func(3, c = 20, b = 17)
print "name :", name, "\tage :", age, "\tpassword :", pwd
sayHello("koma", 22, "****0404")
sayHello("koma")
def func(a, b=5, c=10):
print "a =", a, "\tb =", b, "\tc =", c
func(12, 20)
func(7, c = 21)
func(3, c = 20, b = 17)
运行结果:
name : koma age : 22 password : ****0404
name : koma age : 25 password : ****0404
a = 12 b = 20 c = 10
a = 7 b = 5 c = 21
a = 3 b = 17 c = 20
函数可以有默认值,这样类似于提供了简单的函数重载功能。
传递参数时,
(1)可以按照顺序传递,
(2)也可以不按照顺序传递,按名字给指定的参数赋值。