Are empty function returns type void?


#1

One thing I don’t understand well is why some languages require a specified return type, and some don’t. My assumption is the type is inferred. Is this correct, and if yes, are empty return operations type void?


#2

It’s actually not inferred. It depends on if the language is an expression based language or a statement based language. In a statement based language like Python, not everything is intended to return a value. However, in Ruby everything is supposed to have a value returned. Sometimes that value is nil. The other interesting thing is it looks like Python three has made a lot more of the language expression based versus previously. Take a look at this Python 3:

>>> x = print("Hello")
Hello
>>> x

versus this Python 2 code:

>>> x = print "Howdy."
  File "<stdin>", line 1
    x = print "Howdy."
            ^

in the first example, the print function is a function so that means it will return a value even though it’s not really going to do that. But in the second example, print is not really a function it’s considered a statement that means it doesn’t have a value, won’t ever return anything, and you can’t use it in and equals, and a lot of other places where you could put a value.

However, whether this is good or bad either way is up for debate. Some say that a statement based language is better because you can’t put certain things in the wrong place. Others say that makes language inconsistent.