with a simple filter that test input against a range 0-100.
def foo(foo_input): if 0 <= foo_input <= 100: return f_input
This returns none if foo_input is > 100
. But could it actually 'not' return anything? or does a function allways have to return something?
No. If a return statement is not reached before the end of the function then an implicit None is returned. Show activity on this post. If a return statement is not reached, the function returns None .
Answer. NO, a function does not always have to have an explicit return statement. If the function doesn't need to provide any results to the calling point, then the return is not needed. However, there will be a value of None which is implicitly returned by Python.
We can use the return statement inside a function only. In Python, every function returns something. If there are no return statements, then it returns None. If the return statement contains an expression, it's evaluated first and then the value is returned.
1 Answer. To Ignore python multiple return value you can use the "_" as a variable name for the elements of the tuple.
Functions always return something (at least None
, when no return-statement was reached during execution and the end of the function is reached).
Another case is when they are interrupted by exceptions. In this case exception handling will "dominate over the stack" and you will return to the appropriate except
or get some nasty error :)
Regarding your problem I must say there are two possibilities: Either you have something to return or you do not have.
None
will tell the caller that this was the case ( There is no better way to tell the caller that "nothing" is returned then by None
, so check for it and you will be fine)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With