473,804 Members | 3,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 23 '05 #1
14 15666
<de*********@ya hoo.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
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?


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*********@yah oo.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*******@comc ast.net> wrote in message
news:zp******** ************@co mcast.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)(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
Jul 23 '05 #9
Howard wrote:

"Gary Labowitz" <gl*******@comc ast.net> wrote in message
news:zp******** ************@co mcast.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)(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.)


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2300
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
2078
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 class BaseClass is declared with a function me() that takes no arguments and returns itself, or a pointer or reference to itself. - A derived class DerivedClass that override me() that takes no arguments and returns itself, a pointer or reference...
4
157996
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
2003
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 see how the return value comes to play here. Ex
23
3619
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
3616
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 exit(n)) statement provide both function call and return features of the C programming language? /* headers omitted */ int main (void)
7
2003
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 been freed. What would be the best way to do this? Is this kind of thing common practice? I would guess that perhaps a global long variable would be used, and malloc (free) would add (subtract) to this variable.
18
4069
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
2791
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
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.