In said:
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