1.无参数
#无参数def func1(): print('in func1') return 99def func2(): print('in func2')y1=func1()y2=func2()print(y1,y2)
2.返回值:
#返回值def myprint(): import time with open('myprint.txt','a+') as f: f.write(time.asctime(time.localtime())) f.write('\n')def print1(): print('this is in print1') myprint()def print2(): print('this is in print2') myprint() return print1()def print3(): print('this is in print3') myprint() return print2()print3()
输出:
this is in print3this is in print2this is in print1
3.位置参数与关键字参数
定义函数:
def test(x,y): print('形参x=%s'%x) print('形参y=%s'%y)
#关键字参数调用test(y=2,x=1)x=10y=20test(y=x,x=y)test(x=x,y=y)
输出:
形参x=1形参y=2形参x=20形参y=10形参x=10形参y=20
原由:区分形参和实参
4.位置参数调用:
#位置参数调用test(10,20)
5.混合调用:
test(x=10,2)
File "E:/OldBoy/day01/函数与函数式编程.py", line 47 test(x=10,2) ^SyntaxError: positional argument follows keyword argument
说明关键字参数不能在位置参数前面。
test(2,x=10)
File "E:/OldBoy/day01/函数与函数式编程.py", line 49, in <module>
test(2,x=10)TypeError: test() got multiple values for argument 'x'说明位置参数和关键字参数在一起时候,都默认为位置参数。
test(2,y=10)
输出:
形参x=2形参y=10