3 ms·
Interesting that this uses the coding convention of putting parentheses around the expression in return statements. So return (1); instead of just r
by unwind 3y ago
Interesting that this uses the coding convention of putting parentheses around the expression in return statements.
So
return (1);
instead of just
return 1;
I remember using the former style as I tried (in frustration) to learn C back on the Amiga (so roughly at the same time, late 80s/early 90s) using articles in magazines.
Does anyone know where it comes from?
Some time later (late 90s) at university I learned C again but with actual (good!) teachers, and also got exposed to way more code and talk about code thanks to Internet and comp.lang.c etc, and around that time (I think?) I stopped using them.
Nowadays they always make me think the author is anxious that the function will return before the entire expression is evaluated, which makes me smile. :)
- pavlov 3y agoMaybe it’s a way to highlight that returning an expression value directly is possible? Back then C was rare on home computers. Programmers were more used to BASIC where typically GOSUB / RETURN couldn’t return a value at all.
- tom_ 3y agoI used to do it as well, but I'm not sure why either? (I learned C in the early 90s.) The reference material was never great back in those days, so maybe I just started doing it by extending the rules for if(), while(), for() and function calls and not getting any pushback from the compiler. Eventually I stopped because it saves 1 byte and means pressing fewer keys.
- CakeEngine 3y agoAnother early 90s C learner here, and I also put (and still do!) parentheses around return expressions. But I do: `return( 1 );` not `return (1);` ...because the bracket belongs to the return, not the expression. It's by analogy with 'if' and 'while', a mental "look out, here comes an expression" warming.
- icedchai 3y agoI looked at some C code I wrote about 25 years ago. It all followed the return(N) convention. I also learned C on an Amiga, coincidentally. I started with Lattice / SAS C...
- JNRowe 3y agodmr's reference manual¹ used it, see section 9.10. Matt Dillon's DICE would have been popular on the Amiga then, and it looks like it also favored parens. The latest tarball of DICE² is pretty fun for a spelunking session, if you're feeling nostalgic. It makes for an interesting mix as it pulls in a few contemporaneous non-Dillon tools with varying styles. ¹ https://www.bell-labs.com/usr/dmr/www/cman.pdf https://www.bell-labs.com/usr/dmr/www/cman.pdf ² http://apollo.backplane.com/FreeSrc/dice-1.15.tgz http://apollo.backplane.com/FreeSrc/dice-1.15.tgz
- AnimalMuppet 3y agoIs it to visually distinguish from an empty return? That is, return 1; is visually closer to return; than return (1); is. So this could be a way of signaling "hey, I'm returning a value here, not just leaving the function". But I'm just speculating...
- convolvatron 3y agoI am extremely biased. but I always think of return as an implicitly bound continuation. so it seems quite natural to want to call it.