can you return a void function call?

D

deanbrown3d

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
 
G

Gary Labowitz

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

deanbrown3d

I mean, it seems to make sense to say:

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

then to write:

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

Rolf Magnus

Gary said:
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.
 
M

Matthias Kaeppler

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

deanbrown3d

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

Dean
 
G

Gary Labowitz

Rolf Magnus said:
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.
 
H

Howard

Gary Labowitz said:
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
 
R

Rolf Magnus

Howard said:
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();
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.
 
R

REH

Howard said:
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


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

DHOLLINGSWORTH2

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
 
D

deanbrown3d

DHOLLINGSWORTH2 the point is that 4 lines:

{
showmessage("Hi");
return;
}

can be replace by one line:

return ShowMessage(....)
 
O

Old Wolf

Gary said:
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(); }
 
G

Gary Labowitz

Old Wolf said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top