当前位置: 动力学知识库 > 问答 > 编程问答 >

programming languages - definition of variable

问题描述:

I ended up in a discussion with some friends about the definition of variable with respect to programming.

My understanding is that a variable in programming can be constant or changing.

Their opinion is that the real definition of the word variable is it can change, thus an identifier referring to some value which can change is a variable, where as a set of characters referencing a value which is defined as constant is literally called a constant. i.e.,

Int constant blah

Int argh

Thus by their definition they would refer to blah as a constant and argh as a variable.

My definition is would be the variable blah is constant and argh is also a variable (which is not constant)

Have I been referring to these identifiers incorrectly?

网友答案:

From my experience, it depends who you're talking to. That being said, my definition is * A value is... a value (1, "a", etc) * A variable is a name used to reference a value. It's possible to use multiple names to reference the same value, and for the value referenced by a variable to change over time, but neither is mandatory.

int a = 1;
    ^ variable
        ^ value

The wikipedia link mentioned by Cody Gray reinforces this view, or seems to in my opinion.

If it helps, consider that purely functional languages have variables but, by definition of being a functional language, the values that those variables point at cannot change over time.

It's also worth noting that your definition also depends on the context of your discussion. If you're talking about "variables vs constants", its reasonable to say they're polar opposites. If you're talking about "variables vs values vs keywords", you're talking about a different usage of the word variable (kind of).

As an example, consider fruit vs vegetable. In science terminology, an eggplant is a fruit. In culinary terminology, it's a vegetable. The culinary term vegetable can refer to things, in science terms, are fruits, roots, nuts, and a variety of other things. You need to know the context of your discussion to be able to say whether "x is a fruit" is accurate.

网友答案:

Your friends are correct. Constants and variables are essentially opposites by their definition.

A variable can represent many different values, and the value is unknown when referred to by name.

A constant on the other hand only represents one value at all times, and if you know it's value you can count on it never changing.

Of course in programming languages they are very similar things. They usually follow the same naming rules and can be stored the same way, but, just like variables aren't constants, constants aren't variables.

分享给朋友:
您可能感兴趣的文章:
随机阅读: