void keyword

This is a discussion on void keyword within the C++ Programming forums, part of the General Programming Boards category; Hi I have a fuction starting like: void print ()..., what is the void keyword there for?...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    17

    void keyword

    Hi

    I have a fuction starting like: void print ()..., what is the void keyword there for?

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    It tells the compiler that your function doesn't return anything.

    If it were "int print()", then your function would be returning an int.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I find that it's best to return a value to indicate success, so that the code that called the function knows how to proceed. This is the function template I use:

    Code:
    BOOL DoSomething()
    {
        BOOL bSuccess=false;
    
        //once the function has successfully completed its task:
        bSuccess=true;
    
        return bSuccess;
    }
    This can make debugging easier by allowing you to quickly determine which functions cause the error. Thus, development is sped up.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, for some reason I cringe when i see void anything... not just void main... i don't like the void keyword unless it's in the parameters:
    Code:
    bool somefunction(void);
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Lol, what are you, a C programmer ;-)

    You can also use try..catch blocks if your function doesnt return anything:
    Code:
    try
    {
    somefunction();
    }
    catch (const Error&)
    {
    
    //...
    }
    //...
    void somefunction()
    {
    throw (Error);
    }
    Although my syntax might not be right since I don't have a compiler with me
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Originally posted by JaWiB
    Although my syntax might not be right since I don't have a compiler with me
    Error would have to be a valid type obviously (it'll most likely be a typedef) but it looks all right.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by major_small
    yeah, for some reason I cringe when i see void anything... not just void main... i don't like the void keyword unless it's in the parameters:
    Code:
    bool somefunction(void);
    You blokes are funny. If I have something, like a print method, I would cringe doing something like:
    Code:
    bool blah::print() const {
      cout << data << endl;
      return true;
    }
    I don't like the idea of a function returning the same value every time it's called. I mean if it is never going to be checked, then it has no purpose, it has no "why". But of course that's just me.

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I don't like the idea of a function returning the same value every time it's called.
    Yeah it seems kind of stupid. However, that is not what occurs in my functions. I return bSuccess=true when the function is successful. I return bSuccess=false when the function is not successful. I always check the return values from my functions. Thus, it is very useful.

    You blokes are funny.
    Being pedantic, and sticking to the standard, is a good character trait. It (helps) to ensure perfection, and reduces development time. Having a good programming ethic will pay off when you are developing something on a huge scale, because you will avoid stupid petty small errors.

    I'm not saying that BOOL DoSomething() is going to destroy your computer, but sticking to the standard, and writing 'void' where there are no parameters, will encourage you to write correct code ALL the time.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    bool blah::print() const {
      cout << data << endl;
      return true;
    }
    In this example, why not return the value from cout itself, that way the caller can verify that cout actually worked.

    For example:
    Code:
    bool print(char *data, ofstream &os) {
      return os << data << endl;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 11:46 PM
  5. Error, not sure what it means.
    By RealityFusion in forum C++ Programming
    Replies: 1
    Last Post: 09-25-2005, 02:17 PM

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