一个简单的函数示例如下:

    def maximum (x, y):
        if x > y:
            return x
        else:
            return y
    
    maximum (3, 2)

从中我们可以看出函数的一般使用方式:

  1. 通过def关键字定义函数
  2. 通过括号传入参数
  3. 通过return返回返回值
  4. 通过函数名调用参数

默认参数

Python的函数支持默认参数,示例如下:

    def func(a, b=5, c=10):
        print 'a is', a, 'and b is', b, 'and c is', c

    func(3, 7)
    func(25, c=24)
    func(c=50, a=100)

相关文章:

  • 2021-12-19
  • 2021-12-14
  • 2021-04-10
  • 2021-06-26
  • 2021-06-01
  • 2021-07-15
  • 2021-09-15
  • 2021-07-21
猜你喜欢
  • 2021-09-14
  • 2021-10-16
  • 2021-10-18
  • 2021-10-28
  • 2022-12-23
  • 2021-11-14
  • 2021-11-30
相关资源
相似解决方案