# -*- coding: cp936 -*- def gcd(m,n): #必须有缩进 while n: r = m%n m = n n = r return m print gcd(20,30) #默认参数 def area(width=2,height=3): return width*height print area(4) #第二个参数没有,用默认的3 print area(height=7,width=5) #这样写就不用在意参数的顺序 #返回多个值 def multi(a, b): return (a/b, a%b) print multi(7,3) #任意个参数,arg是列表 def f(*arg): sum = 0 for i in arg: sum += i return sum print f(1,2,3) #任意个参数,arg是字典 def g(**arg): for key in arg: print key,':',arg[key] g(a=1,b=2,c=3) #空函数 def h(a): pass #全局变量 parameter = 1 def glb(parameter): return parameter + globals()['parameter'] print glb(2) #嵌套函数 def multiplier(factor): def multiplyByFactor(number): return number*factor return multiplyByFactor double = multiplier(2) print double(5) print multiplier(2)(5) #重载 #不支持