473,734 Members | 6,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return values for void functions

REH
The other day I did something like this:

void foo
{
}

void bar()
{
return foo();
}

when I meant to do this:

void foo
{
}

void bar()
{
foo();
return;
}

I was suprised that it worked. Is this legal?

Thanks.

Jul 23 '05 #1
23 1925
REH wrote:
The other day I did something like this:

void foo
{
}

void bar()
{
return foo();
}

when I meant to do this:

void foo
{
}

void bar()
{
foo();
return;
}

I was suprised that it worked. Is this legal?


Yes.

V
Jul 23 '05 #2
That is important because if you have a series if if statments. You
can get out of the function when your condition is met. There is also
void func(int &a, int &b, char &c){}

Jul 23 '05 #3
REH wrote:
The other day I did something like this:

void foo
{
}

void bar()
{
return foo();
}

when I meant to do this:

void foo
{
}

void bar()
{
foo();
return;
}

I was suprised that it worked. Is this legal?

Thanks.


Yes it is, and the fact that it is lets you cover the case of a function
returning void with code like:

template < typename xResult >
xResult foo(xResult (*fFunc)())
{
return (*fFunc)();
}

instead of making you write a special case that handls functions that
return void, for example:

void foo(void (*fFunc)())
{
*fFunc();
}

Having to write that second case to cover a return type of void would be
a major pain.
Jul 23 '05 #4
REH

"enki" <en*****@yahoo. com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
That is important because if you have a series if if statments. You
can get out of the function when your condition is met. There is also
void func(int &a, int &b, char &c){}


I'm not sure what your point is.

Jul 23 '05 #5
REH wrote:
The other day I did something like this: cat foo.cc void foo { }

void bar(void) {
return foo();
}
g++ -Wall -ansi -pedantic -c foo.cc foo.cc:1: error: invalid function declaration
foo.cc: In function `void bar()':
foo.cc:4: error: `foo' undeclared (first use this function)
foo.cc:4: error: (Each undeclared identifier \
is reported only once for each function it appears in.)
foo.cc:4: error: return-statement with a value, \
in function returning 'void'

You probably meant:
cat foo.cc void foo(void) { }

void bar(void) {
return foo();
}
g++ -Wall -ansi -pedantic -c foo.cc
nm foo.o 00000006 T _Z3barv
00000000 T _Z3foov c++filt _Z3barv bar() c++filt _Z3foov foo()
when I meant to do this:

void foo(void) { }

void bar(void) {
foo();
return;
}

I was suprised that it worked.
I'm surprised too. My compiler doesn't like it.
Are you "sure" that
you actually compiled the code that you posted?
Is this legal?


You probably shouldn't ask a question like this
in the comp.lang.c++ newsgroup.
Some of our subscribers know a lot about the standards
but the real experts are the compiler developers.
Submit your code to your compiler with options set
for standard error checking and all warnings
and it will tell you whether your code is legal or not.
If you don't trust your compiler, submit your code to
Greg Comeau's free online compiler at

http://www.comeaucomputing.com/tryitout/

The compiler is a *much* more reliable judge of legal C++ code
than "some guy" who subscribes to the comp.lang.c++ newsgroup.
Jul 23 '05 #6

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:ct******** **@nntp1.jpl.na sa.gov...
REH wrote:
The compiler is a *much* more reliable judge of legal C++ code
than "some guy" who subscribes to the comp.lang.c++ newsgroup.


I think that's a pretty bad assessment. How can someone know if their
compiler is any good (compliant), if they don't themselves know what should
be correct? This newsgroup has some folks who are pretty darn good at
interpreting the standard doc's and presenting the OP with good responses.
Who would you trust, your VC++ 6.0 compiler, or one of the regulars here?
I'd listen to the regulars (and the FAQ), myself.

-some guy
Jul 23 '05 #7
In article <ct*********@cu i1.lmms.lmco.co m>, REH <bo***@nowhere. net> wrote:
The other day I did something like this:

void foo
{
}

void bar()
{
return foo();
}

when I meant to do this:

void foo
{
}

void bar()
{
foo();
return;
}

I was suprised that it worked. Is this legal?


Notwithstanding that your foo above is defined incorrectly,
yes, it's legal. Section 6.6.3p3 of Standard C++ reads:
"A return statement with an expression of type "cv void"
can be used only in functions with a return type of cv void;
the expression is evaluated just before the function returns
to its caller."

In case that's not clear 3.9.3p5 reads:
"In this International Standard, the notation cv (or cv1, cv2, etc.),
used in the description of types, represents an arbitrary set of
cv-qualifiers, i.e., one of {const}, {volatile}, {const, volatile},
or the empty set."

