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.

1743 questions
1535
votes
14 answers

What does "javascript:void(0)" mean?

login I've seen such hrefs many times, but I don't know what exactly that means.
omg
  • 123,990
  • 135
  • 275
  • 341
1036
votes
10 answers

How to mock void methods with Mockito

How to mock methods with void return type? I implemented an observer pattern but I can't mock it with Mockito because I don't know how. And I tried to find an example on the Internet but didn't succeed. My class looks like this: public class World…
ibrahimyilmaz
  • 16,643
  • 12
  • 56
  • 79
394
votes
11 answers

Why does a function with no parameters (compared to the actual function definition) compile?

I've just come across someone's C code that I'm confused as to why it is compiling. There are two points I don't understand. The function prototype has no parameters compared to the actual function definition. The parameter in the function…
AdmiralJonB
  • 1,896
  • 2
  • 18
  • 25
228
votes
6 answers

Is it better to use C void arguments "void foo(void)" or not "void foo()"?

What is better: void foo() or void foo(void)? With void it looks ugly and inconsistent, but I've been told that it is good. Is this true? Edit: I know some old compilers do weird things, but if I'm using just GCC, is void foo() Ok? Will foo(bar);…
Zifre
  • 24,944
  • 8
  • 81
  • 102
221
votes
9 answers

Java 8 lambda Void argument

Let's say I have the following functional interface in Java 8: interface Action { U execute(T t); } And for some cases I need an action without arguments or return type. So I write something like this: Action a = () -> {…
Wickoo
  • 5,026
  • 3
  • 24
  • 38
192
votes
11 answers

Unit testing void methods?

What is the best way to unit test a method that doesn't return anything? Specifically in c#. What I am really trying to test is a method that takes a log file and parses it for specific strings. The strings are then inserted into a database. Nothing…
jdiaz
  • 6,884
  • 12
  • 39
  • 50
166
votes
11 answers

Uses for the Java Void Reference Type?

There is a Java Void -- uppercase V-- reference type. The only situation I have ever seen it used is to parameterize Callables final Callable callable = new Callable() { public Void call() { foobar(); …
Julien Chastang
  • 16,970
  • 12
  • 59
  • 88
149
votes
3 answers

Python void return type annotation

In python 3.x, it is common to use return type annotation of a function, such as: def foo() -> str: return "bar" What is the correct annotation for the "void" type? I'm considering 3 options: def foo() -> None: not logical IMO, because None…
Tregoreg
  • 11,747
  • 12
  • 40
  • 63
147
votes
5 answers

What's the better (cleaner) way to ignore output in PowerShell?

Let's say you have a method or a cmdlet that returns something, but you don't want to use it and you don't want to output it. I found these two ways: Add-Item > $null [void]Add-Item Add-Item | Out-Null What do you use? Which is the better/cleaner…
Hinek
  • 8,900
  • 12
  • 48
  • 71
123
votes
2 answers

How to create function that returns nothing

I want to write a function with pl/pgsql. I'm using PostgresEnterprise Manager v3 and using shell to make a function, but in the shell I must define return type. If I don't define the return type, I'm not able to create a function. How can create a…
Kabi
  • 1,525
  • 5
  • 17
  • 21
117
votes
6 answers

What do I return if the return type of a method is Void? (Not void!)

Due to the use of Generics in Java I ended up in having to implement a function having Void as return type: public Void doSomething() { //... } and the compiler demands that I return something. For now I'm just returning null, but I'm wondering…
Daniel Rikowski
  • 66,219
  • 52
  • 237
  • 318
114
votes
9 answers

Why cast unused return values to void?

int fn(); void whatever() { (void) fn(); } Is there any reason for casting an unused return value to void, or am I right in thinking it's a complete waste of time? Follow up: Well that seems pretty comprehensive. I suppose it's better than…
markh44
  • 5,344
  • 5
  • 25
  • 33
101
votes
2 answers

Why cast an unused function parameter value to void?

In some C project, I have seen this code: static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { (void)ud; (void)osize; /* some code not using `ud` or `osize` */ return ptr; } Do the two casts to void serve any…
bastibe
  • 14,886
  • 24
  • 86
  • 118
94
votes
11 answers

Is it bad practice to use return inside a void method?

Imagine the following code: void DoThis() { if (!isValid) return; DoThat(); } void DoThat() { Console.WriteLine("DoThat()"); } Is it OK to use a return inside a void method? Does it have any performance penalty? Or it would be better…
Rodrigo
86
votes
4 answers

Why does this Java 8 lambda fail to compile?

The following Java code fails to compile: @FunctionalInterface private interface BiConsumer { void accept(A a, B b); } private static void takeBiConsumer(BiConsumer bc) { } public static void main(String[] args) { …
Brian Gordon
  • 5,993
  • 2
  • 28
  • 37
1
2 3
99 100