Thread: Can anyone tell me about void plz?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Location
    Aldershot, England, UK
    Posts
    2

    Can anyone tell me about void plz?

    Erm...
    I didn't understand about using void in functions.
    I know that void doesn't return value.
    but i am not clear how to use it in function.
    Can anyone please show me an example how to use void in function?

    Thanks in advance!

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quite simply, if you cannot return a meaningful value from a function, do not return anything. Declare its return type void (although, void is not a type). For example, a function that prints an int:

    Code:
    // Nothing can be meaningfully retrun so, it is void...
    void foo(int x)
    {
       std::cout << "The int: " << x << std::endl;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Aldershot, England, UK
    Posts
    2
    thanks for reply

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Some people also like to use void to explicitly indicate that a function has no parameters. So the two functions declarations below are essentially the same:

    void foo();
    void bar(void);
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    They are the same in C++ since a prototype is a declaration. In C, however, void foo() is a declaration and void bar(void) is a prototype. A prototype is a declaration but a declaration is not a prototype.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Remember, main never returns void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM