views:

155

answers:

3

What is a variable?

This question is important for philosophical reasons.

+5  A: 

Wikipedia defines it as:

a symbolic name associated with a value and whose associated value may be changed

I would say this is a very solid generic definition for a variable. If you want to expand it slightly, you could add that the value is represented by a block of system memory, but this reduces the generalness somewhat.

I'm not sure you're going to get any deeper a definition, if you were hoping for that...

Noldorin
That would suggest that a "readonly variable" doesn't make sense... which may be philosophically true, but isn't as practical as a definition which allows it.
Jon Skeet
@Jon: That's a good point. I would still argue that a "readonly variable" is fit by this definition, however; perhaps a more accurate name would be an "assign-once" variable. In this sense, it may still be *changed*, though only once.
Noldorin
Who is your daddy?
quosoo
I think that the first half of the definition alone is best: "a symbolic name associated with a value". Whether or not the value is mutable really doesn't matter (especially when you consider that pure functional languages adopt a more algebraic interpretation of "variables").
Andrew Hare
Erm........... (spam?)
Noldorin
@Andrew: But then how would you differentiate a variable from a constant? Or would you call them all variables?
Noldorin
@Noldorin: Do you mean a C# constant? If so I really think of those as literals since the compiler replaces all instances of constants with their literal values. If you are talking about C++ constants then I see "constant" as a *qualifier* of "variable" (as in "constant variable") since C++ considers variables to be mutable (consider that functional languages often use the term "mutable variable" which makes sense considering that most functional languages considers variables to be immutable).
Andrew Hare
Oh, and +1 by the way :)
Andrew Hare
@Andrew: Now you're getting into the philosophical aspects of this question. :) I would be inclined to agree with you actually when you say "constant" is a qualifier. It might be best to abandon the everyday sense of the word "variable" in discussing variables in the context of programming.
Noldorin
@Noldorin: Haha - we *must* be getting philosophical if we are abandoning the use our term while we try to define it! :)
Andrew Hare
@Andrew: Heh, indeed... (Though I think in actuality we're just making distinctions between its uses.)
Noldorin
A: 

In C#, you'd declare the What variable as:

object What;

Your declaration seems to be from some kind of English-like programming language. Does the ? indicate nullability?

</lameAttemptAtHumour>

David M
Maybe some variant of LOLCODE? I HAS A VAR, CAN HAS STDIO? :P
Thorarin
+3  A: 

Osborn's Law: "Osborn's Law: Variables won't; constants aren't." :)

Wikipedia has entries for both programming variables and mathematical variables

Variables in most programming languages are mutable - they are buckets that can hold a piece of information with a symbolic name for a period of time.

In functional languages, such as XQuery or XSLT, they cannot be changed once assigned. The same is true of final variables in Java, or const variables in most programming languages.

lavinio