ITPub博客

首页 > IT职业 > IT图书 > python函数有返回值吗

python函数有返回值吗

IT图书 作者:yellowduck007 时间:2021-09-11 10:29:25 0 删除 编辑

Python的函数可以有返回值,也可以没有返回值。函数需要先定义后调用,函数体中 return 语句的结果就是返回值。如果一个函数没有 reutrn 语句,其实它有一个隐含的 return 语句,返回值是 None,类型也是 'NoneType'。

例如

def showplus(x):
    print(x)
    return x + 1
     
num = showplus(6)
add = num + 2
print(add)

输出结果为

6
9

隐含return的情况

def showplus(x):
    print(x)
 
num = showplus(6)
print(num)
print(type(num))

输出结果

6
None
<class 'NoneType'>

更多学习内容,请点击Python学习网。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2611/viewspace-2835883/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    88
  • 访问量
    983