[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppVoid

(C++) void

Keyword denoting that either

The function returns nothing

void sayHello()
{
  std::cout << "Hello world" << std::endl;
}


The function takes no arguments (in C)

(From the C++ MessageBoard, http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=3&MsgID=339047&Setting=P0030F0001, thanks to stober)

In C, int myFunction(void); is the declaration of a function taking no arguments. In C++, int myFunction(); is preferred (see below) to specify this. In C, however, there is a difference between the two, as in C, int myFunction(); denotes a function declaration taking ANY number of unspecified parameter.

Here a (modified) example submitted by stober:

void func1();
void func2(void);
int main()
{
  int a = 0;
  int b = 0;
  func1();
  func1(a);  
  func1(a,b);
  func2(a); //Error on this line! func2 has no arguments!
}


The function takes no arguments (in C++)

This is called 'an abomination' by Bjarne Stroustrup (the creator of C++), Dennis Ritchie, the co-creator of C, and Doug McIlroy, head of the research department where Unix was born [1,3]. In [1] Bjarne Stroustrup gives the following code as example:
int f(void) ; // error: abomination
int g() ; // g takes no argument


The function returns a pointer of unknown data type

void* someFunction();
( Sorry, I haven't got a better example. I need to look in some Win32 API for one).

Avoid void* except in low-level code [2]

See void* for more details.

'void' links

References



last edited (May 20, 2007) by bilderbikkel, Number of views: 6941, Current Rev: 22 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.