473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

can you return a void function call?

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
14 15624
<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
Even if the value is void?

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

"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
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
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
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
DHOLLINGSWORTH2 the point is that 4 lines:

{
showmessage("Hi");
return;
}

can be replace by one line:

return ShowMessage(....)

Jul 23 '05 #13
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Busin | last post by:
Classes B, C, D and E are derived from base class A. A* CreateB(); A* CreateC(); A* CreateD(); A* CreateE(); class X { public:
12
by: David Sobey | last post by:
Hi everyone I'm having huge difficulties overriding a virtual function with a function that returns a covariant type. I'd be grateful if someone could show me some code where this is done: - A...
4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
7
by: ballpointpenthief | last post by:
Hello, I'm thinking that it would be a good idea to start using the return code for main() to return the amount of memory still allocated (which would hopefully be zero) to ensure all memory has...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
7
by: asit | last post by:
#include <stdio.h> //#include <stdlib.h> int main() { int c; printf("c before call=%d\n",c); c=message(); printf("c after call=%d\n",c); return 0;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.