Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to not return anything from a function in python?

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?

like image 531
beoliver Avatar asked Apr 08 '12 22:04

beoliver


People also ask

Can a function not return anything?

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 .

Do all Python functions return something?

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.

What happens if u dont return a function 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.

How do you ignore a return value in Python?

1 Answer. To Ignore python multiple return value you can use the "_" as a variable name for the elements of the tuple.


1 Answers

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.

  • If you have something to return then do so, if not then don't.
  • If you rely on something being returned that has a certain type but you cannot return anything meaningful of this type then 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)
like image 168
Nobody moving away from SE Avatar answered Oct 13 '22 09:10

Nobody moving away from SE