What use is void?

Discussion in 'C Programming' started by Ian Tuomi, Sep 19, 2003.

  1. Ian Tuomi

    Ian Tuomi Guest

    Hello, I was wondering, does it make any difference if you write

    void foo(int x)
    {
    /* insert code here */
    }

    or

    foo(int x)
    {
    /* insert code here */
    }

    What is the difference?? Both ways the function wont return anything,
    right? What is the point in writing void? Is it merely a cosmetic feature?

    --
    Ian Tuomi
    Jyväskylä, Finland

    GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
    !O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!
     
    Ian Tuomi, Sep 19, 2003
    #1
    1. Advertising

  2. Ian Tuomi

    Guest Guest

    Ian Tuomi wrote

    > Hello, I was wondering, does it make any difference if you write
    >
    > void foo(int x)
    > {
    > /* insert code here */
    > }
    >
    > or
    >
    > foo(int x)
    > {
    > /* insert code here */
    > }
    >
    > What is the difference?? Both ways the function wont return anything,
    > right? What is the point in writing void? Is it merely a cosmetic feature?


    Absolutely not. If you omit a return type from a function declaration, the
    return type defaults to `int', so

    foo(int x)

    is actually

    int foo(int x)

    The void type is used as the type returned by functions that generate no
    value.


    ---
    user
     
    Guest, Sep 19, 2003
    #2
    1. Advertising

  3. Ian Tuomi

    -berlin.de Guest

    Ian Tuomi <> wrote:
    > Hello, I was wondering, does it make any difference if you write


    > void foo(int x)
    > {
    > /* insert code here */
    > }


    > or


    > foo(int x)
    > {
    > /* insert code here */
    > }


    > What is the difference?? Both ways the function wont return anything,
    > right? What is the point in writing void? Is it merely a cosmetic feature?


    No, at least with the C89 standard a function with no specified
    return value (like your second example) is supposed to return an
    integer. You need void to explicitely tell the compiler that the
    function isn't returning a value. The new (C99) standard changed
    this, but not in the sense that a function without a specified
    return value returns nothing (aka void), but making it a syntax
    error, so you still need void.
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
     
    -berlin.de, Sep 19, 2003
    #3
  4. Ian Tuomi

    Ian Tuomi Guest

    Ian Tuomi wrote:

    > Hello, I was wondering, does it make any difference if you write
    >
    > void foo(int x)
    > {
    > /* insert code here */
    > }
    >
    > or
    >
    > foo(int x)
    > {
    > /* insert code here */
    > }
    >
    > What is the difference?? Both ways the function wont return anything,
    > right? What is the point in writing void? Is it merely a cosmetic feature?
    >



    --
    Ian Tuomi
    Jyväskylä, Finland

    "Very funny scotty, now beam down my clothes."

    GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
    !O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!
     
    Ian Tuomi, Sep 19, 2003
    #4
  5. Ian Tuomi

    Ed Morton Guest

    On 9/19/2003 12:50 PM, Ian Tuomi wrote:
    > Hello, I was wondering, does it make any difference if you write
    >
    > void foo(int x)
    > {
    > /* insert code here */
    > }
    >
    > or
    >
    > foo(int x)
    > {
    > /* insert code here */
    > }
    >
    > What is the difference??


    The first one returns nothing while the second returns an int.

    Both ways the function wont return anything,
    > right?


    No. If you put a "return 1;" inside both functions, the first one won't compile
    whereas the second will compile with just a warning about the return type
    defaulting to int.

    What is the point in writing void? Is it merely a cosmetic feature?
    >


    No. It tells the compiler (and future developers!) that this function must not
    return anything, so if someone wrongly adds a return(value) statement or wrongly
    assgines a void functions return to a variable, they get a compilation error or
    warning.

    Ed.
     
    Ed Morton, Sep 19, 2003
    #5
  6. Ian Tuomi

    Ian Pilcher Guest

    Ian Tuomi wrote:
    >
    > foo(int x)
    > {
    > /* insert code here */
    > }
    >


    I'm not entirely sure that this is even a valid function declaration.
    If it is, the compiler will assume that foo returns an int.

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Sep 19, 2003
    #6
  7. Ian Tuomi <> wrote:

    >Hello, I was wondering, does it make any difference if you write
    >
    >void foo(int x)
    >{
    >/* insert code here */
    >}
    >
    >or
    >
    >foo(int x)
    >{
    >/* insert code here */
    >}
    >
    >What is the difference?? Both ways the function wont return anything,
    >right? What is the point in writing void? Is it merely a cosmetic feature?


    The first example declares a function returning nothing at all, whereas
    in the second example the function defaults to return int (at least in
    K&R-C and C89). Your compiler will most probably issue a diagnostic if
    you fail to supply a return statement in the second example.

    Furthermore you need void when you want to declare a generic pointer,
    IOW a pointer of which is not known what type of object it points to.
    E.g. the malloc function returns a void*.

    Regards

    Irrwahn
    --
    The generation of random numbers is too important to be left to chance.
     
    Irrwahn Grausewitz, Sep 19, 2003
    #7
  8. Ian Tuomi

    Mike Wahler Guest

    "Ian Tuomi" <> wrote in message
    news:bkffm1$e8p$...
    > Hello, I was wondering, does it make any difference if you write
    >
    > void foo(int x)
    > {
    > /* insert code here */
    > }
    >
    > or
    >


    /* in C99, insert required return type here */

    > foo(int x)
    > {
    > /* insert code here */


    /* insert required return statement here */

    > }


    In C99, this is invalid. A function *must* declare
    a return type. In C89, the omission of a return
    type from a function declaration causes the return
    type to default to 'int'. So in C89 this is also
    invalid, since the function is declared to return
    type 'int' but does not return a value.

    >
    > What is the difference??


    See above.

    >Both ways the function wont return anything,


    > right?


    Wrong. What C book(s) are you reading?

    > What is the point in writing void?


    It specifies that a function does not return a value.
    This is the *only* way to specify no return value.

    >Is it merely a cosmetic feature?


    Absolutely not.

    -Mike
     
    Mike Wahler, Sep 19, 2003
    #8
  9. On Fri, 19 Sep 2003, Mike Wahler wrote:

    > > foo(int x)
    > > {
    > > /* insert code here */

    >
    > /* insert required return statement here */
    >
    > > }

    >
    > In C99, this is invalid. A function *must* declare
    > a return type. In C89, the omission of a return
    > type from a function declaration causes the return
    > type to default to 'int'. So in C89 this is also
    > invalid, since the function is declared to return
    > type 'int' but does not return a value.


    Someone correct me if I'm wrong, but IIRC it's OK to fall through
    without returning as long as you don't check the return value.
     
    Jarno A Wuolijoki, Sep 19, 2003
    #9
  10. Ian Tuomi

    Xenos Guest

    "Jarno A Wuolijoki" <> wrote in message
    news:p...
    > On Fri, 19 Sep 2003, Mike Wahler wrote:
    >
    > > > foo(int x)
    > > > {
    > > > /* insert code here */

    > >
    > > /* insert required return statement here */
    > >
    > > > }

    > >
    > > In C99, this is invalid. A function *must* declare
    > > a return type. In C89, the omission of a return
    > > type from a function declaration causes the return
    > > type to default to 'int'. So in C89 this is also
    > > invalid, since the function is declared to return
    > > type 'int' but does not return a value.

    >
    > Someone correct me if I'm wrong, but IIRC it's OK to fall through
    > without returning as long as you don't check the return value.
    >


    But what would be the point? Either you are returning a value your not
    checking, or one that has a random--thus useless--value.
     
    Xenos, Sep 19, 2003
    #10
  11. "Ian Tuomi" <> wrote in message
    news:bkffm1$e8p$...

    Well, now you know the language rules after reading
    all those posts :)

    Anyways...

    Just one more "reason". Even if it wasn't so that functions
    did default to int it is nice to have void for symmetry and it
    makes it easier to write simple parsing tools so they can use
    simpler heuristics.

    IMO it was a mistake to make functions default to int,
    albeit a small one. More people seem to agree as it is now
    a deprecated feature.

    As mentioned elsethread it is also used to declare generic
    pointers. This is perhaps strange (I think so at least). Consider
    that a pointer to int can point to objects of type int. You can
    in fact have objects of type int so that is ok and intuitive.
    When you have a pointer to void you have pointer than can
    point to objects of type void. But you cannot have objects
    of type void! This is not a big problem though and I find it
    an adequate solution over having a separate keyword or
    type for this, like gptr_t or something like 'object *p;'.

    --
    Thomas
     
    Thomas Stegen, Sep 19, 2003
    #11
  12. Ian Tuomi

    Dan Pop Guest

    In <> Jarno A Wuolijoki <> writes:

    >On Fri, 19 Sep 2003, Mike Wahler wrote:
    >
    >> > foo(int x)
    >> > {
    >> > /* insert code here */

    >>
    >> /* insert required return statement here */
    >>
    >> > }

    >>
    >> In C99, this is invalid. A function *must* declare
    >> a return type. In C89, the omission of a return
    >> type from a function declaration causes the return
    >> type to default to 'int'. So in C89 this is also
    >> invalid, since the function is declared to return
    >> type 'int' but does not return a value.

    >
    >Someone correct me if I'm wrong, but IIRC it's OK to fall through
    >without returning as long as you don't check the return value.


    Right. Back in the days of K&R C, when there were no void functions,
    some people used the following convention:

    int foo() { ... return something; }

    for functions that were supposed to return something and

    bar() { ... }

    for functions that were not supposed to return anything (the equivalent
    of void functions in standard C).

    But from the language point of view, there is no difference between the
    two function definitions: both are defined as taking no parameters and
    returning int. To avoid breaking such code, C89 allowed non-void
    functions that don't return anything, as long as the caller is not
    attempting to use the return value.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 19, 2003
    #12
  13. On Fri, 19 Sep 2003 20:50:55 +0300
    Ian Tuomi <> wrote:
    > Hello, I was wondering, does it make any difference if you write
    >
    > void foo(int x)
    > {
    > /* insert code here */
    > }


    Legal. Returning from this function will choke your compiler.

    > foo(int x)
    > {
    > /* insert code here */
    > }


    Illegal in C99, defaults to int in C89. Not returning here may cause undefined
    behaviour.

    > What is the difference?? Both ways the function wont return anything,
    > right? What is the point in writing void? Is it merely a cosmetic feature?


    Blah Blah Blah, it's been said in this thread more than once.

    Another important example of a use for void lies in prototypes:

    int blog ();
    Is a prototype of a function returning an int, with unspecified function
    arguments.

    int glob (void);
    A prototype for a function returning an int, with no function arguments.

    --
    char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
    c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
    c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
     
    Pieter Droogendijk, Sep 19, 2003
    #13
  14. On Fri, 19 Sep 2003, Xenos wrote:
    >
    > "Jarno A Wuolijoki" <> wrote...
    > > On Fri, 19 Sep 2003, Mike Wahler wrote:
    > > >
    > > > > foo(int x)
    > > > > {
    > > > > /* insert code here */
    > > > > }
    > > >
    > > > In C99, this is invalid. A function *must* declare
    > > > a return type. In C89, the omission of a return
    > > > type from a function declaration causes the return
    > > > type to default to 'int'. So in C89 this is also
    > > > invalid, since the function is declared to return
    > > > type 'int' but does not return a value.

    > >
    > > Someone correct me if I'm wrong, but IIRC it's OK to fall through
    > > without returning as long as you don't check the return value.


    Or not even fall through -- you can insert a blank

    return;

    in the function for the same effect (immediate return to caller,
    undefined return value).

    > But what would be the point? Either you are returning a value your not
    > checking, or one that has a random--thus useless--value.



    int silly_sprintf(char *buffer, const char *fmt, ...)
    {
    if (buffer == NULL) {
    how_many_characters = some_function_of_(input);
    return how_many_characters;
    }
    else {
    strcpy(buffer, some_other_function_of_(input);
    return;
    }
    }


    The idea being that in *some* cases, maybe you want to return
    useful information, and in *some* cases, there's no useful
    information to return. Obviously, this is a bad example;
    a call to silly_sprintf(non_null, "foo") could return an
    integer too. But I'm sure such cases could exist.

    In such cases, the Prime Directive of Misplaced Efficiency Tuning
    dictates that we use 'return;' in place of 'return 0;', to save
    the single "load" instruction. :)


    More practically, in modern C you can even have a function
    that returns a structure directly:

    struct foo return_a_foo(int should_i)
    {
    if (should_i)
    return (struct foo){0, 1, should_i, "and so on", 42, "infinity"};
    else
    return;
    }

    The time saved in not constructing the return value might
    really add up, in this case!

    HTH,
    -Arthur
     
    Arthur J. O'Dwyer, Sep 19, 2003
    #14
  15. Ian Tuomi

    Mike Wahler Guest

    "Jarno A Wuolijoki" <> wrote in message
    news:p...
    > On Fri, 19 Sep 2003, Mike Wahler wrote:
    >
    > > > foo(int x)
    > > > {
    > > > /* insert code here */

    > >
    > > /* insert required return statement here */
    > >
    > > > }

    > >
    > > In C99, this is invalid. A function *must* declare
    > > a return type. In C89, the omission of a return
    > > type from a function declaration causes the return
    > > type to default to 'int'. So in C89 this is also
    > > invalid, since the function is declared to return
    > > type 'int' but does not return a value.

    >
    > Someone correct me if I'm wrong,


    You're wrong.

    > but IIRC


    Recall from where?

    >it's OK to fall through
    > without returning as long as you don't check the return value.


    Not true. Where did you hear that? A function *always*
    returns at the closing brace ( '}' ), regardless of whether
    or not it returns a value. This is a fundamental property
    of functions. There's no such thing as 'fall through'.
    Whether or not the caller inspects or stores the return value
    is irrelevant.

    If you heard what you're telling us from a book, burn it.
    If from an individual, shoot him. :)

    -Mike
     
    Mike Wahler, Sep 19, 2003
    #15
  16. Ian Tuomi

    Mike Wahler Guest

    "Thomas Stegen" <> wrote in message
    news:...
    >
    > "Ian Tuomi" <> wrote in message
    > news:bkffm1$e8p$...
    >
    > Well, now you know the language rules after reading
    > all those posts :)
    >
    > Anyways...
    >
    > Just one more "reason". Even if it wasn't so that functions
    > did default to int it is nice to have void for symmetry and it
    > makes it easier to write simple parsing tools so they can use
    > simpler heuristics.
    >
    > IMO it was a mistake to make functions default to int,
    > albeit a small one. More people seem to agree as it is now
    > a deprecated feature.


    Not deprecated, but disallowed (by 9899:1999)
    9899:1989 is no longer the C standard.

    -Mike
     
    Mike Wahler, Sep 19, 2003
    #16
  17. Ian Tuomi

    Mike Wahler Guest

    "Ian Pilcher" <> wrote in message
    news:_XHab.524304$uu5.87448@sccrnsc04...
    > Ian Tuomi wrote:
    > >
    > > foo(int x)
    > > {
    > > /* insert code here */
    > > }
    > >

    >
    > I'm not entirely sure that this is even a valid function declaration.


    It's valid for C89, but not C99.

    > If it is, the compiler will assume that foo returns an int.


    A C89 compiler, yes. A C99 compiler that doesn't diagnose
    it is broken.

    -Mike
     
    Mike Wahler, Sep 19, 2003
    #17
  18. Pieter Droogendijk <> wrote:

    >On Fri, 19 Sep 2003 20:50:55 +0300
    >Ian Tuomi <> wrote:
    >> Hello, I was wondering, does it make any difference if you write
    >>
    >> void foo(int x)
    >> {
    >> /* insert code here */
    >> }

    >
    >Legal. Returning from this function will choke your compiler.


    Huh, returning from a void function does harm to your compiler???
    Well, (in C99) a return statement /with an expression/ shall not
    appear in a function whose return type is void, but a return statement
    without an expression shall only appear in a function whose return type
    is void.

    This implies that

    void foo(int x)
    {
    /* insert code here */
    return;
    }

    is fine.

    <SNIP>

    Regards

    Irrwahn
    --
    The generation of random numbers is too important to be left to chance.
     
    Irrwahn Grausewitz, Sep 19, 2003
    #18
  19. Ian Tuomi

    Jack Klein Guest

    On Fri, 19 Sep 2003 20:16:53 GMT, "Mike Wahler"
    <> wrote in comp.lang.c:

    >
    > "Thomas Stegen" <> wrote in message
    > news:...
    > >
    > > "Ian Tuomi" <> wrote in message
    > > news:bkffm1$e8p$...
    > >
    > > Well, now you know the language rules after reading
    > > all those posts :)
    > >
    > > Anyways...
    > >
    > > Just one more "reason". Even if it wasn't so that functions
    > > did default to int it is nice to have void for symmetry and it
    > > makes it easier to write simple parsing tools so they can use
    > > simpler heuristics.
    > >
    > > IMO it was a mistake to make functions default to int,
    > > albeit a small one. More people seem to agree as it is now
    > > a deprecated feature.

    >
    > Not deprecated, but disallowed (by 9899:1999)
    > 9899:1989 is no longer the C standard.


    ....and never was, but 9899:1990 was.

    --
    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++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Sep 20, 2003
    #19
  20. On Fri, 19 Sep 2003, Mike Wahler wrote:

    > > Someone correct me if I'm wrong,

    > You're wrong.


    Be more specific.


    > > but IIRC

    > Recall from where?


    Probably this very group or comp.std.c couple of months ago.


    > >it's OK to fall through
    > > without returning as long as you don't check the return value.


    Oh, sorry. Translation:

    ...it's OK to return implicitly by letting the execution reach the
    last closing brace of the function rather than with explicit return
    statement with a proper value even for non-void functions as long
    as you don't check the return value.


    > Not true. Where did you hear that? A function *always*
    > returns at the closing brace ( '}' ), regardless of whether
    > or not it returns a value. This is a fundamental property
    > of functions. There's no such thing as 'fall through'.


    Duh. I was talking about /return/ing rather than returning ;)


    > Whether or not the caller inspects or stores the return value
    > is irrelevant.


    "If the } that terminates a function is reached, and the value of the
    function call is used by the caller, the behavior is undefined."


    > If you heard what you're telling us from a book, burn it.


    Luckily I have only a draft. Otherwise this would get on my wallet.


    > If from an individual, shoot him. :)


    Can I quote that in court?
     
    Jarno A Wuolijoki, Sep 20, 2003
    #20
    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. Sergio

    returning a void (*)(void)

    Sergio, Jan 5, 2005, in forum: C++
    Replies:
    6
    Views:
    438
    Jonathan Turkanis
    Jan 5, 2005
  2. Ollej Reemt
    Replies:
    7
    Views:
    543
    Jack Klein
    Apr 22, 2005
  3. Stig Brautaset

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

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    791
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    842
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    412
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page