Jump to content

Constants - what's the point?

- - - - -

  • Please log in to reply
9 replies to this topic

#1
justanothernoob

justanothernoob

    Newbie

  • Members
  • PipPip
  • 22 posts
Another very basic question I'm sure, but I don't understand the point of constants.

At first, I figured they would just keep a value, but the value could change, such as
const int theconstant = num1 + num2
and whenever you referred to theconstant it would give you the value of num1 + num2 even if the value of those variables had changed.

Well I decided it's about time I should look it up and learn more about constants, but it's looking like they're not as useful as I thought. I looked through a few of my programming books and it just explains constants as an integer or something that doesn't change, and they both give the example of pi=3.14.

This doesn't seem useful, as you can just make an int named pi and not change it. I have a feeling I'm not understanding something. Maybe my first guess was right?

If anyone could explain the purpose of them I'd appreciate it.

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Adding the "const" keyword before a variable ensures that it cannot be changed at compile time. This avoids errors if somebody accidentally tries to change it in their code. Their code won't even compile in those cases. If you have a value that you know is a constant, it's much better to catch the errors at compile time than to introduce a whole lot of logic errors after the value gets inadvertently changed.

It may be easy for you to remember which of your own variables you are treating as constant, but in a team project, where others may not know your code as intimately as you do, this good coding practice ensures that anyone else who manages your code after you will not make a mistake with one of your constants.

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
This is a guess, so I could be wrong, but a reason it could be setting a constant that can change like so:
const int theconstant = num1 + num2

It's possible you are setting references instead of the value.

"theconstant" would still never change at that point, it would just have to reference num1 and num2 and add them.

Even if you -could- change constant values, you probably shouldn't. It will add clutter and make things over complicated.

#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
When you try that code, you get an error "The expression being assigned to 'theconstant' must be constant".

#5
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts

Momerath said:

When you try that code, you get an error "The expression being assigned to 'theconstant' must be constant".
Sorry, the way he explained it, I assumed he did compile it, and did change the value of it. I'm not that familiar with C#.

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
No, if you define something as a constant, it needs to be just that. A constant. It can't depend on anything else.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The point of constants is to avoid having "magic numbers". For example, look at the following code (hopefully close to C# syntax):
float max = 2.075;
float min = -1.384;
float avg = (max+min)/2.0;
float adjusted_range = (max-avg)*0.707;
float adjusted_max = avg+adjusted_range;
float adjusted_min = avg-adjusted_range;

In a case like this, the value 0.707 is a "magic number". It just appears out of nowhere, is not self-documenting, and could confuse something working with the code later. Suppose, further, that you see that same number scattered throughout the code, then see a value of 0.706. Is that 0.706 a typo, or deliberate? What happens if you have to update 0.707 to 0.707107 when it has one meaning, but not another?

If you set two constants in advance:
const float rms = 0.707;
const float reentrant_seed = 0.706;

Now you can use the appropriate names in the appropriate places (rms in the initial code) to clearly identify the intent. In addition, you only have to change a value once to implement a more precise value.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The compiler may rely on the fact the constant will always remain the same, and any references will simply point to that same memory location as an essential optimization.

For example const float a = 3.1, and float b = a may now occupy the same memory address.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
TCristoforo

TCristoforo

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Constants are good for things that only change once from project to project: For example, the name of an app, a version number. It's also good for standard math values, such as PI.

#10
GIT Solutions

GIT Solutions

    Newbie

  • Members
  • PipPip
  • 13 posts
Declare keyword as a constant is good programming practise but only when you need a variable not to be change throughout a class or function. Suppose you want to find the area of circle for that you set the value of pie=3.14 as constant by declaring the variable as constant.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users