Retour de fonction

by Christian Nguyen, Joseph Razik, on 2019-10-18
retour_de_fonction

Pas d'instruction de retour

In [1]:
def r1():
    print('pas de retour ?')
    # pas d'instruction return
In [2]:
ret = r1()
pas de retour ?
In [3]:
print(ret)
None

Avec instruction de retour

In [4]:
def r2(a, b):
    if a > b:
        print('a plus grand que b')
        return a  # interruption
    print('et la suite ?')
In [5]:
r2(3, 1)
a plus grand que b
Out[5]:
3

Retour multiple

In [6]:
def r3(pl):
    return min(pl), max(pl)  # retour multiple possible ?
In [7]:
lnb = [10, 7, 9, 3, 17, 5]
ret1, ret2 = r3(lnb)
In [8]:
print(ret1, ret2)
3 17
In [9]:
print(type(r3(lnb)))
<class 'tuple'>