When can void () be used and what are the benefits of this

I recently started to learn the C language and noticed the "void ()" function, but I would like to know what it does, and these are the best points of the application, and also, perhaps, an alternative to the void, which is potentially more productive, Thank you.

0
source share
4 answers

There is no function named void , but a function can be declared with a return type of void . This means that the function does not return a value.

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

Update

void can also be used to tell the compiler that a function does not accept any arguments. For instance,

 float CalculatePi(void) { .... } 
+3
source

void in C has three uses:

  • Declare that the function does not return a value:

     void foo(int x); 
  • Declare that the function does not accept parameters:

     int baz(void); 
  • (ROBINSON WILL BE DANGEROUS!) Declare a "universal pointer" that can be passed, for example, a magic cookie or passed to someone in a callback to return it to its original type.

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

register_callback is called with a pointer to the void function, which expects as a parameter a pointer that supposedly means something to it. At some (unspecified) time in the future, this function will be called, and bar - as a parameter. You see these things in some kinds of built-in managers and reusable device drivers, although not many.

+3
source

When are void as function arguments useful?

 #include "stdlib.h" #include "stdio.h" void foo(); int main() { foo(5); // Passing 5 though foo has no arguments. Still it valid. return 0; } void foo() { printf("\n In foo \n") ; } 

In the above snippet, although the prototype foo() has no arguments, it is still valid to pass anything to it. So, to avoid such events -

 void foo(void) ; 

It is now guaranteed that passing anything to foo() will lead to compiler errors.

+1
source

I do not think there is a void () function.

However, the void keyword is used to indicate that the function returns nothing, for example:

 void MyFunction() { // function body here } 
0
source

All Articles