Void...

This is a discussion on Void... within the C++ Programming forums, part of the General Programming Boards category; Hi every1 I was wondering, when im reading the tutorials, i come accross th function "void" alot, can anyone explain ...

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321

    Void...

    Hi every1
    I was wondering, when im reading the tutorials, i come accross th function "void" alot, can anyone explain to me what this function is and does?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    it's a specifier for a function that doesn't return a value.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    it's a specifier for a function that doesn't return a value.
    And that's why it needs to use return;, because it can't return anything...
    Last edited by maxorator; 10-05-2006 at 10:35 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    would it need to if it isn't doing any sort of recursion? I mean you can have a void function that doesn't use return;

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    recursion or not, id imagine it would need it. the computer wouldnt 'know' that the function is recursive.

    i thought every function needed a return statement no matter what, except for 'int main' where it is implied.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Um, how does recursion factor into this? The language itself makes the return statement optional for functions that return void.
    Last edited by laserlight; 10-05-2006 at 10:56 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    *sigh*


    void is not a function. void is a type specifier, but a special one. void is the non-type. A pointer to void is one that points to an address without further type qualification. A function returning void returns nothing. In C, a function with only void in the parameter list takes no parameters. A variable cannot be of type void, because it would always be empty, which wouldn't make sense.

    By the way, a function returning void does not need a return statement, except in some decades old, very weird compilers. Unless you want an early return.

    laserlight, you forgot to modify result in the recursive branch. You need
    result *= n;
    after the recursive call.
    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

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Hi every1
    I was wondering, when im reading the tutorials, i come accross th function "void" alot, can anyone explain to me what this function is and does?
    It makes me worry about your learning process.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Sorry about that, I was not thinking properly (never ever tried writing recursive functions that return void, heh). After catching my mistake when I actually went to test it as something looked wrong to me, here's my fix:
    Code:
    void factorial(unsigned int& result, unsigned int n) {
        if (n == 0) {
            result = 1;
        } else {
            factorial(result, n - 1);
            result *= n;
        }
    }
    Last edited by laserlight; 10-05-2006 at 11:09 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by siavoshkc
    It makes me worry about your learning process.
    So you worry about his learning process because he hasn't learned everything yet?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    So you worry about his learning process because he hasn't learned everything yet?
    No, I think by reading a tutorial (Valid One) correctly, he/she should not think void is function. If so, something is wrong.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    Old C programmers never die, they simply get cast into a 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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