473,657 Members | 2,652 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 #1
32 8836
In article <3f************ ***********@new s.xs4all.nl>, Mike Machuidel wrote:
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.

It's not wrong, it's just not needed.

See the FAQ at

http://www.eskimo.com/~scs/C-faq/q20.19.html

--
Andreas Kähäri
Nov 13 '05 #2
Mike Machuidel <ma*******@yaho o.com> wrote:
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.


There's nothing wrong with it from a technical POV. Some people don't
like it as a style issue, because _they_ are the ones who keep
forgetting that return is not a function if they keep seeing the parens,
but that's their problem, not yours. FTR, I don't put parens around the
argument of return, but I won't shout at you for doing so.

Richard
Nov 13 '05 #3

"Andreas Kahari" <ak*******@free shell.org> wrote in message
news:sl******** **************@ vinland.freeshe ll.org...
In article <3f************ ***********@new s.xs4all.nl>, Mike Machuidel

wrote:
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.

It's not wrong, it's just not needed.

See the FAQ at

http://www.eskimo.com/~scs/C-faq/q20.19.html


Technically whitespace isn't needed by the compiler either [at least to the
extent most people use it, obviously you need to seperate tokens...]

Tom
Nov 13 '05 #4

"Mike Machuidel" <ma*******@yaho o.com> wrote in message
news:3f******** *************** @news.xs4all.nl ...
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.


It isn't wrong but if your company has standards for coding I suggest you
follow them.

Also "return(0); " does look stupid I'd put a space there "return (0);"

Though I'm against returning constants alltogether. You should have some

enum {
RES_OK,
RES_ERR
};

[etc]

and return RES_OK instead.

Tom
Nov 13 '05 #5
In <3f************ ***********@new s.xs4all.nl> Mike Machuidel <ma*******@yaho o.com> writes:
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.


There is nothing wrong, if you also write such things:

i = (j) + (k);
for (i = (0); i < (sizeof(foo)); i++) ...
printf("%d\n", (i));

If you don't, then what's the point in surrounding the return
expression by a pair of completely useless parentheses? The language
accepts them for the sole reason that every expression can be
surrounded by an arbitrary number of matching pairs of parentheses
(up to 32 in C89 or 63 in C99). So, why limit yourself at a single
pair, when return((((((((( 0))))))))); is so much cooler?

The other reason against parentheses in return statements is related to
what happens if you make a typo. In C89, retunr(0); is a valid function
call that doesn't require any diagnostic, while retunr 0; is a syntax
error requiring a diagnostic. Why prevent the compiler from detecting
and reporting your typos?

In short, in a C program, *unneeded* parentheses should be used only when
they improve the code readability (usually complex expressions). I don't
find return(0) and more readable than return 0 and the previous paragraph
explains why the latter is to be preferred.

During the early days of C, the syntax of the return statement required
the parentheses. Later, it was realised that they don't serve any *good*
purpose and, by the time K&R1 was printed, they were already removed from
the language syntax (but not from the code examples included in the book).
This explains why some people who learned C more than 15 years ago may
still use them.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
On Fri, 24 Oct 2003, Mike Machuidel wrote:
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.
I would never say it is 'wrong' to use:

return(0);

There could be the off chance a utility would see this as a function and
get confused (or maybe a REALLY junior programmer).

One thought could be that junior programmers might assume it is a function
call. To make it clear to everyone, don't use the parentheses so people
know it is not a function call. The same would hold true for the sizeof
operator.

What it really boils down to is people being nitpicky. I'm sure if I
wrote:

return((((((0)) ))));

that would bother a lot of people. There is nothing really wrong with it.
It just bothers people because there is no need for the parentheses.
Thank you,
Mike Machuidel


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 13 '05 #7
"Andreas Kahari" <ak*******@free shell.org> wrote in message
news:sl******** **************@ vinland.freeshe ll.org...
It's not wrong, it's just not needed.

See the FAQ at

http://www.eskimo.com/~scs/C-faq/q20.19.html


As the FAQ points out, "parenthese s are optional with the sizeof operator,
too, as long as its operand is a variable or a unary expression".

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.

In my opinion, any C programmer who doesn't know that parentheses are
optional in these cases, and others, has no business working on any real
projects other then the ones out of a workbook. Maybe your use of
parentheses suggested to you coworkers a lack of understanding. As you
pointed out, it's not wrong; but, they aren't necessary. What would you
think if a coworker started writing code like this:

int main(void)
{
int a = (32);
printf("%d\n", (a));
return (0);
}

Sean
Nov 13 '05 #8
"Mike Machuidel" <ma*******@yaho o.com> wrote in message
news:3f******** *************** @news.xs4all.nl ...
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;".


I'm dying to know, how old is that programmer?
Nov 13 '05 #9
On Fri, 24 Oct 2003 10:13:57 -0400, Fao, Sean wrote:
In my opinion, any C programmer who doesn't know that parentheses are
optional in these cases, and others, has no business working on any real
projects other then the ones out of a workbook. Maybe your use of
parentheses suggested to you coworkers a lack of understanding. As you
pointed out, it's not wrong; but, they aren't necessary. What would you
think if a coworker started writing code like this:

int main(void)
{
int a = (32);
printf("%d\n", (a));
return (0);
}


Probably the same as if a found a coworker who had written...

if (a && b || c && d != -1)

....parentheses are mainly about style, and yes examples can be provided at
either extreme that are terrible. Personally I find that I think of...

if (x)
while (x)
switch (x)
return (x)
exit (x)

....all as "similar" so I make them look the same. I also, personally, find
that I think of...

strlen(x)
sizeof(x)
malloc(x)

....as "similar", so I make them look the same. That doesn't mean I don't
understand that sizeof() isn't a real function (even though it's even
closer to acting like one in C99), I just prefer to see it that way.

As was said, if you have coding stds. for what you are working on then
almost any style applied consistently is better than a mixture of
anything. So if the rest of the code base used non-parentheses return
statements then you should change.

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.

[1] Well, ok, every one apart from my newsclient.

--
James Antill -- ja***@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/

Nov 13 '05 #10

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

Similar topics

10
2599
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
6723
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
3120
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
8392
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
8305
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,...
0
8825
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...
0
8732
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...
0
7324
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
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
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.