5

I'm working on a fundamental programming class in college and our textbook is "programming logic and design" by joyce farrell(spelling?)

Anyhow, I'm struggling conceptually when it comes to initialized variables and named constants.

Our class is focusing on pseudo-code for the time being and not one particular language so let me illustrate what I'm talking about.

Let's say I am declaring a variable named "myVar" and the data type is numeric:

num myVar

now I want to initialize it (I don't understand this concept) starting with the number 5

num myVar = 5

how is that any different than creating a named constant?

5 Answers 5

5

The initialization process may look the same, but a constant cannot be changed once it is set. Depending on the type of variable (private, protected, public), other things, such as the current class, subclasses, or other classes, can act on and may have the ability to change the value of that variable. A constant stays the same for all things using it.

So lets say you initialize a variable to say:

var normalVariable = 10;

Later in your code you could change it to 20, or something, if you wanted. With a constant (which is usually named in all caps LIKE_THIS), you couldn't change the value to something else, you would most likely get an error if you tried.

For the most part, a variable is what it claims to be. It is something that may not always be a certain value. A constant on the other hand would be something that you want to signify as something that will not change. Think gravity or Pi in the real world.

2
  • 1
    Pi is a constant, but gravity (of the Earth, or otherwise) is a ratio. May 31, 2012 at 21:45
  • Haha. My fault. I figured I would get something wrong with that statement. But hopefully everyone gets the gist of it. Jun 1, 2012 at 1:50
4

I'd guess that you're thinking of variables the way we think of them in an algebraic expression, i.e. as something with unknown value that depends on the other constraints in the equation:

x + y = 3

In an equation like that, the values of x and y depend on each other. If you know what y is, you can deduce the value of x. They're called variables here because there are many pairs {x, y} that satisfy the equation.

Variables in programming are fundamentally different. In programming, a variable still represents some value, but the value can change over the course of the program. Think of it as a box that can hold a value -- you can always change what's in the box. Some languages might allow an equation like the algebraic one above, but in most languages you don't get to create open-ended constraints like that (at least not without more work). Instead, you'd say something like:

y = 5;       // assign 5 to y
x = 3 - y;   // y is 5 from the line above, so x becomes -2
x = x - 1;   // it's okay to assign an expression containing a variable 
             // to that same variable, so x now becomes (-2 - 1), or -3

Constants, on the other hand, are just that -- constant values. They're names for values, and those values can't change. We use them mainly to make code easier to understand:

secondsPerDay = hoursPerDay * minutesPerHour * secondsPerMinute;

is easier to understand than:

secondsPerDay = 24 * 60 * 60;
2

The difference is that a named constant is immutable. With a variable you could write:

myVar = 10

And the variable would store the change. If it were a constant though, an error would be thrown.

2

A named constant is, as the name states, unable to be modified. In your example, you can set the value of myVar at anytime during your program's execution. Depending on the language, you can create a constant with the const (C/C++) or final (Java) keywords.

// C/C++
const int CONSTANT = 5;

// java
public static final int CONSTANT = 5;
2

While it is language dependent, it is common practice when you declare a constant for the program itself to never actually make a variable that hold onto that value. Instead, you can assume that if you have a numeric constant set to 5 that it will find every place that constant is used at compile time and replace it with 5 (or whatever). If you create a variable (not constant) and just set it to 5, the compiler can't make that optimization, even if you never change the variable. This means that when the program runs it will need to create a location in memory for that variable, set it, and then when the program runs it needs to fetch the value of that location in memory rather than using a hard coded value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.