1

I started learning the C language recently, and have noted the function "void()", however, I would like to know what it does and it's best points of application, also perhaps an alternative to void that is potentially more productive. Thank you.

Dillon Chaffey
  • 109
  • 1
  • 2
  • 3
  • 8
    There is no function called `void`. Can you tell us what you have seen exactly with an example? – Jon Mar 23 '11 at 02:17

4 Answers4

4

There is no function called void, but a function can be declared with a return type of void. This means that the function doesn't return a value.

void DoSomething(...)
{
  ....
}

Update

void can also be used to indicate to the compiler that the function does not take any arguments. For example,

float CalculatePi(void)
{
 ....
}
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • My mistake. What I meant to say was: Is void in place of a function's arguments supposed to do anything special? Does it just tell the compiler that there is no specific argument for that function? – Dillon Chaffey Mar 23 '11 at 02:22
  • 6
    In C89, `f(void)` means the function takes no arguments, while `f()` means no type-checking is done on the arguments. – dan04 Mar 23 '11 at 02:25
4

void in C has three uses:

  1. To declare that a function does not return a value:

    void foo(int x);
    
  2. To declare that a function does not accept parameters:

    int baz(void);
    
  3. (DANGER WILL ROBINSON!) To declare a "universal pointer", that may be passed around as e.g. a magic cookie, or handed back to someone in a callback, there to be cast back to its original type.

    int register_callback(void (*foo)(void *baz), void *bar);
    

register_callback is called with a pointer to a void function that expects as parameter a pointer that presumably means something to it. At some (unspecified) time in the future, that function will be called, with bar as the parameter. You see this kind of thing in certain kinds of embedded executives and reusable device drivers, although not so much anywhere.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
John R. Strohm
  • 7,547
  • 2
  • 28
  • 33
  • a forth: casting an expression (function call or so) to throw away its value and only evaluate it for its side effects. – Jens Gustedt Mar 23 '11 at 07:20
  • point #3 deals with `void*`, not `void`. They're 2 very very different things. – pmg Mar 23 '11 at 09:35
  • @Jens: You can use it that way, but I've never seen anyone bother. When was the last time you did (void)printf(...)? – John R. Strohm Mar 23 '11 at 12:53
  • @John, you use that e.g if the expression has a `volatile` qualifier and you want to calm down the compiler. In my macros I also have something which often comes handy, a `P99_NOP` that is expanded to `(void)0`. E.g if you want to declare a macro that has several statements including a `break` you can do `if (1) { .... } else P99_NOP` instead of the commonly used `do { ... } while(0)`. – Jens Gustedt Mar 23 '11 at 13:10
  • @Jens, I generally don't believe in "calming down" the compiler. He's generally a lot smarter than I am about this kind of thing, and is telling me things I really want to know about. (At least, that's been my experience over the last 30 years or so...) – John R. Strohm Mar 23 '11 at 16:28
  • @John, there is no point in the compiler that an expression is without effect when I put it just there such that it has no effect. In any case, this is a valid use of `void`, mentioned in the standard as such. An since you are claiming a general statement about C, this one was missing. That's it. – Jens Gustedt Mar 23 '11 at 18:55
1

When void as function arguments is useful ?

#include "stdlib.h"
#include "stdio.h"

void foo();

int main()
{
    foo(5);    // Passing 5 though foo has no arguments. Still it's valid.
    return 0;
}

void foo()
{
    printf("\n In foo \n") ;
}

In the above snippet, though foo() prototype has no arguments it is still valid to pass something to it. So, to avoid such things to happen -

void foo(void) ;

Now it is guaranteed that passing anything to foo() would generate compiler errors.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • @Dillion One of your comment meant when `void` as function argument is useful. So, posted this snippet. – Mahesh Mar 23 '11 at 02:52
  • Calling `foo(5)` is NOT VALID, ie it invokes UB. To be valid it would have to be called with no parameters in accordance with the function definition (not declaration). The compiler uses the declaration to know about return type (and check arguments): in the above program no checking of arguments is done **but the arguments need to match the function definition** anyway. – pmg Mar 23 '11 at 09:39
  • The correct identification for the headers is `` and `` (note that you don't use anything declared in `` in your program). – pmg Mar 23 '11 at 09:42
0

I don't think there is a void() function.

However, the void keyword is used to indicate that a function doesn't return anything, e.g.:

void MyFunction() {
   // function body here
}
Meta
  • 1,663
  • 2
  • 18
  • 29