473,836 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using "return" with parantheses?

Hi,

I'm a game developer programming mostly in C and ASM for about 7 years.

Today at work a colleague (a C++ programmer) yelled at me I'm a bad C
programmer because I use "return(0); " instead of "return 0;".
He explained that "return" is not a function but a stament, like I
didn't know already. The other colleagues also argreed with him :(.

Can someone please explain what's so wrong about using "return" with
parantheses? I've used them like that from the beginning.

Thank you,
Mike Machuidel

Nov 13 '05
32 8899
Fao, Sean <en**********@y ahoo.comi-want-no-spam> scribbled the following:
"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:bn******** **@oravannahka. helsinki.fi...
Your printf statement has too few parantheses. It's much more legible
as:

(printf)(("%d\n "), (a));
You missed one too ;-) ((printf)(("%d\ n"), (a)));


Good catch!

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr
Nov 13 '05 #21

"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:bn******** **@oravannahka. helsinki.fi...
Fao, Sean <en**********@y ahoo.comi-want-no-spam> scribbled the following:
"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:bn******** **@oravannahka. helsinki.fi...
Your printf statement has too few parantheses. It's much more legible
as:

(printf)(("%d\n "), (a));

You missed one too ;-)

((printf)(("%d\ n"), (a)));


Good catch!


Wow you're fast! I just sent that like 10 seconds ago!

Nov 13 '05 #22
Fao, Sean wrote:
You missed one too ;-)

((printf)(("%d\ n"), (a)));


Uhm. One more level of parenthesis and your C code could be compiled by
a lisp compiler :-)

Nov 13 '05 #23
In <qchmb.9047$9E1 .40254@attbi_s5 2> "Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> writes:

The syntax of if, for, and while, to name a few, C statements, requires
parenthesis and I don't think it makes them look like functions.


That's because something else is *usually* following the right
parenthesis. And even when there is nothing else, many people prefer
to write the semicolon on a separate line.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #24
In <pa************ *************** *@and.org> James Antill <ja***********@ and.org> writes:
As for typing "return (0);" being typoed "reutrn (0);" and not being
caught, then..

1. Every viewer/editor[1] I use for C has syntax highlighting that show the
return as obviously wrong.
2. I've always used a compiler that warns when you use a non-prototyped
function.


Some day, you may be forced to use a less friendly set of tools (some
people have provided real life examples in this newsgroup). The less
your coding style relies on tools, the more robust it is and it is a fact
that "reutrn 0;" requires a diagnostic, while "reutrn (0);" doesn't (in
C89). All other things being equal, it is downright foolish to ignore
such arguments.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #25
In <mN************ *******@news01. bloor.is.net.ca ble.rogers.com> "nobody" <no****@nowhere .non> writes:
OTOH, I've seen ([sur]real code in [sur]real system) something like:

#define return(x) \
{ \
printf("%d\n", x); \
/* 10 or so lines of "debugging" code */
return 0; \
}

In order to use this "facility", return *had* to be coded with parentheses
around value. If that was a good practice is beside the point on hand.


Turning keywords into macros is already bad, but giving these macros
misleading semantics is downright atrocious. To inject some sanity into
such a macro, it should look like this:

#define return(x) \
{ \
printf("%d\n", (int)(x)); \
/* 10 or so lines of "debugging" code */ \
return x; \
}

Still has a problem with functions returning structures or unions, but
it requires a compile time diagnostic. And the basic semantics
of return are preserved (the function still returns what it was
supposed to return).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #26
In <lz************ @cts.com> Keith Thompson <ks*@cts.com> writes:
"Fao, Sean" <en**********@y ahoo.comI-WANT-NO-SPAM> writes:
[...]
When I first started programming in C, I was asked to write a macro, which
would calculate the number of elements in an array, but couldn't use any
functions. I knew I could do it like this:

#define NUM_ELTS(x) (sizeof(x) / sizeof(x[0]))

However, at the time, the parentheses that I was taught to use made me think
that sizeof was a function.


I would write that as

#define NUM_ELTS(x) (sizeof(x) / sizeof((x)[0]))

References to macro arguments should always be enclosed in
parentheses; otherwise you can run into some really nasty problems
with operator precedence. Remember that the argument is expanded into
the text, not into the expression tree.


Care to provide a concrete example of a *correct* NUM_ELTS() invocation
where the extra parentheses make any difference?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #27

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bn******** **@sunnews.cern .ch...
In <qchmb.9047$9E1 .40254@attbi_s5 2> "Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> writes:
The syntax of if, for, and while, to name a few, C statements, requires
parenthesis and I don't think it makes them look like functions.


That's because something else is *usually* following the right
parenthesis. And even when there is nothing else, many people prefer
to write the semicolon on a separate line.


Yes, and also the difference between languages with, and without, reserved
words.

PL/I doesn't have reserved words, so that procedure calls must either be in
an expression, such as part of an assignment statement, or from a CALL
statement.

But C already confuses the issue between statements and function calls.
The exit() function is similar to the STOP statement in PL/I and Fortran,
though it could be considered related to return. Though I do agree that
having return as a function call would be very strange. It does seem that
exit would make more sense as a statement than a function.

-- glen
Nov 13 '05 #28
Da*****@cern.ch (Dan Pop) writes:
In <lz************ @cts.com> Keith Thompson <ks*@cts.com> writes:

[...]
I would write that as

#define NUM_ELTS(x) (sizeof(x) / sizeof((x)[0]))

References to macro arguments should always be enclosed in
parentheses; otherwise you can run into some really nasty problems
with operator precedence. Remember that the argument is expanded into
the text, not into the expression tree.


Care to provide a concrete example of a *correct* NUM_ELTS() invocation
where the extra parentheses make any difference?


I can't think of one, and there may not be one, but I'd much rather
add the parentheses than take the time to convince myself that they're
not necessary in this particular instance.

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #29
Glen Herrmannsfeldt wrote:

"Dan Pop" <Da*****@cern.c h> wrote in message
news:bn******** **@sunnews.cern .ch...
In <qchmb.9047$9E1 .40254@attbi_s5 2> "Glen Herrmannsfeldt"

<ga*@ugcs.calte ch.edu> writes:

The syntax of if, for, and while, to name a few, C statements, requires
parenthesis and I don't think it makes them look like functions.


That's because something else is *usually* following the right
parenthesis. And even when there is nothing else, many people prefer
to write the semicolon on a separate line.


Yes, and also the difference between languages with, and without, reserved
words.

PL/I doesn't have reserved words, so that procedure calls must either be in
an expression, such as part of an assignment statement, or from a CALL
statement.

But C already confuses the issue between statements and function calls.
The exit() function is similar to the STOP statement in PL/I and Fortran,
though it could be considered related to return. Though I do agree that
having return as a function call would be very strange. It does seem that
exit would make more sense as a statement than a function.

Nonetheless, it is a function. Several years ago I examined some C
startup and shutdown. The start: label set up stuff from the command
processor arguments, argc and argv, and then called
'exit(main(argc ,argv))'. exit() saved main()'s return value and, after
'closing' the C environment, returned it to the command processor (an
int value). Of course this is annecdotal and may have nothing to do with
C today.
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #30

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

Similar topics

10
2623
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some of that software. That subject seems not to be addressed, at least not directly, in the C FAQ where FAQ 5.2 seems most relevant. References: * C FAQ 5.2 Null pointers (Including conditions where "casting" of null pointer...
15
6739
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int i = 0,j; j = sample(0);
40
3168
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case I'd like to halt the computation immediately and return false. My current approach is to have...
0
10840
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10588
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
10254
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
9371
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...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3112
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.