2

Here's Stack Overflow's own definition of a void:

Void :

An incomplete type used as syntactic place-holder for the return type of a method/function when no value is returned.

So, what does it return?

I know this may seem like a strange question since voids aren't supposed to return anything.

But I still wonder:

  • Do they return null?
  • Do they return undefined?
  • Does it just throw an exception?
  • Does it have a specific return value, and/or a specific system for these cases?

Or, probably, the question I should've asked first:

  • Does it change according to the language we're using? / Does it depend on the language we're using? (I'm using Java for this one)

So, what happens if I do System.out.println(function());, considering that function() is a void?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    I think “void” is misleading. Conceptually they always return the same value (of type “unit”), not “void”. – Guildenstern Jun 09 '20 at 10:10
  • 1
    Welcome to SO! It is a good question but too broad unfortunately. Every language with "void" keyword treat it differently. There will be several answers considering different language behaviour and it will be impossible to choose the best one out of them. It will be better to ask this question regarding specific language (perhaps you already has one in mind). – Sergey Shubin Jun 09 '20 at 11:13
  • 1
    Definitely depends on the language. For your `System.put.println` example you will get a compile-time error, because a void function does not return anything, so you cannot use it as an expression. – Thilo Jun 09 '20 at 12:49
  • "So, what happens if I do `System.out.println(function());`, considering that `function()` is a void?" In general, this will generate a compile time error as you cannot convert/cast the void "type" to a String (which is what println() expects). But some languages actually allow a "void pointer", which is a pointer that could point to anything. Here's a lengthy discussion on [void pointers in c](https://stackoverflow.com/questions/11626786/what-does-void-mean-and-how-to-use-it). – Idle_Mind Jun 09 '20 at 14:42

0 Answers0