By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,533 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

can you return a void function call?

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
<de*********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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
Jul 23 '05 #2

P: n/a
Even if the value is void?

Jul 23 '05 #3

P: n/a
I mean, it seems to make sense to say:

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

then to write:

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

Jul 23 '05 #4

P: n/a
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.

Jul 23 '05 #5

P: n/a
de*********@yahoo.com 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
Jul 23 '05 #6

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

Dean

Jul 23 '05 #7

P: n/a
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:d0*************@news.t-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
Jul 23 '05 #8

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:zp********************@comcast.com...

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
Jul 23 '05 #9

P: n/a
Howard wrote:

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:zp********************@comcast.com...

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.

Jul 23 '05 #10

P: n/a
REH

"Howard" <al*****@hotmail.com> wrote in message
news:Bv********************@bgtnsc05-news.ops.worldnet.att.net...

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:zp********************@comcast.com...

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

Jul 23 '05 #11

P: n/a
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


<de*********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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

Jul 23 '05 #12

P: n/a
DHOLLINGSWORTH2 the point is that 4 lines:

{
showmessage("Hi");
return;
}

can be replace by one line:

return ShowMessage(....)

Jul 23 '05 #13

P: n/a
Gary Labowitz wrote:
"Rolf Magnus" <ra******@t-online.de> 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(); }

Jul 23 '05 #14

P: n/a
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Gary Labowitz wrote:
"Rolf Magnus" <ra******@t-online.de> 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
Jul 23 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.