UN dichiarazione di restituzione viene utilizzato per terminare l'esecuzione della chiamata di funzione e restituisce il risultato (valore dell'espressione che segue la parola chiave return) al chiamante. Le istruzioni successive alle istruzioni return non vengono eseguite. Se l'istruzione return è priva di espressione, viene restituito il valore speciale None. UN ritorno dichiarazione viene generalmente utilizzato per richiamare una funzione in modo che le istruzioni passate possano essere eseguite.
Nota: L'istruzione Return non può essere utilizzata al di fuori della funzione.
Sintassi:
def fun(): statements . . return [expression]>
Esempio:
def cube(x): r=x**3 return r>
Esempio:
Python3
# Python program to> # demonstrate return statement> def> add(a, b):> > # returning sum of a and b> > return> a> +> b> def> is_true(a):> > # returning boolean of a> > return> bool> (a)> # calling function> res> => add(> 2> ,> 3> )> print> (> 'Result of add function is {}'> .> format> (res))> res> => is_true(> 2> <> 5> )> print> (> '
Result of is_true function is {}'> .> format> (res))> |
>
>
Produzione:
Result of add function is 5 Result of is_true function is True>
Restituzione di più valori
In Python, possiamo restituire più valori da una funzione. Di seguito sono riportati diversi modi.
- Utilizzo di Object: è simile a C/C++ e Java, possiamo creare una classe (in C, struct) per contenere più valori e restituire un oggetto della classe.
Esempio
Python3
# A Python program to return multiple> # values from a method using class> class> Test:> > def> __init__(> self> ):> > self> .> str> => 'geeksforgeeks'> > self> .x> => 20> > # This function returns an object of Test> def> fun():> > return> Test()> > # Driver code to test above method> t> => fun()> print> (t.> str> )> print> (t.x)> |
>
>Produzione
geeksforgeeks 20>
- Utilizzo della tupla: una tupla è una sequenza di elementi separati da virgole. Viene creato con o senza (). Le tuple sono immutabili. Vedere Questo per i dettagli di tupla .
Python3
onclick js
# A Python program to return multiple> # values from a method using tuple> > # This function returns a tuple> def> fun():> > str> => 'geeksforgeeks'> > x> => 20> > return> str> , x;> # Return tuple, we could also> > # write (str, x)> > # Driver code to test above method> str> , x> => fun()> # Assign returned tuple> print> (> str> )> print> (x)> |
>
>
- Produzione:
geeksforgeeks 20>
- Utilizzo di un elenco: un elenco è come un array di elementi creato utilizzando parentesi quadre. Sono diversi dagli array in quanto possono contenere elementi di tipo diverso. Le liste sono diverse dalle tuple poiché sono mutabili. Vedi questo per i dettagli dell'elenco.
Python3
# A Python program to return multiple> # values from a method using list> > # This function returns a list> def> fun():> > str> => 'geeksforgeeks'> > x> => 20> > return> [> str> , x];> > # Driver code to test above method> list> => fun()> print> (> list> )> |
>
>
- Produzione:
['geeksforgeeks', 20]>
- Utilizzo di un dizionario: un dizionario è simile all'hash o alla mappa in altre lingue. Vedere Questo per i dettagli di dizionario .
Python3
# A Python program to return multiple> # values from a method using dictionary> > # This function returns a dictionary> def> fun():> > d> => dict> ();> > d[> 'str'> ]> => 'techcodeview.com'> > d[> 'x'> ]> => 20> > return> d> > # Driver code to test above method> d> => fun()> print> (d)> |
>
mamta kulkarni
>
- Produzione:
{'x': 20, 'str': 'techcodeview.com'}>
Funzione che restituisce un'altra funzione
In Python, le funzioni sono oggetti, quindi possiamo restituire una funzione da un'altra funzione. Ciò è possibile perché le funzioni sono trattate come oggetti di prima classe in Python. Per saperne di più sugli oggetti di prima classe clicca qui.
Nell'esempio seguente, la funzione create_adder restituisce la funzione sommatore.
Python3
# Python program to illustrate functions> # can return another function> def> create_adder(x):> > def> adder(y):> > return> x> +> y> > return> adder> add_15> => create_adder(> 15> )> print> (> 'The result is'> , add_15(> 10> ))> # Returning different function> def> outer(x):> > return> x> *> 10> def> my_func():> > > # returning different function> > return> outer> # storing the function in res> res> => my_func()> print> (> '
The result is:'> , res(> 10> ))> |
>
>
Produzione:
The result is 25 The result is: 100>