Hi All,
I was taught that argument valuse is not supposed to be changed in
function body. Say, below code is not good.
void foo1(int x)
{
x ++;
printf("x+1 = %d\n", x);
}
It should be "refactor-ed" to be
void foo2(int x)
{
int y = x;
y ++;
printf("x + 1 = %d\n", y);
}
Recently, I found one guy posted code like foo1 in one C lang forum. I
pointed out that it is not good code standard. Surprisingly, seems all
people criticise me as dogmatism. It is claimed that changing arugment
value will not affect caller code and it saves stack size. I understand
their point, but I DO remember it is commmon that argument is not
suppsoed to be changed.
Now I am confused. Is there anybody can give some explaination why it is
a code rule that argument should not be changed?
Thanks & Regards
Nov 15 '05
64 3290
On Tue, 26 Jul 2005 20:35:56 GMT, Mark
<so***@localbar.com> wrote: "CBFalconer" <cb********@yahoo.com> wrote in message news:42***************@yahoo.com... <snip> Of course, after all the fuss, this is a function that can be improved by not altering the input parameter. And by improved, do you mean broken?
size_t length_of_string(const char *s) { const char *t = s;
while (*t++) continue; return t - s; }
(and I will live with the possibility that ptr_diff is too small)
You're also living with the fact that: printf("%d\n", length_of_string("")); will print 1.
Actually, it will exhibit undefined behaviour since the function returns
size_t and %d expects an int...
Since we're being picky, and all...
Chris C
On 26 Jul 2005 13:30:57 -0700, Tim Rentsch
<tx*@alumnus.caltech.edu> wrote: I think of modifying function parameters in much the same way that I think of using goto. Completely undisciplined use of goto is bad. Beginners need to be steered away from goto rather emphatically, because if they aren't they tend to overuse it. After reaching a certain level of experience and competence, and mainly having acquired the discipline not to use it unnecessarily, allowing goto's in certain cases seems acceptable and reasonable. I use goto myself now and then; but when I do it's a conscious decision made after considering and comparing non-goto alternatives.
Good analogy, although I don't think of modifying function parameters as
being quite as harmful as using goto. I don't remember the last time I
wrote a C program with a goto anywhere, it's something which I wouldn't
rule out totally but I can usually avoid it without too much strain
(often by having multiple exit points to a function, which some people
regard as almost as bad).
Chris C
CBFalconer wrote: pete wrote: CBFalconer wrote:
size_t length_of_string(const char *s) { const char *t = s;
while (*t++) continue; return t - s; }
(and I will live with the possibility that ptr_diff is too small)
ptr_diff is too small. Why not just use strlen?
Because all this is illustrations of cases for the subject line, not practical proposals. You snipped the whole raison d'etre.
It's not an example of fast code.
It's not an example of applying the rules of C
to do what strlen does.
--
pete
In article <sl******************@ccserver.keris.net>
Chris Croughton <ch***@keristor.net> wrote: Good analogy, although I don't think of modifying function parameters as being quite as harmful as using goto. ...
It is in some other, non-C languages:
SUBROUTINE FOO(X, I, LAST)
INTEGER I, LAST
REAL X(LAST)
IF (I .GT. LAST) GOTO 20
10 CONTINUE
COMPUT(X(I))
I = I + 1
GOTO 10
20 CONTINUE
RETURN
END
Here, this Fortran code has the obnoxious side effect of incrementing
the variable you call it with:
...
CALL FOO(ARR, I, 20)
C now I .EQ. 21
Perhaps some of the dogmatic "never modify a parameter" is left over
from old Fortran coders. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
CBFalconer <cb********@yahoo.com> writes: Tim Rentsch wrote: CBFalconer <cb********@yahoo.com> writes: Tim Rentsch wrote: CBFalconer <cb********@yahoo.com> writes: > Tim Rentsch wrote: >> > ... snip ... >> general rule, be thought of as good style. At the very least, it >> should be immediately obvious which parameters are modified and which >> are not. If a development team really wanted the freedom to modify a >> function parameter on any function, then it would be good practice to >> attach 'const' to any parameters that are not modified. There doesn't >> seem to be much upside to allowing function parameters to be modified; > ... snip ... > > On the contrary, hanging extraneous 'const' on the parameters would > impose an extra anomalous connection between code modules.
What connection would that be? Declaring a parameter 'const' (the parameter itself, not something it points to) doesn't impose any burden on any call site.
It restricts the freedom of action when revising the function, to no purpose. We are not talking about pointers here, but actual value parameters. Modifying them cannot affect anything outside the function anyhow. You still haven't given any connection between "code modules", assuming by "code modules" you mean two different sections of code each at least as big as one function body, which is how I think most people would read the term.
As to the rest of the comment - lots of people see a benefit in using 'const' for function-local variables that won't be modified during function execution. If you choose not to avail yourself of those benefits, well, that's your prerogative. But it seems rather silly to say that there are no such benefits.
I've put myself in a peculiar position, for one who espouses Pascal and the restrictions imposed by strong typing, etc. Here I am arguing for the elimination of restrictions in C. In part it is the minimalist in me - I don't want any extra variables declared, and I don't want any extra restrictions forcing me to so declare.
Up to here I understand what you're saying.
I consider every extra thing that is needed to be known outside the connected modules to be a connection. For a normal non-static function there are three entities involved: The user, the prototype, and the function. For minimal connectivity there should be minimal restrictions on the user and the function. In general we can consider the prototype to be the longest lived of all.
Maybe this makes sense.
For the rest of it you lose me. I'm not sure how some words
are being used, nor what you're meaning to say with them.
For the three entities: of course the developer has to
understand a function in order to write it or modify it; it
can't be any different whether there are lots of restrictions
or none at all.
As regards prototypes: having function parameters be const
needn't affect the function's prototype at all. The code
below is legal:
int foo( int );
...
int
foo( const int t ){
return t;
}
See 6.7.5.3 p15 (the parenthesized final sentence).
On 27 Jul 2005 04:37:10 GMT, Chris Torek
<no****@torek.net> wrote: In article <sl******************@ccserver.keris.net> Chris Croughton <ch***@keristor.net> wrote:Good analogy, although I don't think of modifying function parameters as being quite as harmful as using goto. ... It is in some other, non-C languages:
SUBROUTINE FOO(X, I, LAST) INTEGER I, LAST REAL X(LAST) IF (I .GT. LAST) GOTO 20 10 CONTINUE COMPUT(X(I)) I = I + 1 GOTO 10 20 CONTINUE RETURN END
Here, this Fortran code has the obnoxious side effect of incrementing the variable you call it with:
Oh, I see, you are using CONTINUE as a null statement just to be the
target of the GOTO. Confused me at first, I was looking for the DO...
... CALL FOO(ARR, I, 20) C now I .EQ. 21
Perhaps some of the dogmatic "never modify a parameter" is left over from old Fortran coders. :-)
I am an old FORTRAN IV coder <g>. Interesting that it's something I
never particularly carried over (although I wouldn't use I as a
parameter, it's far too useful as a loop variable). I think I was
relieved when I found variables passed by value, although that might
trip me up if I ever program Fortran...
Chris C
"Chris Croughton" <ch***@keristor.net> wrote in message
news:sl******************@ccserver.keris.net... On Tue, 26 Jul 2005 20:35:56 GMT, Mark <so***@localbar.com> wrote:
"CBFalconer" <cb********@yahoo.com> wrote in message news:42***************@yahoo.com... <snip> Of course, after all the fuss, this is a function that can be improved by not altering the input parameter. And by improved, do you mean broken?
size_t length_of_string(const char *s) { const char *t = s;
while (*t++) continue; return t - s; }
(and I will live with the possibility that ptr_diff is too small)
You're also living with the fact that: printf("%d\n", length_of_string("")); will print 1.
Actually, it will exhibit undefined behaviour since the function returns size_t and %d expects an int...
Actually, it's very well defined on my implementation.
size_t = unsigned int
with value explicitly set to 1 the sign bit is irrelevant.
You've peaked my curiosity, on what implementations
(excluding the infamous DeathStation 9000) is a
size_t not defined as an 'unsigned int' ?
Since we're being picky, and all...
We? Ah, I didn't realize there were two of you! :-)
Regards,
Mark
In article <sl******************@ccserver.keris.net>,
Chris Croughton <ch***@keristor.net> wrote: Oh, I see, you are using CONTINUE as a null statement just to be the target of the GOTO. Confused me at first, I was looking for the DO...
Um, CONTINUE with a DO is just a null statement to be the termination
of the DO. You only need it when the terminating statement would be
a GOTO.
-- Richard
In article <dc*********@news3.newsguy.com> Chris Torek <no****@torek.net> writes:
.... SUBROUTINE FOO(X, I, LAST) INTEGER I, LAST REAL X(LAST) IF (I .GT. LAST) GOTO 20 10 CONTINUE COMPUT(X(I)) I = I + 1 GOTO 10 20 CONTINUE RETURN END
Here, this Fortran code has the obnoxious side effect of incrementing the variable you call it with:
It has a much more obnoxious effect. If I <= LAST on entry it gives
an infinite loop and quite some UB. ;-).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
On Wed, 27 Jul 2005 21:32:34 GMT, Mark
<so***@localbar.com> wrote: "Chris Croughton" <ch***@keristor.net> wrote in message news:sl******************@ccserver.keris.net... On Tue, 26 Jul 2005 20:35:56 GMT, Mark <so***@localbar.com> wrote:
"CBFalconer" <cb********@yahoo.com> wrote in message news:42***************@yahoo.com... <snip> Of course, after all the fuss, this is a function that can be improved by not altering the input parameter. And by improved, do you mean broken?
size_t length_of_string(const char *s) { const char *t = s;
while (*t++) continue; return t - s; }
(and I will live with the possibility that ptr_diff is too small)
You're also living with the fact that: printf("%d\n", length_of_string("")); will print 1.
Actually, it will exhibit undefined behaviour since the function returns size_t and %d expects an int...
Actually, it's very well defined on my implementation. size_t = unsigned int with value explicitly set to 1 the sign bit is irrelevant.
You've peaked my curiosity, on what implementations (excluding the infamous DeathStation 9000) is a size_t not defined as an 'unsigned int' ?
At least one x86 compiler which had 16 bit int but allowed objects
bigger than 64KB (Aztec? IAC? One of the expensive ones, anyway). The
same can easily apply to 64 bit machines (IIRC one of the DEC Alpha
compilers had size_t 64 bits and int 32 bits). Definitely doesn't need
a DS9000 series machine, the usual recommendation is to cast it to
unsigned long at least for printing (unsigned long long with a compiler
and library which support that).. > Since we're being picky, and all...
We? Ah, I didn't realize there were two of you! :-)
I am large, I contain multitudes (generally attributed to Walt Whitman,
but I've heard that it have been said earlier).
Chris C
>In article <dc*********@news3.newsguy.com> Chris Torek <no****@torek.net> writes:
[snippage] IF (I .GT. LAST) GOTO 20 10 CONTINUE COMPUT(X(I)) I = I + 1 GOTO 10 20 CONTINUE
In article <IK********@cwi.nl> Dik T. Winter <Di********@cwi.nl> wrote:It has a much more obnoxious effect. If I <= LAST on entry it gives an infinite loop and quite some UB. ;-).
Oops. Swap the first two quoted lines, yes. :-) Also, the third line
should say "CALL COMPUT...", though of course this was really shorthand
for "some computation using X(I)".
(It has been at least 20 years since I wrote any Fortran, and even
then I used various RATFOR variants -- the one on BSD Unix, and
before that, the one I wrote for myself to use with Microsoft's
F80 on my TRS-80. Of course I had to write that one in Fortran.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Chris Torek wrote: In article <sl******************@ccserver.keris.net> Chris Croughton <ch***@keristor.net> wrote:Good analogy, although I don't think of modifying function parameters as being quite as harmful as using goto. ...
It is in some other, non-C languages:
SUBROUTINE FOO(X, I, LAST) INTEGER I, LAST REAL X(LAST) IF (I .GT. LAST) GOTO 20 10 CONTINUE COMPUT(X(I)) I = I + 1 GOTO 10 20 CONTINUE RETURN END
Here, this Fortran code has the obnoxious side effect of incrementing the variable you call it with:
... CALL FOO(ARR, I, 20) C now I .EQ. 21
Perhaps some of the dogmatic "never modify a parameter" is left over from old Fortran coders. :-)
Chris, you've been writing C for() loops for too long. That function
NEVER returns! 8^)
Ed
(You need the 10 on the IF line.)
In article <S3*******************@newshog.newsread.com>, "Mark" <so***@localbar.com> writes: You've peaked my curiosity,
Impressive. Most people's curiosity generally gets piqued; peaking it
requires something *really* curious.
on what implementations (excluding the infamous DeathStation 9000) is a size_t not defined as an 'unsigned int' ?
Well, most I32LP64 implementations, I imagine.
--
Michael Wojcik mi************@microfocus.com
Vinegar keeps more flies away than honey does.
Michael Wojcik wrote: "Mark" <so***@localbar.com> writes: You've peaked my curiosity,
Impressive. Most people's curiosity generally gets piqued; peaking it requires something *really* curious.
I can see direct applicability to a PHB. :-)
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
"Michael Wojcik" <mw*****@newsguy.com> wrote in message
news:dc*********@news4.newsguy.com... In article <S3*******************@newshog.newsread.com>, "Mark" <so***@localbar.com> writes: You've peaked my curiosity, Impressive.
Thank you.
Most people's curiosity generally gets piqued;
peaked curiosity (googled) 85,300 hits
piqued curiosity (googled) 139,000 hits
I'll give you 'more' people... but not most ;)
peaking it requires something *really* curious.
Are you sure about that?
peaked : To bring to a maximum of development, value, or intensity
pique : French, a prick, irritation, from Old French, from piquer, to prick,
from Vulgar Latin *piccre, ultimately of imitative origin.]
What??? Come on, a french prick irritation???
I guess what you call pique we call the clap! :-)
Regards,
Mark This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mike420 |
last post by:
In the context of LATEX, some Pythonista asked what the big
successes of Lisp were. I think there were at least three *big*
successes.
a. orbitz.com web site uses Lisp for algorithms, etc.
b....
|
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: LaundroMat |
last post by:
Suppose I have this function:
def f(var=1):
return var*2
What value do I have to pass to f() if I want it to evaluate var to 1?
I know that f() will return 2, but what if I absolutely want to...
|
by: Larax |
last post by:
Best explanation of my question will be an example, look below at this
simple function:
function SetEventHandler(element)
{
// some operations on element
element.onclick =
function(event)
{
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |