Thread: 'void' in function argument

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Well, if you intend a function to have no parameters, why not declare its prototype stating your intention explicitly?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    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
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 02: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