11.2. Function Return

11.2.1. Rationale

return

Python keyword for specifying value outcome from a function.

11.2.2. Syntax

def myfunction():
    return <expression>
>>> def mean():
...     return (1+2) / 2
>>> def add():
...     a = 1
...     b = 2
...     return a + b

11.2.3. Return Keyword

  • return keyword indicates outcome of the function

>>> def hello():
...     return 'hello'
>>>
>>>
>>> hello()
'hello'

Code after return will not execute:

>>> def hello():
...     print('before')
...     return 'hello'
...     print('after')
>>>
>>> hello()
before
'hello'

You can have more than one return keyword in a function, although function will close after hitting any of them, and will not proceed any further.

>>> def hello():
...     print('before')
...     return 'hello'
...     return 'world'
...     print('after')
>>>
>>> hello()
before
'hello'
>>> def hello():
...     print('before')
...     return 'hello'
...     print('between')
...     return 'world'
...     print('after')
>>>
>>> hello()
before
'hello'
>>> def hello():
...     if True:
...         return 'hello'
...     else:
...         return 'world'
>>>
>>> hello()
'hello'
>>> def hello():
...     if True:
...         return 'hello'
...     else:
...         return 'world'
...     print('after if')
>>>
>>> hello()
'hello'

11.2.4. Return Basic Type

>>> def myfunction():
...     return 42
>>> def myfunction():
...     return 13.37
>>> def myfunction():
...     return 'Mark Watney'
>>> def myfunction():
...     return True

11.2.5. Return Sequence

>>> def myfunction():
...     return list([42, 13.37, 'Mark Watney'])
>>> def myfunction():
...     return [42, 13.37, 'Mark Watney']
>>> def myfunction():
...     return tuple((42, 13.37, 'Mark Watney'))
>>> def myfunction():
...     return (42, 13.37, 'Mark Watney')
>>> def myfunction():
...     return 42, 13.37, 'Mark Watney'
>>> def myfunction():
...     return set({42, 13.37, 'Mark Watney'})
>>> def myfunction():
...     return {42, 13.37, 'Mark Watney'}

11.2.6. Return Mapping

>>> def myfunction():
...     return dict(firstname='Mark', lastname='Watney')
>>> def myfunction():
...     return {'firstname': 'Mark', 'lastname': 'Watney'}

11.2.7. Return Nested Sequence

>>> def myfunction():
...     return [('Mark', 'Watney'),
...             {'Jan Twardowski', 'Melissa Lewis'},
...             {'astro': 'Иван Иванович', 'agency': {'name': 'Roscosmos'}},
...             {'astro': 'Jiménez', 'missions': ('Mercury', 'Gemini')},
...             {'astro': 'Martinez', 'missions': (list(), tuple(), set())}]

11.2.8. Return None

  • Python will return None if no explicit return is specified

>>> def myfunction():
...     return None
>>> def myfunction():
...     print('hello')
>>> def myfunction():
...     pass
>>> def myfunction():
...     """My function"""

11.2.9. Intercept Returned Value

>>> def myfunction():
...     return 1
>>>
>>>
>>> result = myfunction()
>>> print(result)
1

11.2.10. Assignments

Code 11.2. Solution
"""
* Assignment: Function Return Numbers
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min

English:
    1. Define function `compute` without parameters
    2. Function should return sum of `1` and `2`
    3. Define `result` with result of function call
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj funkcję `compute` bez parametrów
    2. Funkcja powinna zwracać sumę `1` i `2`
    3. Zdefiniuj `result` z wynikiem wywołania funkcji
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isfunction

    >>> isfunction(compute)
    True
    >>> result
    3
"""