'void' in function argument

This is a discussion on 'void' in function argument within the C Programming forums, part of the General Programming Boards category; I'm used to C++ and I just started to use C and I'm not sure if it is neccessary or ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    266

    'void' in function argument

    I'm used to C++ and I just started to use C and I'm not sure if it is neccessary or just recommended to add 'void' in a empty parameter list in a function.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    When you are defining a function, the void parameter is unnecessary. If you are declaring a function (prototype), the void parameter means that the function takes no arguments. If you leave out the void, then the number of arguments that the function can take is left unspecified.
    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

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Is that bad?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Well, if you intend a function to have no parameters, why not declare its prototype stating your intention explicitly?
    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

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Alright. Thanks for the reply.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    When you are defining a function, the void parameter is unnecessary. If you are declaring a function (prototype), the void parameter means that the function takes no arguments. If you leave out the void, then the number of arguments that the function can take is left unspecified.
    Is that true for C++ also?
    I'm pretty sure func() means func( void ) in C++, but I don't have the standard memorized...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Is that true for C++ also?
    I'm pretty sure func() means func( void ) in C++, but I don't have the standard memorized...
    No, in C++, a function with no arguments or using void is the same thing. C++ functions that take variable number of arguments must use "..." notation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's a difference between C and C++ (C++ accepts the void but it doesn't work the other way round - no void still means no arguments).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    No, in C++, a function with no arguments or using void is the same thing. C++ functions that take variable number of arguments must use "..." notation.

    --
    Mats
    Good. At least I got that much right.

  10. #10
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    I almost always declare a function with no arguments as func( void ) as well as I always write return( something ). (with brackets)

    Call me old fashioned, but imho it's a question of continuity and clearness.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by EOP View Post
    I almost always declare a function with no arguments as func( void ) as well as I always write return( something ). (with brackets)

    Call me old fashioned, but imho it's a question of continuity and clearness.
    But return is a keyword, not a function. Why make it look like a function?

    The only time I use parentheses around the return value is if I'm returning the result of an expression, like:
    Code:
    return (a < b);

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But it is not needed there either...

    Another thing, do you use brackets for the case statement too?
    Code:
    switch (n) {
        case (FIRST_OPTION)
    ...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Quote Originally Posted by anon View Post
    But it is not needed there either...

    Another thing, do you use brackets for the case statement too?
    Code:
    switch (n) {
        case (FIRST_OPTION)
    ...
    Caught.
    Probably I'm just used to it from my first encounter with C in the 80s.

    And I'm a fan of brackets as (imho) they make code much more readable if you just scan it.

  14. #14
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I use ( ) for the return statement because it resembles a function to me.

    I don't use ( ) for case because case looks like a label to me.

    Just a personal preference unless someone can explain why it is bad.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Just a personal preference unless someone can explain why it is bad.
    As you say, it is personal (or sometimes corporate) preference. On one hand, putting in the parentheses where they are not necessary clutters up the code. On the other hand, leaving them out could result in ambiguity to a reader who is not as familiar with precedence. With that in mind, I leave out the parentheses for return statements where precedence does not come into play.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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