void

This is a discussion on void within the C++ Programming forums, part of the General Programming Boards category; what is void and where do i use it? example void gameplay() i dont get why it cant just be ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    void

    what is void and where do i use it?

    example

    void gameplay() i dont get why it cant just be int gameplay()

    and also could you just put this in every program or are there certain sircumstances and what does it do?

    int main( int argc, char* argv[])

    ps.the codes were to small to bother with code blocks...
    Last edited by lilhawk2892; 07-02-2006 at 08:47 PM.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    void signifies lack of type.

    if you have int gameplay(), you must return a value. If your function has nothing meaningful to return then int gamplay() is at best wasted effort (an extra "return 0") and at worst confusing to users (what the hell do I do with the int returned from gameplay???)

    basically void signifies you don't care about the return value/arguements.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    ok and why bother with the () about void gameplayer

    but int can always be used its just pointless at times and how do i know when i dont care about the return value

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Because you aren't returning an int, so there is no reason to. The void data type is used when the function returns nothing. If you're passing data to a class to store, then you would return void because the class doesn't need to return any data. Or if you have a function that just outputs a line of text... it doesn't need to return anything.

    Void can also be specified as a pointer which can point to any memory address regardless of the value. It also can be used to implicitly specify that there is no arguments being passed to a function, but is not required in C++ as it is in C.
    Code:
    // Example 1
    void myClass::setData(int d) {
       this->data = d;
       return;   // This line is optional
    }
    
    // Example 2
    int data = 5;
    int *myIntPtr = &data;
    void *myVoidPtr = myIntPtr;
    
    char info = 'A';
    char *myCharPtr = &info;
    myVoidPtr = myCharPtr;
    
    // Example 3
    int myClass::getData(void) {
       return this->data;
    }
    Last edited by SlyMaelstrom; 07-02-2006 at 08:55 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    thanks ALOT,too bad i could never figure out a use for pointers

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Just keep on learning... soon their usage will hit you in the face like a ton of bricks.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    lol,yeah i've been noticing that alot now.
    especially with switch statements,i use them all the time

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. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 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. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21