Questions tagged [void]

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

Programming languages derived from C or Algol68, like C++, C#, Java, etc., may define the return type of methods/functions as void when the method/function does not return a value, but simply completes execution.

An example of its use in Java, compared to a non-void method, is:

int nonVoidMethod {
    // do something
    return 0; // return a value to the caller
}

void voidMethod {
    // do something
    return; // no value allowed to be returned from a void method
    // a "return" statement is not required
}

Although void is used as a type, it's an incomplete type:

  • In some languages, such as Java and Algol68, void is only a keyword used as a return type. It is not considered as a valid type in other language constructs.

  • Other languages, like C and C++, consistently define void as a type with an empty set of values. This allows its use for example in compound type constructs (void pointers). But no void objects can be instantiated.

1913 questions
67
votes
4 answers

What's the difference between Void and no parameter?

I have a class which defines two overloaded methods public void handle(Void e) protected void handle() Obviously they are different, especially handle(Void e) is public. What's the difference between those two? How to call the first method? I…
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
64
votes
10 answers

Difference between int main() and int main(void)?

What does the following mean : int main(void) {...} VS int main() {...} ? I think that int main() {...} means that main doesn't receive any parameters (from command line) , however: int main(int argc, char *argv[]) does. But, what does int…
JAN
  • 21,236
  • 66
  • 181
  • 318
63
votes
6 answers

variable or field declared void

I have a function called: void initializeJSP(string Experiment) And in my MyJSP.h file I have: 2: void initializeJSP(string Experiment); And when I compile I get this error: MyJSP.h:2 error: variable or field initializeJSP declared void Where is…
Eduardo
  • 19,928
  • 23
  • 65
  • 73
62
votes
4 answers

What is the need of Void class in Java

I am not clear with the class java.lang.Void in Java. Can anybody elaborate in this with an example.
giri
  • 26,773
  • 63
  • 143
  • 176
59
votes
5 answers

Void as return type

I was testing return types with PHP 7. I've created a simple script to test return types of PHP 7:
Daan
  • 12,099
  • 6
  • 34
  • 51
59
votes
7 answers

How can I return a default value for an attribute?

I have an object myobject, which might return None. If it returns None, it won't return an attribute id: a = myobject.id So when myobject is None, the stament above results in a AttributeError: AttributeError: 'NoneType' object has no attribute…
alwbtc
  • 28,057
  • 62
  • 134
  • 188
57
votes
3 answers

Should I use Unit or leave out the return type for my scala method?

I am not sure what the difference is between specifying Unit as the return type of my scala method or leaving out the return type altogether. What is the difference? Can anyone please advise?
balteo
  • 23,602
  • 63
  • 219
  • 412
57
votes
14 answers

Why can't I explicitly return void from a method?

void run() { ... if (done) return cancel(); ... } where cancel() return void. This won't compile... and I can almost understand why. But if I want to return a void from a void, why not? Instead, I end up writing something like this: if…
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
57
votes
4 answers

JavaScript `undefined` vs `void 0`

What exactly is the difference between undefined and void 0 ? Which is preferred and why?
Pacerier
  • 86,231
  • 106
  • 366
  • 634
55
votes
6 answers

Is sizeof(void()) a legal expression?

From [5.3.3/1], I found that: The sizeof operator shall not be applied to an expression that has function or incomplete type From [3.9/5] I found that: Incompletely-defined object types and cv void are incomplete types Anyway, for sizeof does…
skypjack
  • 49,335
  • 19
  • 95
  • 187
53
votes
4 answers

Understanding the difference between f() and f(void) in C and C++ once and for all

Ok, so I have heard different opinions on this subject and just want to make sure I understand it correctly. For C++ Declarations void f(); and void f(void); mean precisely the same thing, the function f does not take any parameters. Ditto for…
user500944
45
votes
7 answers

Why can't we declare a variable of type void?

I'm looking for a formal explanation of that fact in the Standard. I've found what 3.9.1/9 says and trying to give an explanation used that section. Section 3.9.1/9, N3797: The void type has an empty set of values. The void type is an incomplete…
user2953119
41
votes
3 answers

int main() vs void main() in C

In C, I know that int main() returns an int where void main() does not. Other than that, is there a difference between them? Is the first better than the second?
madU
  • 649
  • 2
  • 6
  • 7
41
votes
4 answers

What is System.Void?

I know that methods declared with void does not return anything. But it seems that in C#, void is more than just a keyword, but a real type. void is an alias for System.Void, like int that is for System.Int32. Why am I not allowed to use that type?…
ordag
  • 2,497
  • 5
  • 26
  • 35
41
votes
3 answers

How to delete void pointer?

Is there anything wrong when deleting an object like this in C++? MyCls* c = new MyCls(); void* p = (void*)c; delete (MyCls*)p;
user3277361
  • 519
  • 1
  • 4
  • 4