Re: Re-use the argument?



richard@xxxxxxxxxxxxxxx (Richard Tobin) writes:

In article <g2h74u$5h3$1@xxxxxxxxxxxxxxxxxxxxxxxxx>,
Richard <rgrdev@xxxxxxxxx> wrote:

Generally it is considered bad form to modify an argument to a
function. It makes things a bit more difficult to read.

What a load of nonsense. If its not a const its no more than a local
variable.

[...]

I really am not interested in that. Its effectively a local variable and
I have never seen anyone object, in the real world, to it being
manipulated.

There's clearly no general rule against modifying variables, at least
in traditional imperative languages like C. But that doesn't mean
you should use any available variable as a temporary. Some variables

It's not a temporary. Any more than any other is.

are intended for a single purpose, and are named accordingly. Some
variables ("i" for example) are traditionally used for a succession

Yes. Agreed. But I'm not sure what this simple concept has to do with
the question about the rights and wrongs of using a parameter. Clearly
you do not use a parameter called "szDate" or whatever to store a
celsius reading ....

of values in a loop. The key is to be clear. For example, consider
a function to determine kinetic energy given mass and speed:

double ke(double mass, double speed)
{
return mass * speed * speed;
}

It would clearly be wrong to write:

double ke(double mass, double speed)
{
speed *= speed;
speed *= mass;
return speed;
}

because that would use the variable "speed" for something which
is not a speed. On the other hand, this would be quite reasonable:

Obviously. I'm at a loss to understand why you feel the need to say this
to be honest.


int mystrlen(char *s)
{
int l = 0;
while(*s++)
l++;
return l;
}

"s" is clearly just a string, and it remains a string as you proceed
through it.

Perhaps still reasonable is:

double ftoc(double temp)
{
temp -= 32;
temp /= 1.8;
return temp;
}

because temp represents a temperature throughout, though its units
change. But it would clearly be wrong if the variable were called
"tempinfahrenheit".

So I don't think there's a special rule for re-using argument

There most definitely isn't any special rule.

variables. You should just apply the same reasoning you would for any
variable: does it make sense to use this variable for this purpose?

-- Richard

I'm not sure who you are addressing this too so I will refrain from
commenting further.

.



Relevant Pages

  • Re: Re-use the argument?
    ... a function to determine kinetic energy given mass and speed: ... "s" is clearly just a string, and it remains a string as you proceed ... double ftoc(double temp) ... You should just apply the same reasoning you would for any ...
    (comp.lang.c)
  • Re: Re-use the argument?
    ... Richard Tobin said: ... a function to determine kinetic energy given mass and speed: ... You need the velocity, not the speed. ...
    (comp.lang.c)
  • Re: Forces in a finite bounded universe
    ... (Richard Tobin) ... Now place 2 masses, maybe one fairly large and the other a small test ... mass, ... How does the test mass respond to the presence ...
    (sci.physics)
  • Re: Dodgy French Food [was -New prefixes for binary multiples; kibi, mebi, gibi etc]
    ... Richard Tobin wrote: ... >>Mass is a primary property, ... You perceive its weight (which is in fact a secondary ...
    (uk.comp.sys.mac)