[net.lang.c] return expr and Decus-C

minow@decvax.UUCP (Martin Minow) (05/05/84)

Previously, on net.lang.c, the following was stated:

1.  Decus C does not support "return expression;" where the
    expression is not enclosed in parentheses.

2.  Decus C predates Kernighan and Ritchie, where the parentheses
    were shown as optional.

3.  Version 6 Unix C compilers accepted this syntax before
    before of Kernighan and Ritchie.

These statements are true.  However, Decus C was coded using the
Unix Version 6 reference manual, where the only valid forms for the
return statement were

	return
and	return ( expression )

Since the person who wrote Decus C did not have access to the
source code for Unix C, he had no way of knowing that the syntax
accepted by the compiler did not agree with the reference manual.

We have not bothered to change Decus C as we feel that we would be
legitimizing a poor programming practice.  The change to the compiler,
which is very simple, is left as a exercise for the student.


Martin Minow
decvax!minow

gwyn@brl-vgr.UUCP (05/08/84)

return expr;
is preferable to
return(expr);
since the latter looks like a function call (which it is not).

smh@mit-eddie.UUCP (Steven M. Haflich) (05/10/84)

This message is empty.

toml@druxm.UUCP (05/10/84)

>	From: gwyn@brl-vgr.UUCP
>
>	return expr;
>	is preferable to
>	return(expr);
>	since the latter looks like a function call (which it is not).

By this reasoning, we should use "while expr" instead of "while (expr)",
and "if expr" instead of "if (expr)".  And don't forget "switch expr" to
replace "switch (expr)".

My own opinion is that "return (expr)" is clearer, since it clearly
distinguishes between the keyword and the parameter.  I think of this as
a general "thing-to-do (thing-to-do-it-with)" syntax that is consistent
among C commands, functions, and macro pseudo-functions.

		Tom Laidig
		AT&T Information Systems Laboratories, Denver
		...!ihnp4!druxm!toml

coltoff@burdvax.UUCP (Joel Coltoff) (05/10/84)

<< SLIME MOLD >>

	Do you think that

	if expr

	looks better than

	if(expr)

	since that also looks like a function call.


	I love to play devils' advocate
-- 

	Joel Coltoff	{presby,bpa,psuvax}!burdvax!coltoff
			(215)648-7258

gwyn@brl-vgr.ARPA (Doug Gwyn ) (05/11/84)

Yes,
	( expr )
is also an
	expr
which is why
	return( expr );
is legal although the definition is
	return expr;
However, stylistically I recommend leaving unnecessary parentheses off
the return expr since, as I said, they make it look like a function call.
Both ways are legal and this wouldn't be an important issue except that
some C compilers (apparently the DECUS C compiler) do not accept the
more general form.

gwyn@brl-vgr.ARPA (Doug Gwyn ) (05/11/84)

The examples
	while expr
etc. are silly.  They are not even legal C.
	return expr;
is.

alan@allegra.UUCP (Alan S. Driscoll) (05/11/84)

> From: gwyn@brl-vgr.ARPA (Doug Gwyn )
> Newsgroups: net.lang.c
> Subject: Re: return expr and Decus-C

> The examples
>	while expr
> etc. are silly.  They are not even legal C.
>	return expr;
> is.


Of course the examples aren't legal C, but that's not the
point.  The question is, if you are worried that

	return (expr);

looks like a function call, why aren't you worried that

	while (expr) stat;

also looks like a function call?  (Remember that 'stat'
can be the null statement.)  Aren't you being just a bit
inconsistent?

-- 
	Alan S. Driscoll
	AT&T Bell Laboratories

chris@umcp-cs.UUCP (05/14/84)

Who says ``while (expr) statement;'' looks like a function call?
If it were a function call, the statement part would be a syntax
error.  (Same goes for if(), for(), etc....  Everything except
``return''.  [Well, ok, ``do-while''s are a bit funky, but I try
to end them with ``} while (expr);''.])
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland

ks@ecn-ee.UUCP (05/14/84)

#R:decvax:-47800:ecn-ee:13100012:000:816
ecn-ee!ks    May 13 20:44:00 1984

> Of course the examples aren't legal C, but that's not the
> point.  The question is, if you are worried that
> 
> 	return (expr);
> 
> looks like a function call, why aren't you worried that
> 
> 	while (expr) stat;
> 
> also looks like a function call?  (Remember that 'stat'
> can be the null statement.)  Aren't you being just a bit
> inconsistent?
> 
> -- 
> 	Alan S. Driscoll
> 	AT&T Bell Laboratories

Personally, my perferred style avoids confusion.
If a keyword is followed by a '(', I seperate them by a space.
Admittedly,
	while(expr);
looks like a function call, but
	while (expr)
		;
does not.
Clear style is up to the programmer.
Function calls should "look" like function calls,
macros should look like function calls, but
"return"s should not look like function calls.

				Kirk Smith
				Purdue EE

alan@allegra.UUCP (Alan S. Driscoll) (05/14/84)

[]

>From: chris@umcp-cs.UUCP
>Newsgroups: net.lang.c
>Subject: Re: return expr and Decus-C
>Posted: Sun May 13 21:47:44 1984

>Who says ``while (expr) statement;'' looks like a function call?
>If it were a function call, the statement part would be a syntax
>error.

You are forgetting that the body of a while statement may be empty.
-- 

	Alan S. Driscoll
	AT&T Bell Laboratories

gwyn@brl-vgr.ARPA (Doug Gwyn ) (05/14/84)

Sorry, folks, but the C language gives me no option on
	if ( cond ) stmt
or
	while ( cond ) stmt
It DOES give me the choice of whether to write
	return expr;
or
	return( expr );
Given the choice, I vote against the redundant parentheses.  If the
rest of the language syntax were more like Algol-68 or Djikstra's
then I would be happier, but short of using Bourne's macros I have
no choice about that.

C definitely overworks its special characters and keywords, and
this adversely affects readability.

(Nevertheless I would rather use C than any of the major contenders,
but within its syntax one should still adopt as readable a style as the
language permits.)

dyer@vaxuum.DEC (Example #22) (05/15/84)

Re: return expr and Decus-C____________________________________________________
	Keywords like if, while, and switch (which use parentheses for their
arguments) have already been mentioned as non-functions that use parentheses.
I like the parentheses, and if anyone wishes to avoid confusion between func-
tions and keywords-with-parentheses, just use some whitespace!
	For example:
	    function(arg1,arg2)
	    keyword (arg1,arg2)
or, if you prefer,
	   function( arg1 , arg2 )
	   keyword ( arg1 , arg2 )
	The idea is that the function is "connected" to the parentheses.
		<_Jym_>
: Jym Dyer : Nashua, NH : ...{allegra,decvax,ucbvax}!decwrl!rhea!vaxuum!dyer :

gwyn@brl-vgr.ARPA (Doug Gwyn ) (05/15/84)

Thanks, Kirk.  I too use white space differently for the different
(overloaded!) uses of parentheses in C.  E.g.
	func( param, param );
	if ( cond )
		stmt;
	while ( cond )
		stmt;
	a = (b + c) / d;
	return a == b;

chris@umcp-cs.UUCP (05/16/84)

You're right, I did actually forget about null statements, but I'm
right too:  in most cases,

	<insert arbitrary function name> (expr) <statement> ;

would be a syntax error.  Besides, I try to keep all my "while"s
with null statements as

	while (expr)
		;

(By the way, does anyone have an option for the "indent" program
that comes with Gosling Emacs, to make it not put null statement
";"s right up against the close parenthesis?)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland

ajs@hpfcla.UUCP (05/16/84)

> return expr;
> is preferable to
> return(expr);
> since the latter looks like a function call (which it is not).

Unless, of course, expr is more complicated than the null string, in which
case parentheses are a very nice way to set it apart.  But seriously...


	RETURN(3)		Parody Systems			RETURN(3)


	NAME
	   return -- return from C procedure call

	SYNOPSIS
	   void return (value)
	   anytype value;

	DESCRIPTION
	   This  procedure  never  returns to the  caller.  Instead,  it
	   casts the given  value to the  procedure  type of the calling
	   procedure,  without loss of size or accuracy.  Then it causes
	   the user  program  to  continue  immediately  after the place
	   where the calling  procedure was itself  called.  The calling
	   procedure  appears  to  return  value  to  its  caller  as  a
	   constant of the proper type, should the caller care to use it
	   in an expression (including a simple assignment).

	   The value parameter is optional and may be omitted.

	   If a  procedure  ends  without  calling  return()  first, the
	   operating system calls it for you with no parameters.

	SEE ALSO
	   setjmp(3)

	BUGS
	   Due to a bug in the compiler,  this  procedure  can be called
	   without placing parentheses around its parameter.

rcd@opus.UUCP (Dick Dunn) (05/16/84)

>>	return expr;
>>	is preferable to
>>	return(expr);
>>	since the latter looks like a function call (which it is not).
>
>By this reasoning, we should use "while expr" instead of "while (expr)",
>and "if expr" instead of "if (expr)".  And don't forget "switch expr" to
>replace "switch (expr)".

Seems like a good idea to me.  (Put down that torch!  Not for C, but for
another language.)  After all, Pascal manages without the parens around the
expr's in "if", "while", and "case" (Pascal analog of "switch").  Of
course, some Pascalers are amazed that C doesn't have "then" to go with
"if".
(It's a little trickier to get rid of BOTH the parens around the expr in
the "if" AND the "then" keyword - but it's possible.  Look at the way Icon
handles comparable situations.)
-- 
...A friend of the devil is a friend of mine.		Dick Dunn
{hao,ucbvax,allegra}!nbires!rcd				(303) 444-5710 x3086

bobm@zinfandel.UUCP (05/16/84)

#R:decvax:-47800:zinfandel:14600019:000:446
zinfandel!bobm    May 14 23:40:00 1984

> 1.  Decus C does not support "return expression;" where the
>     expression is not enclosed in parentheses.

> We have not bothered to change Decus C as we feel that we would be
> legitimizing a poor programming practice.  The change to the compiler,
> which is very simple, is left as a exercise for the student.

What's poor about writing

	return 0;

At least it doesn't look like a function call, like

	return(0);

does.

					Bob Miller

hamilton@uiucuxc.UUCP (05/16/84)

#R:decvax:-47800:uiucuxc:21000012:000:155
uiucuxc!hamilton    May 16 14:39:00 1984

i prefer
    return (expr);

why shouldn't a function un-call resemble a function call?  i've never
gone hunting for a function named "return" by mistake.

jim@ism780.UUCP (05/18/84)

#R:brl-vgr:-151400:ism780:12500008:000:410
ism780!jim    May 16 19:00:00 1984

> >Who says ``while (expr) statement;'' looks like a function call?
> >If it were a function call, the statement part would be a syntax
> >error.
>
> You are forgetting that the body of a while statement may be empty.

Some people can't tell the difference between a discussion and a debating
club.  Please restrict this sort of irrelevant drivel to net.politics.

-- Jim Balter, INTERACTIVE Systems (ima!jim)