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
21
votes
4 answers

Why does ostream::write() require ‘const char_type*’ instead of ‘const void*’ in C++?

The fwrite() function in C uses const void *restrict buffer as the first argument, so you can pass pointer to your struct as the first parameter directly. http://en.cppreference.com/w/c/io/fwrite e.g. fwrite(&someStruct, sizeof(someStruct), 1,…
Mr. Ree
  • 871
  • 9
  • 20
21
votes
5 answers

java "void" and "non void" constructor

I wrote this simple class in java just for testing some of its features. public class class1 { public static Integer value=0; public class1() { da(); } public int da() { class1.value=class1.value+1; return 5; …
Hadi
  • 1,203
  • 1
  • 10
  • 20
20
votes
1 answer

async Task vs async void

This might be a very stupid question, but I have the following lines of coding that convert RAW images to BitmapImages: public async void CreateImageThumbnails(string imagePath, int imgId) { await Task.Run(() => controlCollection.Where(x =>…
CareTaker22
  • 1,260
  • 3
  • 18
  • 36
20
votes
4 answers

How to implement an interface member that returns void in F#

Imagine the following interface in C#: interface IFoo { void Bar(); } How can I implement this in F#? All the examples I've found during 30 minutes of searching online show only examples that have return types which I suppose is more common in…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
20
votes
3 answers

How can I resolve this case of "Useless use of a variable in a void context"?

How can I resolve this case of "Useless use of a variable in a void context"? For example: my $err = $soap_response->code, " ", $soap_response->string, "\n"; return $err; I get warnings like "Useless use of a variable in a void context"? Why?…
joe
  • 34,529
  • 29
  • 100
  • 137
20
votes
5 answers

Can a C# lambda expression ever return void?

I have the following method, and I want to know if there is anything that can go in place default(void) below because there is a compiler error that says that void is not valid here: private void applyDefaultsIfNecessary(ApplicationConfiguration…
Raghu Dodda
  • 1,505
  • 1
  • 21
  • 28
20
votes
5 answers

java.lang.Void vs void vs Null

What exactly is the difference between Void, void, and can I just use null instead? I'm asking this is because I'm looking at sample Android code where they used Void but Eclipse errors on it (it says Void cannot be resolved to a variable). My code…
Josh
  • 3,264
  • 1
  • 23
  • 35
19
votes
3 answers

C# thread method return a value?

Possible Duplicate: Access return value from Thread.Start()'s delegate function public string sayHello(string name) { return "Hello ,"+ name; } How can i use this method in Thread? That ThreadStart method just accept void methods. I'm…
Ogan Tabanlı
  • 209
  • 1
  • 3
  • 10
19
votes
4 answers

What is calling void(); doing?

I came across void(); being used as a 'do-nothing' in the 'else' branch of a ternary operator, as a shorthand for a null pointer check if(var){ var->member(); } as var ? var->member() : void(); but I can't seem to find any reference to the…
Jay
  • 2,553
  • 3
  • 17
  • 37
19
votes
6 answers

Why does the Void wrapper class exists in the JDK?

Possible Duplicate: Uses for the Java Void Reference Type? What is the real usage of Void class in real world problems? In which scenario can we use this class?
user1623177
19
votes
2 answers

in c: func(void) vs. func()

When a C function does not accept any arguments, does it have to be declared/defined with a "void" parameter by the language rules? PC-Lint seems to have problems when there's nothing at all in the argument-list, and I was wondering if it's…
noamtm
  • 12,435
  • 15
  • 71
  • 107
18
votes
5 answers

Using void in functions without parameter?

In C++ using void in a function with no parameter, for example: class WinMessage { public: BOOL Translate(void); }; is redundant, you might as well just write Translate();. I, myself generally include it since it's a bit helpful when…
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
18
votes
1 answer

PostgreSQL functions returning void

Functions written in PL/pgSQL or SQL can be defined as RETURNS void. I recently stumbled upon an odd difference in the result. Consider the following demo: CREATE OR REPLACE FUNCTION f_sql() RETURNS void LANGUAGE sql AS 'SELECT NULL::void'; --…
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
18
votes
2 answers

Sending items in a LINQ sequence to a method that returns void

Often while I'm dealing with LINQ sequences, I want to send each item to a method returning void, avoiding a foreach loop. However, I haven't found an elegant way to do this. Today, I wrote the following code: private StreamWriter _sw; …
a developer
  • 423
  • 1
  • 5
  • 12
18
votes
11 answers

Genericity vs type-safety? Using void* in C

Coming from OO (C#, Java, Scala) I value very highly the principles of both code reuse and type-safety. Type arguments in the above languages do the job and enable generic data structures which are both type-safe and don't 'waste' code. As I get…
Joe
  • 46,419
  • 33
  • 155
  • 245