question on void

This is a discussion on question on void within the C++ Programming forums, part of the General Programming Boards category; what exactly does void do? i have seen it used in the tutorials but cant find where it explains it. ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    Question question on void

    what exactly does void do? i have seen it used in the tutorials but cant find where it explains it.

    eg:

    Code:
    #include <stdio.h>
    
    void playgame();
    void loadgame();
    void playmultiplayer();
    	
    int main()
    {
        int input;
    
        printf( "1. Play game\n" );
        printf( "2. Load game\n" );
        printf( "3. Play multiplayer\n" );
        printf( "4. Exit\n" );
        printf( "Selection: " );
        scanf( "%d", &input );
        switch ( input ) {
            case 1:            /* Note the colon, not a semicolon */
                playgame();
                break;
            case 2:          
                loadgame();
                break;
            case 3:         
                playmultiplayer();
                break;
            case 4:        
                printf( "Thanks for playing!\n" );
                break;
            default:            
                printf( "Bad input, quitting!\n" );
                break;
        }
        getchar();
    
    }

    from the tutorials

    or similiar or other uses, i really dont understand what it does.

    thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It is being used to declare a couple of functions that do not return a value.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    Those are functions that declared in your main file.

    To get a better understanding on this, you can treat these as subroutines.

    So if you look clearly, you'll see that there're 3 subrountines defined.

    1. play game
    2. load game
    3. play multiplayer

    Each subroutine contains different logic. It's pretty obvious that the playgame routine will allow you to play the game. The loadgame routine will load the game for you, and the playmultiplayer routine will allow you to play with 1 or more players.
    -dc

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In C++, you have to list a return type for your functions. If your function does not return anything, then you signify that with "void", which means "nothing" or "an absence of value".
    Last edited by 7stud; 01-27-2006 at 07:21 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    It means, when you call a function, you can do this
    loadgame();

    Whereas if the function returned anything other than void, you would normally want to do this
    anAnswer = loadgame();

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Of course, if the function doesn't have a void return type, you can ignore the return value and treat it as if it returned void:
    Code:
    int function();
    
    function();
    A void function doesn't have to have a return statement. It can, but if it does, return isn't followed by anything:
    Code:
    void function() {
        /* ... */
        if(something) return;
        /* ... */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    You use functions of the type 'void' if you don't need an output value. For example if you have a function that only prints some text to the screen, you would use type 'void', because you don't need a return value.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    void is "not a type", the "non-type". Void basically says, there's nothing here. Therefore, a function returning void returns nothing, and a pointer to void points to nothing in particular (it's just an address).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    thanks a heap. all u ppl here are really helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Case Statement
    By danlee58 in forum C Programming
    Replies: 16
    Last Post: 11-23-2008, 07:46 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 06:53 AM

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