What use do you have in using constants over variables?

  • Thread starter amun25dringer11
  • Start date
A

amun25dringer11

I hear people talking about like:
const float PI=3.14;
that
and they say you can only change it in one place
but why do you need it if you can use it in a variable
ex.
float PI=3.14;
and then I can assign it to another float;
for(int pi=PI;;pi++)
and if you change the value of PI then the value of pi will change as
well;

Can you please tell me what uses for constants I am missing?
 
M

Matthias Buelow

const float PI=3.14;
Can you please tell me what uses for constants I am missing?

You avoid finding some

PI = 3.0; // the Bible says so

piece of code in the netherworlds of your program bowels some day.
 
P

Pete Becker

I hear people talking about like:
const float PI=3.14;
that
and they say you can only change it in one place
but why do you need it if you can use it in a variable
ex.
float PI=3.14;
and then I can assign it to another float;
for(int pi=PI;;pi++)
and if you change the value of PI then the value of pi will change as
well;

Can you please tell me what uses for constants I am missing?

#include <iostream>
float PI = 3.14;

void change_it(float& value)
{
value = 3.0;
}

int main()
{
change_it(PI);
std::cout << PI << '\n';
return 0;
}

Back in the early days of FORTRAN you could do the same thing with
literal constants like 2.
 
J

James Kanze

I hear people talking about like:
const float PI=3.14;
that
and they say you can only change it in one place
but why do you need it if you can use it in a variable
ex.
float PI=3.14;
and then I can assign it to another float;
for(int pi=PI;;pi++)
and if you change the value of PI then the value of pi will
change as well;

And you consider that an advantage?
Can you please tell me what uses for constants I am missing?

Basically, it says that the value won't change. I've had
constants that later became variables, read from a configuration
file. It happens. (But I have my doubts about a program which
decides to make the value of pi configurable.)

If the type is integral, and the initialization expression is
also constant, there are significant differences; the constant
is an "integral constant expression", which is required in a
certain number of cases: the dimensions of a C style array, as a
non-type argument for a template, etc. In such cases, the use
of "const" goes beyond being simply a compiler enforced
documentation; it plays an important role in the language
itself.

Also, const is part of the type system. In the case of class
types, in particular, it plays an important role in things like
overload resolution. Thus, for example, if I have:
std::vector< int > v1 ;
std::vector< int > const v2 ;
v1.begin() and v2.begin() have different types.

Finally, in cases where the compiler can see the actual object
declaration, const can play an appreciable role in optimization.
This is probably most important for constants of integral types
as well, but I suspect that most compilers can also benefit from
floating point constants: if you write something like 2*pi, and
pi is a constant, the compiler will fold the expression,
replacing it with the results. If pi is not a constant, the
compiler will probably have to read it and do the multiplication
each time it encounters the expression.
 
J

Juha Nieminen

Can you please tell me what uses for constants I am missing?

Constants have basically two purposes: They are small guards against
bugs, and sometimes the compiler can perform certain optimizations when
using constants which it can't do with non-constants. (Of course there
is also the few cases where a constant is required as per C++ syntax,
such as for example when defining the size of an array.)

Personally I *always* use 'const' with everything which is not
intended to be changed. I use it even with temporary variables which
contain temporary calculation results inside functions. Basically what
I'm saying to the compiler with that is "this is something I don't want
to change, if I accidentally attempt to do so, tell me". Usually if an
attempt to change a constant is made, that's a programming error made by
mistake, and it's much better to catch those at compile time than at
testing. Better use 'const' by default, and remove it later if you
really need to change it after all, than the other way around.

Besides this, the compiler can sometimes generate faster code when
variables which don't change have been declared const, which is always a
plus, and certainly doesn't hurt. Basically you are getting faster code
by making your code cleaner and safer. The best of both worlds.
 
M

Matthias Buelow

Juha said:
Basically you are getting faster code
by making your code cleaner and safer. The best of both worlds.

In principle, I agree but just for completeness, the more constraints
you add, the less flexible the code becomes, which can induce headaches
later on, that's something not to be omitted from the discussion. It's
not a simple "win-win" situation in the general case.
 
J

Juha Nieminen

Matthias said:
In principle, I agree but just for completeness, the more constraints
you add, the less flexible the code becomes, which can induce headaches
later on, that's something not to be omitted from the discussion. It's
not a simple "win-win" situation in the general case.

I'm not sure that's really a problem. What consistently using 'const'
(with local variables inside functions) does is self-documentation: If
a variable has been declared 'const', you can see directly from the
declaration that this variable is not changed later in that function
(and thus isn't eg. a counter or anything like that). If it doesn't have
'const' it means that the variable *is* changed later in the function,
so you can look forward to that. (Of course, as said, this requires a
consistent use of 'const' everywhere.)

If the function implementation needs to be changed so that a variable
which is marked as 'const' needs to be changed after all, the 'const'
can be removed. This is basically like changing its comment. Now it says
"this variable is changed during the execution of this function".

IMO if this is done in a consistent and meticulous way, it helps
reading the implementation of the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top