-2

Is return none necessary? if not used what will happen does it keep returning variables and commands etc? thank you for the help

  • Does this answer your question? [return, return None, and no return at all?](https://stackoverflow.com/questions/15300550/return-return-none-and-no-return-at-all) – Gino Mempin Jun 05 '22 at 08:15
  • Related: [Is there a reason python functions should always return some value?](https://stackoverflow.com/q/66259318/2745495) – Gino Mempin Jun 05 '22 at 08:21
  • *return None* is not necessary. *return* without an expression will return None. If a function ends without an explicit *return* then it will implicitly return None. No idea what you mean by "returning ... commands" – DarkKnight Jun 05 '22 at 08:26

2 Answers2

2

In python, if you do not return anything, it will automatically (implicitly) return None.

You can test that by making a function like

def fun():
    pass

Then you can print the output of that function

print(fun())

And you will see that it will output None. Thus, it is not a must to return None.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Ahmad
  • 56
  • 6
1

No, return statements are not necessary in a Python function.

If there is no return statement, Python will implicitly return none when it reaches the end of the function.

Ryan Zhang
  • 1,856
  • 9
  • 19
  • 2
    An explicit `return` is sometimes needed for example, a function is doing a loop to search for a value and then raising an Exception if the loop did not find any. For that case, if you did find a value, you can break out of the loop by `return`-ing, – Gino Mempin Jun 05 '22 at 08:17