>
Which of these functions would you go with?
>
unsigned Func(unsigned const x)
{
return 3*x - 2;
}
>
or:
>
unsigned Func(unsigned x)
{
x *= 3;
x -= 2;
>
return x;
}
Neither. I'd go with this one:
unsigned Func(unsigned int x)
{
return 3 * x - 2;
}
(after renaming Func to reflect the purpose of the function, and replacing
the magic numbers with meaningfully-named symbolic constants).
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.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Jun 7, 8:26 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
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:
>
Which of these functions would you go with?
>
unsigned Func(unsigned const x) {
return 3*x - 2;
}
or:
unsigned Func(unsigned x) {
x *= 3;
x -= 2;
return x;
}
Why are you asking such stupid questions? They only annoy.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
On Sat, 07 Jun 2008 00:59:33 -0700, Tomás Ó hÉilidhe wrote:
>Tomás Ó hÉilidhe wrote:
Which of these functions would you go with?
>
The reason I ask is that the two functions could produce different code
for a microcontroller , one using less memory than the other.
If one uses more memory, and it becomes a problem, use the other one. I
can imagine situations where either is better, so until then, for me,
there's no way of knowing, and no reason for caring.
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;
It depends on the complexity of the function. I have no objection to:
return 3 * x - 2;
but I would draw the line at expressions that were significantly more
complicated and not immediately obvious. I nearly used:
return u * t + (a * t * t) / 2;
as an example of a significantly more complex expression, but decided that
it was blindingly obvious, so I'd actually be okay with it.
As an example of what I *would* consider to be too complex to go in a
return statement, consider this ill-considered (but cool) snippet from "C
Unleashed":
Note also the magic number (sigh). Note, too, the spurious precision, given
that the result is being cast to int. Note, finally, that the cast is
probably superfluous!
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Jun 7, 8:26 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>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_Keit h) kst-u@mib.org <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"
Why are you asking such stupid questions? They only annoy.
>
Something's up with your quoting, I only put { on the same line for
the definition of structs, unions, classes, etc.
Maybe he fixed it himself to suit his style?
The reason I ask is that the two functions could produce different
code for a microcontroller , one using less memory than the other.
And how are the posters here supposed to find out *if* and *which*
implementation and microcontroller would do that?
Since you seem to care only for a single microcontroller , test it
yourself.
P.S. It'd be nice if you changed your name to ASCII characters...
In article <jOmdnbaxJuwX1d fVnZ2dneKdnZydn Z2d@bt.com>,
Richard Heathfield <rjh@see.sig.in validwrote:
>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;
>It depends on the complexity of the function. I have no objection to:
>
return 3 * x - 2;
>
>but I would draw the line at expressions that were significantly more
>complicated and not immediately obvious. I nearly used:
>
return u * t + (a * t * t) / 2;
>
>as an example of a significantly more complex expression, but decided that
>it was blindingly obvious, so I'd actually be okay with it.
There's another question though, which is whether - if you decide to
split up the evaluation - you should re-use the argument variable
or introduce a new one. Generally I would say that you should give
your arguments meaningful names, so it would usually be wrong to
assign some value with a different meaning to them.
>As an example of what I *would* consider to be too complex to go in a
>return statement, consider this ill-considered (but cool) snippet from "C
>Unleashed":
[...]
One reason to write long expressions is when they're copied from
somewhere else, and you want the reader to be able to see the direct
correspondence of your code with the referenced expression. I have
done this for a distance-on-the-earth formula.
-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
On Jun 7, 8:40 am, CBFalconer <cbfalco...@yah oo.comwrote:
>Tomás Ó hÉilidhe wrote:
>>
>>Which of these functions would you go with?
>> unsigned Func(unsigned const x) {
>> return 3*x - 2;
>> }
>>or:
>> unsigned Func(unsigned x) {
>> x *= 3;
>> x -= 2;
>> return x;
>> }
>Why are you asking such stupid questions? They only annoy.
>
Something's up with your quoting, I only put { on the same line for
the definition of structs, unions, classes, etc.
He obviously reformatted it.
The reason I ask is that the two functions could produce different
code for a microcontroller , one using less memory than the other.
The compiler could produce different inlined code for it on two
subsequent calls as well. If you want to know which will be more
efficient on your particular implementation you need to look at your
implementation, but unless you have reason to suspect a problem I would
suggest writing the clearest code which in my opinion is
unsigned Func( unsigned x)
{
return 3*x - 2;
}
I also see no reason for the compiler to produce inefficient code for it.
--
Flash Gordon
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.)
You mean declaring 'void f(const double t);' and later defining 'void
f(double t) {...}'? If so, I'd say yes because of
6.7.5.3 Function declarators (including prototypes)
....
13 The storage-class specifier in the declaration specifiers for a
parameter declaration, if present, is ignored unless the declared
parameter is one of the members of the parameter type list for a
function definition.
On Jun 7, 8:26 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>
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;
I would use return 3*x - 2; because this is you intention. Let
the compiler then optimize resource usage.
>>Which of these functions would you go with?
>>>
>> unsigned Func(unsigned const x) {
>> return 3*x - 2;
>> }
>>or:
>> unsigned Func(unsigned x) {
>> x *= 3;
>> x -= 2;
>> return x;
>> }
>>
>Why are you asking such stupid questions? They only annoy.
>
Something's up with your quoting, I only put { on the same line
for the definition of structs, unions, classes, etc.
>
The reason I ask is that the two functions could produce different
code for a microcontroller , one using less memory than the other.
I was in a bad mood, and I apologize for the response. Also, I
habitually try to remove blank lines and make a quote more compact,
without loosing any facts.
There is no reason to use other than clear source code, unless you
find that the final result is not satisfactory. That is system
dependant, and not answerable here.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
"Tomás Ó hÉilidhe" <toe@lavabit.co mwrote in message news:
>
Which of these functions would you go with?
>
unsigned Func(unsigned const x)
{
return 3*x - 2;
}
>
or:
>
unsigned Func(unsigned x)
{
x *= 3;
x -= 2;
>
return x;
}
>
Generally it is considered bad form to modify an argument to a function. It
makes things a bit more difficult to read.
Comment