can you return a void function call?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deanbrown3d@yahoo.com

    can you return a void function call?

    Hi there!

    Suppose I have a function

    void ShowMessage(str ing s);

    (that has a void return type.)


    In my other function F, also with a void return type, can I do this?

    void F(int i)
    {
    // Can I return this?
    return ShowMessage("Hi Dean"); // ????

    }

    I can seem to be able to do this in Borland Builder6, but I didn't
    think it was possible until recently. Is this a compiler-specific
    setting?


    THX!

    Dean

  • Gary Labowitz

    #2
    Re: can you return a void function call?

    <deanbrown3d@ya hoo.com> wrote in message
    news:1109942913 .901777.136520@ l41g2000cwc.goo glegroups.com.. .[color=blue]
    > Hi there!
    >
    > Suppose I have a function
    >
    > void ShowMessage(str ing s);
    >
    > (that has a void return type.)
    >
    >
    > In my other function F, also with a void return type, can I do this?
    >
    > void F(int i)
    > {
    > // Can I return this?
    > return ShowMessage("Hi Dean"); // ????
    >
    > }
    >
    > I can seem to be able to do this in Borland Builder6, but I didn't
    > think it was possible until recently. Is this a compiler-specific
    > setting?[/color]

    No, this doesn't make sense. It is invalid to return a value from a function
    returning void.
    --
    Gary


    Comment

    • deanbrown3d@yahoo.com

      #3
      Re: can you return a void function call?

      Even if the value is void?

      Comment

      • deanbrown3d@yahoo.com

        #4
        Re: can you return a void function call?

        I mean, it seems to make sense to say:

        return ShowMessage("Hi "); // One line

        then to write:

        {
        ShowMessage("Hi "); // Four lines
        return;
        }

        Comment

        • Rolf Magnus

          #5
          Re: can you return a void function call?

          Gary Labowitz wrote:
          [color=blue][color=green]
          >> In my other function F, also with a void return type, can I do this?
          >>
          >> void F(int i)
          >> {
          >> // Can I return this?
          >> return ShowMessage("Hi Dean"); // ????
          >>
          >> }
          >>
          >> I can seem to be able to do this in Borland Builder6, but I didn't
          >> think it was possible until recently. Is this a compiler-specific
          >> setting?[/color]
          >
          > No, this doesn't make sense. It is invalid to return a value from a
          > function returning void.[/color]

          Nonsense! It is perfectly valid to do that, as long as the 'value' is of
          type void:

          void a()
          {
          }

          void b()
          {
          return a();
          }

          The reason for this being allowed are templates. Think e.g. of the standard
          library's mem_fun, which might use something like this (copied from
          TC++PL):

          template <class R, class T> class mem_fun_t : public unary_function< T*, R> {
          R(T::*pmf)();
          public:
          explicit mem_fun_t(R(T:: *p)()) : pmf(p) {}
          R operator()(T* p) const { return (p->*pmf)(); }
          };

          Now if you have a member function returning void:

          class X
          {
          public:
          void foo() {};
          };

          int main()
          {
          std::mem_fun_t< void, X> fun = &X::foo;
          fun(&X);
          }

          for 'fun', the operator becomes:

          void operator()(X* p) const { return (p->*pmf)(); }

          And (p->*pmf) becomes a function returning void.

          Comment

          • Matthias Kaeppler

            #6
            Re: can you return a void function call?

            deanbrown3d@yah oo.com wrote:[color=blue]
            > Even if the value is void?
            >[/color]

            void is not a value, void is nothing, so it doesn't make sense to return
            anything. If you want to return something, then you'll have to use the
            right signature for your function.
            Invoking 'return' without passing a value just means that you are about
            to leave the scope of the function.

            --
            Matthias Kaeppler

            Comment

            • deanbrown3d@yahoo.com

              #7
              Re: can you return a void function call?

              Wow and there's the reason! Thanks Rolf, makes sense. It must have
              always been that way, I just didn't notice.

              Dean

              Comment

              • Gary Labowitz

                #8
                Re: can you return a void function call?

                "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                news:d09s1o$21v $05$1@news.t-online.com...[color=blue]
                > Gary Labowitz wrote:
                >[color=green][color=darkred]
                > >> In my other function F, also with a void return type, can I do this?
                > >>
                > >> void F(int i)
                > >> {
                > >> // Can I return this?
                > >> return ShowMessage("Hi Dean"); // ????
                > >>
                > >> }
                > >>
                > >> I can seem to be able to do this in Borland Builder6, but I didn't
                > >> think it was possible until recently. Is this a compiler-specific
                > >> setting?[/color]
                > >
                > > No, this doesn't make sense. It is invalid to return a value from a
                > > function returning void.[/color]
                >
                > Nonsense! It is perfectly valid to do that, as long as the 'value' is of
                > type void:
                >
                > void a()
                > {
                > }
                >
                > void b()
                > {
                > return a();
                > }[/color]

                Interesting.
                If you coded b()'s return as
                return void; //compiler error
                But with the call to a() returning nothing, it looks like the 'nothing' is
                the temporary value as a result of evaluating a(). On the other hand, the
                compiler may be optimizing out the call to a( ) since a is empty. But no, if
                there is code in the a( ) body it executes and so does the return.
                void is surely tricky business, and the reason to allow void as a 'return'
                from a function call makes sense.
                There would appear to be no other way to set a temporary to void, I should
                think, but if there is I'd like to see it.
                --
                Gary


                Comment

                • Howard

                  #9
                  Re: can you return a void function call?


                  "Gary Labowitz" <glabowitz@comc ast.net> wrote in message
                  news:zpKdnSREvc _yE7XfRVn-pg@comcast.com. ..[color=blue]
                  >
                  > Interesting.
                  > If you coded b()'s return as
                  > return void; //compiler error[/color]

                  That's an error because "void" is a type, and you don't return types, you
                  return objects. It's like writing:
                  return int; // compiler error
                  [color=blue]
                  > There would appear to be no other way to set a temporary to void, I should
                  > think, but if there is I'd like to see it.[/color]

                  I can't think of any way, either. I tried using something like
                  DoVoid(DoVoid() ), and even DoVoid((void)(D oVoid())), but that doesn't
                  compile. (CodeWarrior just says the signatures don't match, but doesn't say
                  what it thinks the parameter is, if not a void temporary.)

                  -Howard


                  Comment

                  • Rolf Magnus

                    #10
                    Re: can you return a void function call?

                    Howard wrote:
                    [color=blue]
                    >
                    > "Gary Labowitz" <glabowitz@comc ast.net> wrote in message
                    > news:zpKdnSREvc _yE7XfRVn-pg@comcast.com. ..[color=green]
                    >>
                    >> Interesting.
                    >> If you coded b()'s return as
                    >> return void; //compiler error[/color]
                    >
                    > That's an error because "void" is a type, and you don't return types, you
                    > return objects. It's like writing:
                    > return int; // compiler error[/color]

                    You can however do:

                    return (void)0;

                    Or (at least on g++):

                    return void();
                    [color=blue][color=green]
                    >> There would appear to be no other way to set a temporary to void, I
                    >> should think, but if there is I'd like to see it.[/color]
                    >
                    > I can't think of any way, either. I tried using something like
                    > DoVoid(DoVoid() ), and even DoVoid((void)(D oVoid())), but that doesn't
                    > compile. (CodeWarrior just says the signatures don't match, but doesn't
                    > say what it thinks the parameter is, if not a void temporary.)[/color]

                    g++ says "too many arguments", since the function expects zero argument, but
                    I provided one. Even though it's of type void, it is nevertheless an
                    argument. It would be a nice thing to have void parameters. This could be
                    useful in templates.

                    Comment

                    • REH

                      #11
                      Re: can you return a void function call?


                      "Howard" <alicebt@hotmai l.com> wrote in message
                      news:Bv0Wd.3243 62$w62.81185@bg tnsc05-news.ops.worldn et.att.net...[color=blue]
                      >
                      > "Gary Labowitz" <glabowitz@comc ast.net> wrote in message
                      > news:zpKdnSREvc _yE7XfRVn-pg@comcast.com. ..[color=green]
                      > >
                      > > Interesting.
                      > > If you coded b()'s return as
                      > > return void; //compiler error[/color]
                      >
                      > That's an error because "void" is a type, and you don't return types, you
                      > return objects. It's like writing:
                      > return int; // compiler error
                      >[color=green]
                      > > There would appear to be no other way to set a temporary to void, I[/color][/color]
                      should[color=blue][color=green]
                      > > think, but if there is I'd like to see it.[/color]
                      >
                      > I can't think of any way, either. I tried using something like
                      > DoVoid(DoVoid() ), and even DoVoid((void)(D oVoid())), but that doesn't
                      > compile. (CodeWarrior just says the signatures don't match, but doesn't[/color]
                      say[color=blue]
                      > what it thinks the parameter is, if not a void temporary.)
                      >
                      > -Howard
                      >[/color]

                      Well, you can do this:

                      void foo()
                      {
                      return (void) 0;
                      }





                      Comment

                      • DHOLLINGSWORTH2

                        #12
                        Re: can you return a void function call?

                        if hte function is void, then return void, no other data, datatypes.

                        Some times you have void functions that have multiple exit points. And in
                        turn several return statements.

                        return; // looks like this






                        <deanbrown3d@ya hoo.com> wrote in message
                        news:1109942913 .901777.136520@ l41g2000cwc.goo glegroups.com.. .[color=blue]
                        > Hi there!
                        >
                        > Suppose I have a function
                        >
                        > void ShowMessage(str ing s);
                        >
                        > (that has a void return type.)
                        >
                        >
                        > In my other function F, also with a void return type, can I do this?
                        >
                        > void F(int i)
                        > {
                        > // Can I return this?
                        > return ShowMessage("Hi Dean"); // ????
                        >
                        > }
                        >
                        > I can seem to be able to do this in Borland Builder6, but I didn't
                        > think it was possible until recently. Is this a compiler-specific
                        > setting?
                        >
                        >
                        > THX!
                        >
                        > Dean
                        >[/color]


                        Comment

                        • deanbrown3d@yahoo.com

                          #13
                          Re: can you return a void function call?

                          DHOLLINGSWORTH2 the point is that 4 lines:

                          {
                          showmessage("Hi ");
                          return;
                          }

                          can be replace by one line:

                          return ShowMessage(... .)

                          Comment

                          • Old Wolf

                            #14
                            Re: can you return a void function call?

                            Gary Labowitz wrote:[color=blue]
                            > "Rolf Magnus" <ramagnus@t-online.de> wrote:
                            >[color=green]
                            > > void a()
                            > > {
                            > > }
                            > >
                            > > void b()
                            > > {
                            > > return a();
                            > > }[/color]
                            >
                            > But with the call to a() returning nothing, it looks like the
                            > 'nothing' is the temporary value as a result of evaluating a().
                            > There would appear to be no other way to set a temporary to
                            > void, I should think, but if there is I'd like to see it.[/color]

                            What temporary? The function 'a' doesn't return a value at all.
                            'b' is exactly equivalent to:
                            void b() { a(); }

                            Comment

                            • Gary Labowitz

                              #15
                              Re: can you return a void function call?

                              "Old Wolf" <oldwolf@inspir e.net.nz> wrote in message
                              news:1110152593 .848759.301930@ g14g2000cwa.goo glegroups.com.. .[color=blue]
                              > Gary Labowitz wrote:[color=green]
                              > > "Rolf Magnus" <ramagnus@t-online.de> wrote:
                              > >[color=darkred]
                              > > > void a()
                              > > > {
                              > > > }
                              > > >
                              > > > void b()
                              > > > {
                              > > > return a();
                              > > > }[/color]
                              > >
                              > > But with the call to a() returning nothing, it looks like the
                              > > 'nothing' is the temporary value as a result of evaluating a().
                              > > There would appear to be no other way to set a temporary to
                              > > void, I should think, but if there is I'd like to see it.[/color]
                              >
                              > What temporary? The function 'a' doesn't return a value at all.
                              > 'b' is exactly equivalent to:
                              > void b() { a(); }[/color]

                              I see. I guess I would rather think of it as
                              void b( ) { return;}
                              or even
                              void b( ) { a( ); return;}
                              Doesn't b( ) always need a return statement? (I know, my g++ accepts it
                              without, but I like to code it. Go figure.)
                              --
                              Gary


                              Comment

                              Working...