As you mentioned, despite the legality, it may not necessarily
be what you wanted, or it may be (this becomes more transparent
with templates).
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #8
Some guy wrote:
E. Robert Tisdale wrote:
The compiler is a *much* more reliable judge of legal C++ code
than "some guy" who subscribes to the comp.lang.c++ newsgroup.
I think that's a pretty bad assessment.
How can someone know if their compiler is any good (compliant)
if they don't themselves know what should be correct?


First of all, there are *no* C++ compiler which comply fully
with the ANSI/ISO standards. The Comeau compilers come very close.
It is practically impossible for programmers to determine
for themselves whether any implementation complies or not.
This newsgroup has some folks
who are pretty darn good at interpreting the standard doc's
And many more who are not.
How to you tell the difference.
and presenting the OP with good responses.
Who would you trust, your VC++ 6.0 compiler, or one of the regulars here?
I *don't* trust the "regulars here".
The comp.lang.c++ newsgroup is *not* the most appropriate forum
to discuss the ANSI/ISO C++ standards or whether or not
any particular implementation complies.
The comp.std.c++ newsgroup is more appropriate.
The comp.std.c++ newsgroup attracts real experts
on the ANSI/ISO C++ standards so contributions
are more likely to be properly vetted.
I'd listen to the regulars (and the FAQ), myself.


That's probably bad advice. The first problem is that
we have no way to determine who the "regulars" are
much less whether or not they have any credentials,
training or experience in C++ compiler implementation.
We have no way to determine whether their interpretations
of the ANSI/ISO standards are correct or not.
Jul 23 '05 #9

E. Robert Tisdale wrote:
Some guy wrote:
E. Robert Tisdale wrote:
The compiler is a *much* more reliable judge of legal C++ code
than "some guy" who subscribes to the comp.lang.c++ newsgroup.
I think that's a pretty bad assessment.
How can someone know if their compiler is any good (compliant)
if they don't themselves know what should be correct?


First of all, there are *no* C++ compiler which comply fully
with the ANSI/ISO standards. The Comeau compilers come very close.
It is practically impossible for programmers to determine
for themselves whether any implementation complies or not.


In all honesty, when you get to the point where Comeau is, you
discover that the standard is slightly fuzzy. It's not unusual
for a Comeau user to discover that some obscure case in the
standard cannot be implemented. Just have a look at the Core DR
lists.
I *don't* trust the "regulars here".
The comp.lang.c++ newsgroup is *not* the most appropriate forum
to discuss the ANSI/ISO C++ standards or whether or not
any particular implementation complies.
The comp.std.c++ newsgroup is more appropriate.
The comp.std.c++ newsgroup attracts real experts
on the ANSI/ISO C++ standards so contributions
are more likely to be properly vetted.


Of course, the clc++m group attracts real experts as well. Reason
is simple, moderation keeps the traffic low enough. In fact, such
a trivial question would fit better here or in clc++m than in csc++
I'd listen to the regulars (and the FAQ), myself.


That's probably bad advice. The first problem is that
we have no way to determine who the "regulars" are
much less whether or not they have any credentials,
training or experience in C++ compiler implementation.
We have no way to determine whether their interpretations
of the ANSI/ISO standards are correct or not.


You don't have to implement a compiler to know one is wrong.
In fact, once you gain some experience, you'll learn what's
easy and what's impossible without implementing one. And
for 99% of the questions here, any interpretation backed by
a quote is correct simply because there is no misinterpretati on
possible. The "expert" part is finding the right quote.
Regards,
Michiel Salters

Jul 23 '05 #10

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

Similar topics

66
5016
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
9
2730
by: Ann Huxtable | last post by:
I have the following code segment - which compiles fine. I'm just worried I may get run time probs - because it looks like the functions are being overloaded by the return types?. Is this Ok: ? template <class T1, class T2> int getValue( T1 col, T2 row ) ; template <class T1, class T2> double getValue( T1 col, T2 row ) ;
16
1993
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
15
2806
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
18
2451
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
4
6952
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but I'm defining the functions as void*, and then casting when I use them. This code is going into a general purpose framework, and it would be much nicer if the user didn't need to do any casting. Can someone tell me how to set up those typedefs...
18
4058
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;
2
4540
by: rsk | last post by:
Friends, The following logic just converts the hexadecimal values to the binay values taken into the array. for(i=31;i>=0;i--){ if ((1<<i)&var1) array_var1=1; else array_var1=0;
4
2480
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. Here's the approach I'm currently looking at. I throw exceptions only from constructors. Destructors, of course, do not throw exceptions. All other functions return a signed integer. The values are all stored in one large header file (as...
0
8957
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
8783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9248
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
9193
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6043
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
4555
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
4819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.