Re: Re-use the argument?



Tomás Ó hÉilidhe <toe@xxxxxxxxxxx> writes:
On Jun 7, 8:26 am, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
But if your question is: is it worth const-decorating parameters for the
hell of it?, the answer is "no". C is a pass-by-value language.

I was refering purely to the "re-using" of the function argument. A
person might think that the latter version would result in more
efficient code because it does not use a temporary variable for doing
the calculations, but then again this benefit could be rescinded by i)
the use of an acumulator register or ii) the compiler's automatic re-
using of const variables when it knows they're no longer needed. Or
then again the person might just choose the former without any
consideration, leaving it all in the hands of the compiler.

I'm just more curious as to what I'd get for a "Yes or No" answer to
this from people. Would you do a simple R-value expression:

return 3*x - 2;

or would you attempt to do individual operations on the argument:

x *= 3;
x -= 2;
return x;

You chose a poor example. There's no advantage in breaking down such
a trivial calculation into three separate statements. This really has
nothing to do with whether modifying a parameter object is a good
idea; it's just a matter of writing clear code.

What you *meant* to ask, I think, is whether it's a good idea to use a
function parameter as an ordinary modifiable object. And the answer
is, it depends. If it makes the code clearer, go ahead. If it
doesn't, don't.

There is something to be said for leaving a parameter with the
original value that was passed in, so the name means the same thing
throughout the function. But it's not a hard and fast rule.

Declaring a parameter as "const" is a bit tricky, since it's
meaningful only inside the function, even though it's visible to
anyone who looks at the function declaration. (I'm not sure whether
you can legally use "const" on a parameter in the definition, but not
in a separate declaration.)

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Simple question on Pointers
    ... Such a member is, of course, bad idea. ... const SomeClass, this method exhibits undefined behavior, but you won't ... your completely unambiguous declaration that m_iMember is, in fact, constant ...
    (microsoft.public.vc.language)
  • Re: reference trouble in double[3] vs. double*
    ... > class Triplet { ... You should have a declaration in the commented-out section like this: ... Please note that a const member function cannot return a non-const ... If you need to provide non-const access too, ...
    (comp.lang.cpp)
  • Re: Making C better (by borrowing from C++)
    ... Delaying definition of a variable until the point where definition ... A compiler can always determine if a variable has const semantics ... able to wrap the declaration into a scope. ... declaration by about an order of magnitude. ...
    (comp.lang.c)
  • Re: Use of the Volatile keyword for a pointer to a volatile memory block
    ... > volatile', as they are basically handled the same way) always appear after ... > leftmost side of a declaration they apply to the thing on their right. ... > volatile pointer to an const double you would use ... specifiers aren't saying that we have a double const (what's that, ...
    (comp.lang.c)
  • Re: F77,F90 Namelist input of logical variables
    ... rather than by stating a general rule. ... use an entity in a declaration or attribute specification before the ... done in separate statements). ... no deficiencies and the other way is to make it so complicated ...
    (comp.lang.fortran)

Loading