• 技术文章 >常见问题 >Python常见问题

    python函数没有返回值吗

    silencementsilencement2019-11-13 14:26:18原创13031

    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学习网- 专业的python自学、交流公益平台!

    专题推荐:返回值
    上一篇:python怎么对字典排序 下一篇:python division如何取整

    相关文章推荐

    • python中return一般怎么写• python的return语法错误怎么解决• Python函数中必须要有return吗

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网