FunctionsΒΆ

  • Functions in Python do not specify argument types, only names (dynamic typing)
>>> def f(a, b, c=0):
...     return a + b + c
...
>>> f(1, 2, 3)
6
>>> f(1, 2)
3
>>> f(1, 2.0)
3.0
>>> f("one ", "two ", "three")
'one two three'
  • ALL functions return a value; the default return value is None
>>> def g(x, y = None):
...     if y is not None:
...         return x + y
...
>>> g(1,4)
5
>>> print g(1)
None