if I define a function with no parameters, should i put void?

Discussion in 'C Programming' started by TTroy, Feb 17, 2005.

  1. TTroy

    TTroy Guest

    For function definitions (not declarations/prototypes), is it necessary
    to put void in the emptry braces if the function is to receive no
    parameters? Does this turn any error checking off or cause any other
    effects?
    -thanks
     
    TTroy, Feb 17, 2005
    #1
    1. Advertising

  2. TTroy wrote:
    > For function definitions (not declarations/prototypes), is it necessary
    > to put void in the emptry braces if the function is to receive no
    > parameters? Does this turn any error checking off or cause any other
    > effects?


    No. In a function _definition_ empty '()' immediately means "no parameters".

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 17, 2005
    #2
    1. Advertising

  3. Andrey Tarasevich wrote:
    > TTroy wrote:
    >
    >>For function definitions (not declarations/prototypes), is it necessary
    >>to put void in the emptry braces if the function is to receive no
    >>parameters? Does this turn any error checking off or cause any other
    >>effects?

    >
    >
    > No. In a function _definition_ empty '()' immediately means "no parameters".
    >


    <Style Opinion>
    I personally believe that placing "void" inside the parens
    makes the code more readable. But that is my opinion.
    </Style Opinion>

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Feb 17, 2005
    #3
  4. TTroy

    TTroy Guest

    Andrey Tarasevich wrote:
    > TTroy wrote:
    > > For function definitions (not declarations/prototypes), is it

    necessary
    > > to put void in the emptry braces if the function is to receive no
    > > parameters? Does this turn any error checking off or cause any

    other
    > > effects?

    >
    > No. In a function _definition_ empty '()' immediately means "no

    parameters".
    >


    Even if I use the empty () definition of a function as the only
    declaration?

    I also have another question. If I was calling a function and also
    casting values of arguments, does this cast come BEFORE or AFTER the
    automatic conversion caused by the prototype? I guess the question
    applies when there is no prototype - does the case happen before or
    after the promotions?
     
    TTroy, Feb 17, 2005
    #4
  5. TTroy

    Eric Sosman Guest

    TTroy wrote:
    > Andrey Tarasevich wrote:
    >
    >>TTroy wrote:
    >>
    >>>For function definitions (not declarations/prototypes), is it

    >
    > necessary
    >
    >>>to put void in the emptry braces if the function is to receive no
    >>>parameters? Does this turn any error checking off or cause any

    >
    > other
    >
    >>>effects?

    >>
    >>No. In a function _definition_ empty '()' immediately means "no

    >
    > parameters".
    >
    >
    > Even if I use the empty () definition of a function as the only
    > declaration?


    Let's get specific: If you write

    void f(void) { ... }
    void g(void) { f(42); }

    .... the compiler is required to issue a diagnostic for the
    mismatch between the number of arguments in the call and the
    number of parameters in the prototype. But if you write

    void f() { ... }
    void g(void) { f(42); }

    .... the compiler is not required to catch the mistake.

    > I also have another question. If I was calling a function and also
    > casting values of arguments, does this cast come BEFORE or AFTER the
    > automatic conversion caused by the prototype? I guess the question
    > applies when there is no prototype - does the case happen before or
    > after the promotions?


    Before. The cast operator is part of the expression
    that calculates the value you provide as an argument, just
    like other operators (+, -, ...) that might appear in the
    expression. The expression yields a value of some type,
    and conversions or promotions occur depending on what that
    type is and on what the compiler knows about the function.
    The cast has already been applied before this happens.

    --
     
    Eric Sosman, Feb 17, 2005
    #5
  6. TTroy

    Luke Wu Guest

    TTroy wrote:
    > Andrey Tarasevich wrote:
    > > TTroy wrote:
    > > > For function definitions (not declarations/prototypes), is it

    > necessary
    > > > to put void in the emptry braces if the function is to receive no
    > > > parameters? Does this turn any error checking off or cause any

    > other
    > > > effects?

    > >
    > > No. In a function _definition_ empty '()' immediately means "no

    > parameters".
    > >

    >
    > Even if I use the empty () definition of a function as the only
    > declaration?
    >


    empty () in declarations tell the compiler to assume nothing about
    parameters (turns parameter checking off)

    >
    > I also have another question. If I was calling a function and also
    > casting values of arguments, does this cast come BEFORE or AFTER the
    > automatic conversion caused by the prototype? I guess the question
    > applies when there is no prototype - does the case happen before or
    > after the promotions?


    Before.

    For example, back in the days when there were no prototypes, and people
    wanted to send intergers to functions that accepted only floats, they
    would do this:

    int i = 7;
    func((float)i);

    These days casting is never required because prototypes do the same
    thing, but casting is still used when "weird" conversions might cause
    the compiler to complain.
     
    Luke Wu, Feb 17, 2005
    #6
  7. TTroy

    Jens Marder Guest

    In ANSI-C that's fine (no void needed)

    But in old Standard-C (K&R) code func() ment, that
    0..many parameters are possible (NOT checked).

    So if you don't come across OLD K&R-style C-code, you
    NEEDN'T use func(void), but i guess it doesn't hurt, either.


    "TTroy" <> schrieb im Newsbeitrag
    news:...
    > For function definitions (not declarations/prototypes), is it necessary
    > to put void in the emptry braces if the function is to receive no
    > parameters? Does this turn any error checking off or cause any other
    > effects?
    > -thanks
    >
     
    Jens Marder, Feb 17, 2005
    #7
  8. TTroy wrote:
    >> ...
    >> No. In a function _definition_ empty '()' immediately means "no

    > parameters".
    >>

    >
    > Even if I use the empty () definition of a function as the only
    > declaration?


    Yes.

    > I also have another question. If I was calling a function and also
    > casting values of arguments, does this cast come BEFORE or AFTER the
    > automatic conversion caused by the prototype? I guess the question
    > applies when there is no prototype - does the case happen before or
    > after the promotions?


    Explicitly specified cast precedes any implicit conversions.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 17, 2005
    #8
  9. Luke Wu wrote:
    >> > ...
    >> > No. In a function _definition_ empty '()' immediately means "no

    >> parameters".
    >> >

    >>
    >> Even if I use the empty () definition of a function as the only
    >> declaration?
    >>

    >
    > empty () in declarations tell the compiler to assume nothing about
    > parameters (turns parameter checking off)
    > ...


    Once again, unless this function declaration also happens to be a
    definition. In that case '()' explicitly means "no parameters".

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Feb 17, 2005
    #9
  10. TTroy

    Flash Gordon Guest

    Jens Marder wrote:

    Please don't top post. You reply belongs after or intermixed with the
    text you are replying to.

    Top posting fixed.

    > "TTroy" <> schrieb im Newsbeitrag
    > news:...
    >
    >>For function definitions (not declarations/prototypes), is it necessary
    >>to put void in the emptry braces if the function is to receive no
    >>parameters? Does this turn any error checking off or cause any other
    >>effects?
    >>-thanks

    >
    > In ANSI-C that's fine (no void needed)


    You obviously don't know pre-ANSI C, since it was ANSI C that
    *introduced* void to the language.

    > But in old Standard-C (K&R) code func() ment, that
    > 0..many parameters are possible (NOT checked).


    It means the same in *all* versions of C.

    > So if you don't come across OLD K&R-style C-code, you
    > NEEDN'T use func(void), but i guess it doesn't hurt, either.


    If you use old K&R style C then you *can't* use void because it did not
    exist then.

    With ANSI/ISO C (as others have stated) you need to specify void if you
    want the compiler to be required to diagnose parameters being passed to
    a function that does not accept parameters.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Feb 18, 2005
    #10
  11. TTroy

    Jack Klein Guest

    On 17 Feb 2005 12:34:48 -0800, "Luke Wu" <>
    wrote in comp.lang.c:

    >
    > TTroy wrote:
    > > Andrey Tarasevich wrote:
    > > > TTroy wrote:
    > > > > For function definitions (not declarations/prototypes), is it

    > > necessary
    > > > > to put void in the emptry braces if the function is to receive no
    > > > > parameters? Does this turn any error checking off or cause any

    > > other
    > > > > effects?
    > > >
    > > > No. In a function _definition_ empty '()' immediately means "no

    > > parameters".
    > > >

    > >
    > > Even if I use the empty () definition of a function as the only
    > > declaration?
    > >

    >
    > empty () in declarations tell the compiler to assume nothing about
    > parameters (turns parameter checking off)
    >
    > >
    > > I also have another question. If I was calling a function and also
    > > casting values of arguments, does this cast come BEFORE or AFTER the
    > > automatic conversion caused by the prototype? I guess the question
    > > applies when there is no prototype - does the case happen before or
    > > after the promotions?

    >
    > Before.
    >
    > For example, back in the days when there were no prototypes, and people
    > wanted to send intergers to functions that accepted only floats, they
    > would do this:
    >
    > int i = 7;
    > func((float)i);
    >
    > These days casting is never required because prototypes do the same
    > thing, but casting is still used when "weird" conversions might cause
    > the compiler to complain.


    No, prototypes don't always do the same thing:

    #include <stdio.h>

    int main(void)
    {
    printf("%d", 1);
    return 0;
    }

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 18, 2005
    #11
  12. Andrey Tarasevich wrote:
    > Luke Wu wrote:
    > > empty () in declarations tell the compiler to assume nothing
    > > about parameters (turns parameter checking off)
    > > ...

    >
    > Once again, unless this function declaration also happens to be a
    > definition. In that case '()' explicitly means "no parameters".


    An empty declaration, even if part of a function definition, is
    still not a prototype...

    void foo()
    {
    foo(42); /* UB, but no diagnostic is required */
    }

    void baa(void)
    {
    baa(42); /* constraint violation */
    }

    --
    Peter
     
    Peter Nilsson, Feb 18, 2005
    #12
  13. On Thu, 17 Feb 2005 12:01:42 -0800, TTroy wrote:

    >
    > Andrey Tarasevich wrote:
    >> TTroy wrote:
    >> > For function definitions (not declarations/prototypes), is it

    > necessary
    >> > to put void in the emptry braces if the function is to receive no
    >> > parameters? Does this turn any error checking off or cause any

    > other
    >> > effects?

    >>
    >> No. In a function _definition_ empty '()' immediately means "no

    > parameters".
    >>

    >
    > Even if I use the empty () definition of a function as the only
    > declaration?


    If you define a function with no paramaters then it has no parameters, how
    else could it be? However if you don't specify the parameter list as void
    (i.e. make it a prototype) you reduce the amount of error checking that
    the compiler is required to do. So specifying void is a good thing.

    > I also have another question. If I was calling a function and also
    > casting values of arguments, does this cast come BEFORE or AFTER the
    > automatic conversion caused by the prototype? I guess the question
    > applies when there is no prototype - does the case happen before or
    > after the promotions?


    The promotions happen after the argument expression has been evaluated
    and any casts are part of the argument expression.

    Lawrence
     
    Lawrence Kirby, Feb 18, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    579
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    826
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    861
    S.Tobias
    Jul 22, 2005
  4. Jason
    Replies:
    2
    Views:
    534
    Jonathan Mcdougall
    May 13, 2006
  5. Replies:
    1
    Views:
    427
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page