473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

What use do you have in using constants over variables?

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?
Dec 5 '07 #1
4 1376
am*************@gmail.com wrote:
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.
Dec 5 '07 #2
On 2007-12-05 06:31:20 -0500, am*************@gmail.com said:
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 5 '07 #3
am*************@gmail.com wrote:
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.
Dec 7 '07 #4
Juha Nieminen wrote:
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.
Dec 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
1
by: 2obvious | last post by:
I want to declare some constants on the application level in global.asax to use throughout my application, e.g.: Sub Application_OnStart() Const NUM As Integer = 5 End Sub Problem is, when I...
5
by: Amelyan | last post by:
I am struggling here trying to determine what is a good programming practice as far as referencing your URLs. When you use Response.Redirect, do you use 1) Hard-coded string --...
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
9
by: Jacek Dziedzic | last post by:
Hi! I often find that my programs need to store information on "current mode of something" with two or at most several mutually exclusive "modes" to choose from, e.g. - datafile: is it in a)...
6
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
3
by: mattyizzo | last post by:
Here is my code. Basically I'm trying to solve for CI and CF, where each use a bunch of constants and a variable or two which can have up to 10 values. I get the following error, however, at line...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
1
Dormilich
by: Dormilich | last post by:
Hello, I'd like to collect some opinions about the question: in which data type (variable or constant) it is best to store passwords for a database? (I'm using PHP/MySQL but the matter should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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...
1
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...
0
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.