can you return a void function call?

Discussion in 'C++' started by deanbrown3d@yahoo.com, Mar 4, 2005.

  1. Guest

    Hi there!

    Suppose I have a function

    void ShowMessage(string 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
    , Mar 4, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > Hi there!
    >
    > Suppose I have a function
    >
    > void ShowMessage(string 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?


    No, this doesn't make sense. It is invalid to return a value from a function
    returning void.
    --
    Gary
    Gary Labowitz, Mar 4, 2005
    #2
    1. Advertising

  3. Guest

    Even if the value is void?
    , Mar 4, 2005
    #3
  4. Guest

    I mean, it seems to make sense to say:

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

    then to write:

    {
    ShowMessage("Hi"); // Four lines
    return;
    }
    , Mar 4, 2005
    #4
  5. Rolf Magnus Guest

    Gary Labowitz wrote:

    >> 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?

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


    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.
    Rolf Magnus, Mar 4, 2005
    #5
  6. wrote:
    > Even if the value is void?
    >


    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
    Matthias Kaeppler, Mar 4, 2005
    #6
  7. Guest

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

    Dean
    , Mar 4, 2005
    #7
  8. "Rolf Magnus" <> wrote in message
    news:d09s1o$21v$05$-online.com...
    > Gary Labowitz wrote:
    >
    > >> 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?

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

    >
    > Nonsense! It is perfectly valid to do that, as long as the 'value' is of
    > type void:
    >
    > void a()
    > {
    > }
    >
    > void b()
    > {
    > return a();
    > }


    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
    Gary Labowitz, Mar 4, 2005
    #8
  9. Howard Guest

    "Gary Labowitz" <> wrote in message
    news:...
    >
    > Interesting.
    > If you coded b()'s return as
    > return void; //compiler error


    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

    > 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.


    I can't think of any way, either. I tried using something like
    DoVoid(DoVoid()), and even DoVoid((void)(DoVoid())), 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
    Howard, Mar 4, 2005
    #9
  10. Rolf Magnus Guest

    Howard wrote:

    >
    > "Gary Labowitz" <> wrote in message
    > news:...
    >>
    >> Interesting.
    >> If you coded b()'s return as
    >> return void; //compiler error

    >
    > 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


    You can however do:

    return (void)0;

    Or (at least on g++):

    return void();

    >> 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.

    >
    > I can't think of any way, either. I tried using something like
    > DoVoid(DoVoid()), and even DoVoid((void)(DoVoid())), 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.)


    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.
    Rolf Magnus, Mar 4, 2005
    #10
  11. REH Guest

    "Howard" <> wrote in message
    news:Bv0Wd.324362$...
    >
    > "Gary Labowitz" <> wrote in message
    > news:...
    > >
    > > Interesting.
    > > If you coded b()'s return as
    > > return void; //compiler error

    >
    > 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
    >
    > > 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.

    >
    > I can't think of any way, either. I tried using something like
    > DoVoid(DoVoid()), and even DoVoid((void)(DoVoid())), 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
    >


    Well, you can do this:

    void foo()
    {
    return (void) 0;
    }
    REH, Mar 4, 2005
    #11
  12. 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






    <> wrote in message
    news:...
    > Hi there!
    >
    > Suppose I have a function
    >
    > void ShowMessage(string 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
    >
    DHOLLINGSWORTH2, Mar 5, 2005
    #12
  13. Guest

    DHOLLINGSWORTH2 the point is that 4 lines:

    {
    showmessage("Hi");
    return;
    }

    can be replace by one line:

    return ShowMessage(....)
    , Mar 6, 2005
    #13
  14. Old Wolf Guest

    Gary Labowitz wrote:
    > "Rolf Magnus" <> wrote:
    >
    > > void a()
    > > {
    > > }
    > >
    > > void b()
    > > {
    > > return a();
    > > }

    >
    > 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.


    What temporary? The function 'a' doesn't return a value at all.
    'b' is exactly equivalent to:
    void b() { a(); }
    Old Wolf, Mar 6, 2005
    #14
  15. "Old Wolf" <> wrote in message
    news:...
    > Gary Labowitz wrote:
    > > "Rolf Magnus" <> wrote:
    > >
    > > > void a()
    > > > {
    > > > }
    > > >
    > > > void b()
    > > > {
    > > > return a();
    > > > }

    > >
    > > 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.

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


    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
    Gary Labowitz, Mar 7, 2005
    #15
    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:
    499
    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:
    775
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  4. Abhishek
    Replies:
    12
    Views:
    787
    Eric Sosman
    Jan 30, 2006
  5. Replies:
    1
    Views:
    392
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page