python--函数的返回值、函数的参数(python定义函数返回值的调用)

网友投稿 552 2022-08-25


python--函数的返回值、函数的参数(python定义函数返回值的调用)

函数的返回值

特性:

减少重复代码使程序变的可扩展使程序变得易维护

函数名:命名规则和变量一样

函数的返回值:

return, 函数执行完毕. 不会执行后面逻辑

如果函数中不写return返回None只写return 返回Nonereturn 返回值. 返回一个值return 值1, 值2,... 返回多个值. 调用方接收到的是元组

遇到return结束函数里的语句,下面的不会执行

没有return,返回None

def foo(): print('foo')foo # 表示是函数foo() # 表示执行foo函数

def sayhi(): # 函数名 print("Hello, I'm nobody!")sayhi() # 调用函数

结果:

Hello, I'm nobody!

之前写的乘法是这样的

a, b = 5, 8c = a ** bprint(c)

改成函数之后是这样的

a, b = 5, 8# 改成用函数写def calc(x, y): res = x ** y return res # 返回函数执行结果c = calc(a, b) # 结果赋值给c变量print(c)

函数和过程

函数有return 过程没有return

# 函数def func1(): print('in the func1') return 0# 过程def func2(): print('in the func2')x = func1()y = func2()print('from func1 return is %s' % x)print('from func2 return is %s'

结果:

in the func1in the func2from func1 return is 0from func2 return is

import time # 导入time模块def logger(): # 定义了函数名为logger time_format = '%Y-%m-%d %X' # 时间格式,Y年m月d日X时分秒 time_current = time.strftime(time_format) # time.strftime引用上面的时间格式 with open('ccc.txt', 'a+') as f: f.write('%s end action\n' % time_current) # 将日期时间和end action写入到f里def aaa(): print('in the test1') logger()def bbb(): print('in the test2') logger()def ccc(): print('in the test3') logger()aaa()bbb()ccc()

结果:

in the test1in the test2in the test3ccc.txt里写入2020-06-15 22:17:37 end action2020-06-15 22:17:37 end action2020-06-15 22:17:37 end action

def test(): print('in the test') return 0 print('end test')test()

结果:

in the test# 遇到return结束函数里的语句,下面的不会执行

def test(): print('in the test') return 0x = test()print(x)

结果:

in the test0

x=test()先执行函数里的内容,最后将函数里的返回值赋给x

def test(): print('in the test') return 1print(test()) # 相当于x=test() print(x)

结果:

in the test1

没有 return 返回 None

def test(): print('in the test')x = test()print(x)

结果:

in the testNone

def test1(): print('in the test1')def test2(): print('in the test2') return 0def test3(): print('in the test3') return 1, 'hello', {'name': 'zouzou'} return test2x = test1()y = test2()z = test3()print(x)print(y)print(z)

结果:

in the test1in the test2in the test3None0(1, 'hello', {'name': 'zouzou'})

先执行x,y,z,在打印他们的返回值

函数的参数

关键字参数不能写在位置参数前面(定义函数时)参数组不能在位置参数和关键字参数前面默认参数放在最后面

形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量

实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值

def test(x, y): # x,y叫形参 print(x) print(y)test(2, 4) # 位置参数,实参

def test(x, y): # x,y叫形参 print(x) print(y)test(x=2, y=3) # 关键字参数

def test(x, y=3): # y为默认参数 print(x) print(y)test(2)

参数组

*args接收的位置参数,转换成元组的形式,没有传参返回空元组

**kwargs接收的关键字参数,转换成字典的形式,没有传参返回空字典

def test(**kwargs): # 参数组,接收的参数不确定时用** print(kwargs)test()

结果:

{}

def test(*args): # 参数组,接收的参数不确定时用* print(args)test()

结果:

()

def test(x, *args): print(x) print(args) # 打印的是一个元组test(2, 4, 6, 8)test(*[6, 8, 9, 45, 5]) # args=tuple[6,8,9,45,5]

结果:

2(4, 6, 8)6(8, 9, 45, 5)

def test(**kwargs): # 参数组,接收的参数不确定时用* print(kwargs) # 打印的是一个字典 print(kwargs['name']) print(kwargs['age']) print(kwargs['job'])test(name='zou', age=18, job='IT') # 接收的是关键字参数

结果:

{'name': 'zou', 'age': 18, 'job': 'IT'}zou18IT


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python--函数的返回值、函数的参数(Python函数返回值)
下一篇:java实现简单的学生管理系统
相关文章

 发表评论

暂时没有评论,来抢沙发吧~