void func () returning value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yevgen Muntyan

    #1

    void func () returning value?

    Hey,

    Consider the following code:

    void func (void)
    {
    }

    void func2 (void)
    {
    return func ();
    }

    gcc -pedantic says the following:

    muntyan@munt10:/tmp$ gcc -pedantic -c file.c
    file.c: In function ‘func2’:
    file.c:7: warning: ‘return’ with a value, in function returning void

    If I recall correctly, compiler in some Microsoft Visual Studio version
    told me the same thing. So, is this a compiler bug (feature), or is this
    "return func ();" invalid in standard C?

    Thanks,
    Yevgen
  • Harald van Dijk

    #2
    Re: void func () returning value?

    Yevgen Muntyan wrote:
    Hey,
    >
    Consider the following code:
    >
    void func (void)
    {
    }
    >
    void func2 (void)
    {
    return func ();
    }
    >
    gcc -pedantic says the following:
    >
    muntyan@munt10:/tmp$ gcc -pedantic -c file.c
    file.c: In function ‘func2’:
    file.c:7: warning: ‘return’ with a value, in function returning void
    >
    If I recall correctly, compiler in some Microsoft Visual Studio version
    told me the same thing. So, is this a compiler bug (feature), or is this
    "return func ();" invalid in standard C?
    It's invalid in standard C. Perhaps you're thinking of C++, where it is
    allowed?

    Comment

    • Nick Keighley

      #3
      Re: void func () returning value?

      Harald van Dijk wrote:
      Yevgen Muntyan wrote:
      Consider the following code:

      void func (void)
      {
      }

      void func2 (void)
      {
      return func ();
      }

      gcc -pedantic says the following:

      muntyan@munt10:/tmp$ gcc -pedantic -c file.c
      file.c: In function ‘func2’:
      file.c:7: warning: ‘return’ with a value, in function returning void

      If I recall correctly, compiler in some Microsoft Visual Studio version
      told me the same thing. So, is this a compiler bug (feature), or is this
      "return func ();" invalid in standard C?
      >
      It's invalid in standard C. Perhaps you're thinking of C++, where it is
      allowed?
      since when has it been valid for a void function to return a value, in
      either C or C++?


      --
      Nick Keighley

      Comment

      • Greg

        #4
        Re: void func () returning value?


        Nick Keighley wrote:
        Harald van Dijk wrote:
        Yevgen Muntyan wrote:
        >
        Consider the following code:
        >
        void func (void)
        {
        }
        >
        void func2 (void)
        {
        return func ();
        }
        >
        gcc -pedantic says the following:
        >
        muntyan@munt10:/tmp$ gcc -pedantic -c file.c
        file.c: In function ‘func2’:
        file.c:7: warning: ‘return’ with a value, in functionreturni ng void
        >
        If I recall correctly, compiler in some Microsoft Visual Studio version
        told me the same thing. So, is this a compiler bug (feature), or is this
        "return func ();" invalid in standard C?
        It's invalid in standard C. Perhaps you're thinking of C++, where it is
        allowed?
        >
        since when has it been valid for a void function to return a value, in
        either C or C++?
        It's not legal in C++ for a "return" statement of a void function to
        return a value [§6.6.3]. The above program is nonetheless OK since it
        is legal in C++ for a return statement in a void function to have a
        (cv) void type expression - as in this case, the return expression is
        simply a call to another void function.

        Greg

        Comment

        • Risto Lankinen

          #5
          Re: void func () returning value?


          "Nick Keighley" <nick_keighley_ nospam@hotmail. comwrote in message
          news:1161157294 .584254.312760@ h48g2000cwc.goo glegroups.com.. .
          >since when has it been valid for a void function to return a value, in
          >either C or C++?
          A void function can "return" an expression whose type is void . This
          is to facilitate templates such as...

          template <typename TT func() { return static_cast<T>( expr); }

          .... to be instantiable also for T=void.

          - Risto -

          --
          Nick Keighley


          Comment

          • Yevgen Muntyan

            #6
            Re: void func () returning value?

            Harald van Dijk wrote:
            Yevgen Muntyan wrote:
            >
            >>Hey,
            >>
            >>Consider the following code:
            >>
            >>void func (void)
            >>{
            >>}
            >>
            >>void func2 (void)
            >>{
            > return func ();
            >>}
            >>
            >>gcc -pedantic says the following:
            >>
            >>muntyan@munt1 0:/tmp$ gcc -pedantic -c file.c
            >>file.c: In function ‘func2’:
            >>file.c:7: warning: ‘return’ with a value, in function returning void
            >>
            >>If I recall correctly, compiler in some Microsoft Visual Studio version
            >>told me the same thing. So, is this a compiler bug (feature), or is this
            >>"return func ();" invalid in standard C?
            >
            >
            It's invalid in standard C. Perhaps you're thinking of C++, where it is
            allowed?
            I am sure I was thinking about C :)
            After comparing C and C++ standards it looks like it's a useful C++
            feature which wasn't backported to C standard. I thought it was
            natural to do 'return func()' for func() returning nothing, but
            now I'm curious if there are other languages where it's valid
            (I could swear it's so in one of languages I know and where function
            can really return nothing, but it's not the case).

            Best regards,
            Yevgen

            Comment

            • mark_bluemel@pobox.com

              #7
              Re: void func () returning value?


              Yevgen Muntyan wrote:
              After comparing C and C++ standards it looks like it's a useful C++
              feature which wasn't backported to C standard.
              For some value of "useful"...

              BTW: are you implying that C is, or should be, a subset of C++?
              That would, I suspect, be "fighting talk" :-)
              I thought it was
              natural to do 'return func()' for func() returning nothing,
              I thought it was unnatural - funny how people's interpretation of
              "natural" varies...

              Comment

              • Yevgen Muntyan

                #8
                Re: void func () returning value?

                mark_bluemel@po box.com wrote:
                Yevgen Muntyan wrote:
                >
                >
                >>After comparing C and C++ standards it looks like it's a useful C++
                >>feature which wasn't backported to C standard.
                >
                >
                For some value of "useful"...
                void foo (void)
                {
                ...
                for (i = 0; i < 42; ++i)
                if (bar (i))
                return simple_foo (i);
                real_foo_here() ;
                }

                Using two statements (function call and return) makes it lot
                uglier for me - bunch of curly brackets added. Not a real problem,
                of course, but I find "return void_func();" very nice.
                BTW: are you implying that C is, or should be, a subset of C++?
                That would, I suspect, be "fighting talk" :-)
                No, of course no, I think that C++ is a superset of C ;)
                Seriously speaking, // comments are a (useful, I think) C++
                feature "backported " to C, aren't they? I absolutely didn't mean
                "C sucks because C++...." or "C++ is so much nicer because..."

                Best regards,
                Yevgen

                Comment

                • mark_bluemel@pobox.com

                  #9
                  Re: void func () returning value?


                  Yevgen Muntyan wrote:
                  void foo (void)
                  {
                  ...
                  for (i = 0; i < 42; ++i)
                  if (bar (i))
                  return simple_foo (i);
                  real_foo_here() ;
                  }
                  >
                  Using two statements (function call and return) makes it lot
                  uglier for me - bunch of curly brackets added. Not a real problem,
                  of course, but I find "return void_func();" very nice.
                  As a predominantly maintenance programmer, I would immediately have
                  changed your code to

                  void foo (void)
                  {
                  ...
                  for (i = 0; i < 42; ++i) {
                  if (bar (i)) {
                  return simple_foo (i);
                  }
                  }
                  real_foo_here() ;
                  }

                  for starters, so splitting function call and return into two lines is
                  not a big overhead. Leaving braces out is a recipe for future bugs
                  IMHO....

                  I stick to my opinion that "void foo()" means that "foo()" doesn't
                  return anything, so combining an arbitrary call to a void function with
                  the return is fairly stupid. If you wanted to call something and ignore
                  its result would you do (for example) 'return (void)printf("M y Dog Has
                  Fleas\n");' ?

                  As for "//" for comments, I'm rather inclined to prefer "/* ... */" but
                  that's a personal thing, I guess

                  Comment

                  • Fred Kleinschmidt

                    #10
                    Re: void func () returning value?


                    <mark_bluemel@p obox.comwrote in message
                    news:1161178776 .881285.207080@ m7g2000cwm.goog legroups.com...
                    >
                    Yevgen Muntyan wrote:
                    >
                    >void foo (void)
                    >{
                    > ...
                    > for (i = 0; i < 42; ++i)
                    > if (bar (i))
                    > return simple_foo (i);
                    > real_foo_here() ;
                    >}
                    >>
                    >Using two statements (function call and return) makes it lot
                    >uglier for me - bunch of curly brackets added. Not a real problem,
                    >of course, but I find "return void_func();" very nice.
                    >
                    As a predominantly maintenance programmer, I would immediately have
                    changed your code to
                    >
                    void foo (void)
                    {
                    ...
                    for (i = 0; i < 42; ++i) {
                    if (bar (i)) {
                    return simple_foo (i);
                    }
                    }
                    real_foo_here() ;
                    }
                    >
                    for starters, so splitting function call and return into two lines is
                    not a big overhead. Leaving braces out is a recipe for future bugs
                    IMHO....
                    >
                    I stick to my opinion that "void foo()" means that "foo()" doesn't
                    return anything, so combining an arbitrary call to a void function with
                    the return is fairly stupid. If you wanted to call something and ignore
                    its result would you do (for example) 'return (void)printf("M y Dog Has
                    Fleas\n");' ?
                    >
                    As for "//" for comments, I'm rather inclined to prefer "/* ... */" but
                    that's a personal thing, I guess
                    >
                    I would have changed it to:

                    void foo (void)
                    {
                    ...
                    for (i = 0; i < 42; ++i) {
                    if (bar (i)) {
                    simple_foo (i);
                    return;
                    }
                    }
                    real_foo_here() ;
                    }

                    Think what would happen if simple_foo() werre changed
                    to return a value (an error code, of perhaps something else
                    that foo() is not interested in). If you instead used
                    return simple_foo(i)
                    then suddenly the program would not compile.

                    Now, this may or may not be a reasonable thing.
                    The compile error would certainly warn you that
                    perhaps you should pay attention to the return value
                    of the modified simple_foo() function.
                    --
                    Fred L. Kleinschmidt
                    Boeing Associate Technical Fellow
                    Technical Architect, Software Reuse Project



                    Comment

                    • Al Balmer

                      #11
                      Re: void func () returning value?

                      On Wed, 18 Oct 2006 09:43:18 GMT, Yevgen Muntyan
                      <muntyan.remove this@tamu.eduwr ote:
                      >Harald van D?k wrote:
                      >Yevgen Muntyan wrote:
                      >>
                      >>>Hey,
                      >>>
                      >>>Consider the following code:
                      >>>
                      >>>void func (void)
                      >>>{
                      >>>}
                      >>>
                      >>>void func2 (void)
                      >>>{
                      >> return func ();
                      >>>}
                      >>>
                      >>>gcc -pedantic says the following:
                      >>>
                      >>>muntyan@munt 10:/tmp$ gcc -pedantic -c file.c
                      >>>file.c: In function ‘func2’:
                      >>>file.c:7: warning: ‘return’ with a value, in function returning void
                      >>>
                      >>>If I recall correctly, compiler in some Microsoft Visual Studio version
                      >>>told me the same thing. So, is this a compiler bug (feature), or is this
                      >>>"return func ();" invalid in standard C?
                      >>
                      >>
                      >It's invalid in standard C. Perhaps you're thinking of C++, where it is
                      >allowed?
                      >
                      >I am sure I was thinking about C :)
                      >After comparing C and C++ standards it looks like it's a useful C++
                      >feature which wasn't backported to C standard.
                      In C, it's a trivial transformation to

                      func();
                      return;

                      What's useful about it? (Aside from possible use in obfuscation.)
                      I thought it was
                      >natural to do 'return func()' for func() returning nothing, but
                      >now I'm curious if there are other languages where it's valid
                      >(I could swear it's so in one of languages I know and where function
                      >can really return nothing, but it's not the case).
                      >
                      >Best regards,
                      >Yevgen
                      --
                      Al Balmer
                      Sun City, AZ

                      Comment

                      • Richard

                        #12
                        Re: void func () returning value?

                        "Harald van Dijk" <truedfx@gmail. comwrites:
                        Yevgen Muntyan wrote:
                        >Hey,
                        >>
                        >Consider the following code:
                        >>
                        >void func (void)
                        >{
                        >}
                        >>
                        >void func2 (void)
                        >{
                        > return func ();
                        >}
                        >>
                        >gcc -pedantic says the following:
                        >>
                        >muntyan@munt10 :/tmp$ gcc -pedantic -c file.c
                        >file.c: In function ‘func2’:
                        >file.c:7: warning: ‘return’ with a value, in function returning void
                        >>
                        >If I recall correctly, compiler in some Microsoft Visual Studio version
                        >told me the same thing. So, is this a compiler bug (feature), or is this
                        >"return func ();" invalid in standard C?
                        >
                        It's invalid in standard C. Perhaps you're thinking of C++, where it is
                        allowed?
                        >
                        It is also bad practice IMO.

                        When reading the code you would immediately assume that func()
                        is returning a required value.

                        I have also found in years of modifying large legacy code bases that
                        "return func()" is not as easy to monitor and debug as "rc= func();
                        return rc;". Admittedly a personal preference built up from my own
                        experiences.

                        Comment

                        • Elijah Cardon

                          #13
                          Re: void func () returning value?


                          "Richard" <rgrdev@gmail.c omwrote in message
                          news:87ac3tab01 .fsf@gmail.com. ..
                          "Harald van D?k" <truedfx@gmail. comwrites:
                          >
                          >Yevgen Muntyan wrote:
                          >>Hey,
                          >>>
                          >>Consider the following code:
                          >>>
                          >>void func (void)
                          >>{
                          >>}
                          >>>
                          >>void func2 (void)
                          >>{
                          >> return func ();
                          >>}
                          >>>
                          >>gcc -pedantic says the following:
                          >>>
                          >>muntyan@munt1 0:/tmp$ gcc -pedantic -c file.c
                          >>file.c: In function 'func2':
                          >>file.c:7: warning: 'return' with a value, in function returning void
                          >>>
                          >>If I recall correctly, compiler in some Microsoft Visual Studio version
                          >>told me the same thing. So, is this a compiler bug (feature), or is this
                          >>"return func ();" invalid in standard C?
                          >>
                          >It's invalid in standard C. Perhaps you're thinking of C++, where it is
                          >allowed?
                          >>
                          >
                          It is also bad practice IMO.
                          >
                          When reading the code you would immediately assume that func()
                          is returning a required value.
                          >
                          I have also found in years of modifying large legacy code bases that
                          "return func()" is not as easy to monitor and debug as "rc= func();
                          return rc;". Admittedly a personal preference built up from my own
                          experiences.
                          Why do people TRY to find bad logic scenarios with the C Programming
                          Language. You have two void functions and try to return a call to the first
                          in the second. If a compiler didn't scream, I would. EC


                          Comment

                          • Yevgen Muntyan

                            #14
                            Re: void func () returning value?

                            Fred Kleinschmidt wrote:
                            <mark_bluemel@p obox.comwrote in message
                            news:1161178776 .881285.207080@ m7g2000cwm.goog legroups.com...
                            >
                            >>Yevgen Muntyan wrote:
                            >>
                            >>
                            >>>void foo (void)
                            >>>{
                            >> ...
                            >> for (i = 0; i < 42; ++i)
                            >> if (bar (i))
                            >> return simple_foo (i);
                            >> real_foo_here() ;
                            >>>}
                            >>>
                            >>>Using two statements (function call and return) makes it lot
                            >>>uglier for me - bunch of curly brackets added. Not a real problem,
                            >>>of course, but I find "return void_func();" very nice.
                            >>
                            >>As a predominantly maintenance programmer, I would immediately have
                            >>changed your code to
                            >>
                            >>void foo (void)
                            >>{
                            > ...
                            > for (i = 0; i < 42; ++i) {
                            > if (bar (i)) {
                            > return simple_foo (i);
                            > }
                            > }
                            > real_foo_here() ;
                            >>}
                            >>
                            >>for starters, so splitting function call and return into two lines is
                            >>not a big overhead. Leaving braces out is a recipe for future bugs
                            >>IMHO....
                            >>
                            >>I stick to my opinion that "void foo()" means that "foo()" doesn't
                            >>return anything, so combining an arbitrary call to a void function with
                            >>the return is fairly stupid. If you wanted to call something and ignore
                            >>its result would you do (for example) 'return (void)printf("M y Dog Has
                            >>Fleas\n");' ?
                            >>
                            >>As for "//" for comments, I'm rather inclined to prefer "/* ... */" but
                            >>that's a personal thing, I guess
                            >>
                            >
                            >
                            I would have changed it to:
                            >
                            void foo (void)
                            {
                            ...
                            for (i = 0; i < 42; ++i) {
                            if (bar (i)) {
                            simple_foo (i);
                            return;
                            }
                            }
                            real_foo_here() ;
                            }
                            >
                            Think what would happen if simple_foo() werre changed
                            to return a value (an error code, of perhaps something else
                            that foo() is not interested in). If you instead used
                            return simple_foo(i)
                            then suddenly the program would not compile.
                            Yes, exactly. Compiler will complain if either foo or simple_foo
                            change return value from void to something. And it is a really
                            good thing.

                            Regards,
                            Yevgen

                            Comment

                            • Yevgen Muntyan

                              #15
                              Re: void func () returning value?

                              mark_bluemel@po box.com wrote:
                              Yevgen Muntyan wrote:
                              >
                              >
                              >>void foo (void)
                              >>{
                              > ...
                              > for (i = 0; i < 42; ++i)
                              > if (bar (i))
                              > return simple_foo (i);
                              > real_foo_here() ;
                              >>}
                              >>
                              >>Using two statements (function call and return) makes it lot
                              >>uglier for me - bunch of curly brackets added. Not a real problem,
                              >>of course, but I find "return void_func();" very nice.
                              >
                              >
                              As a predominantly maintenance programmer, I would immediately have
                              changed your code to
                              >
                              void foo (void)
                              {
                              ...
                              for (i = 0; i < 42; ++i) {
                              if (bar (i)) {
                              return simple_foo (i);
                              }
                              }
                              real_foo_here() ;
                              }
                              >
                              for starters, so splitting function call and return into two lines is
                              not a big overhead. Leaving braces out is a recipe for future bugs
                              IMHO....
                              >
                              If I got this piece of code (with split return statement), I would
                              change it to:

                              void foo (void)
                              {
                              ...
                              for (i = 0; i < 42; ++i)
                              {
                              if (bar (i))
                              {
                              simple_foo ();
                              return;
                              }
                              }
                              real_foo_here() ;
                              }

                              And *here* brackets are no good if you can do without them at all.
                              A nitpick/rationale: your coding style forces you to always use brackets
                              since you can't clearly see if there is an opening bracket, so closing
                              bracket alone is easily lost, and "Leaving braces out is a recipe for
                              future bugs". In mine, I can easily *see* that I need to add brackets if
                              I suddenly add a statement. Hope it will not start yet another flame
                              war on coding style ;)
                              I stick to my opinion that "void foo()" means that "foo()" doesn't
                              return anything, so combining an arbitrary call to a void function with
                              the return is fairly stupid. If you wanted to call something and ignore
                              its result would you do (for example) 'return (void)printf("M y Dog Has
                              Fleas\n");' ?
                              No, I wouldn't. Because this cast hides return value, so it's dangerous.
                              In case with void-void compiler will warn me if some function changes
                              return type, so I'm safe.
                              >
                              As for "//" for comments, I'm rather inclined to prefer "/* ... */" but
                              that's a personal thing, I guess
                              Sure it is.

                              Best regards,
                              Yevgen

                              Comment

                              Working...