Python の return ステートメント

return ステートメント は、関数呼び出しの実行を終了し、結果 (return キーワードに続く式の値) を呼び出し元に返すために使用されます。 return ステートメントの後のステートメントは実行されません。 return ステートメントに式がない場合は、特別な値 None が返されます。あ 戻る 声明 全体的に、渡されたステートメントを実行できるように関数を呼び出すために使用されます。

注記: return 文は関数外では使用できません。

構文:

def fun(): statements . . return [expression] 

例:

def cube(x): r=x**3 return r 

例:

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))>

出力:

Result of add function is 5 Result of is_true function is True 

複数の値を返す

Python では、関数から複数の値を返すことができます。以下にさまざまな方法があります。

    オブジェクトの使用: これは C/C++ や Java と似ており、複数の値を保持するクラス (C では struct) を作成し、そのクラスのオブジェクトを返すことができます。

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)>

出力

geeksforgeeks 20 
    タプルの使用: タプルは、カンマで区切られた項目のシーケンスです。 () の有無にかかわらず作成されます。タプルは不変です。見る これ 詳細については タプル

Python3




# 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)>

    出力:
geeksforgeeks 20 
    リストの使用: リストは、角かっこを使用して作成される項目の配列のようなものです。さまざまなタイプの項目を含めることができるため、配列とは異なります。リストは変更可能なため、タプルとは異なります。リストの詳細はこちらをご覧ください。

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> )>

    出力:
['geeksforgeeks', 20] 
    辞書の使用: 辞書は、他の言語のハッシュやマップに似ています。見る これ 詳細については 辞書

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)>

    出力:
{'x': 20, 'str': 'techcodeview.com'} 

別の関数を返す関数

Python では関数はオブジェクトなので、別の関数から関数を返すことができます。 Python では関数がファーストクラスのオブジェクトとして扱われるため、これが可能になります。ファースト クラス オブジェクトの詳細については、ここをクリックしてください。

以下の例では、create_adder 関数は adder 関数を返します。

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> ))>

出力:

The result is 25 The result is: 100