I think it took me three years to understand what a variable was.
And I still don't know why it took me so long to understand and why I suddenly understood it.
It's not that I didn't know that assigning '1' to 'a' would result in 'a' having a value of '1', but I didn't understand the concept and workings behind it. I just thought it was magic.
I still remember when I was about 8 or 9 years old, learning Basic, and not understanding why I couldn't do something like this:
10 LET X + 2*Y = 5
20 LET X - Y = 10
30 PRINT X
(Yes, now I know about Prolog).
The only person around me who knew anything about programming was my grandfather, but when I asked him how to get user's input, he began explaining something about interrupts (he only programmed mainframes and got out of the field in early 80s), which left me even more confused and believing that this is just too advanced for me.
There's something interesting here too in that what many call variables are actually a bit more like "assignables". The upshot is that only in programming do variables behave this way---distinct and unlike mere "names" which we're more familiar with from day-to-day life.
So often one "learns (programming) variables" in how they're implemented instead of merely what they mean. Their meaning is much more hairy than mere naming.
Variable to me means 'can change', Constant to me means 'can't change'. It's the mathematical way to use variables in a way that confuses me, though I can see how if you only use 'constant' to refer to an entity like Pi can make sense.
Programming and Mathematics have a lot in common but it would be a mistake to take all your knowledge about terminology from one domain and apply it un-thinkingly to another.
A variable in algebra is exactly what you consider a constant in a programming language. If I say "x = 5" in algebra,that means "x represents the value 5" (and I can substitute one for the other anywhere). The variable x can't suddenly represent the value 6 halfway through my calculations. Variables in Racket and Erlang work exactly as in algebra: single-assignment binding.
> A variable in algebra is exactly what you consider a constant in a programming language.
I disagree. A constant is not expected to change, a variable is absolutely expected to change.
The difference is that a mathematical variable changes "between invocations" of a mathematical statement. An "assignable" changes "within invocations".
"Variable" in math doesn't mean change, it means that the relation a variable is involved in holds over any variation of valid denotations of that variable. So the notion of variance is external to the proposition, whereas in programming the variation is internalized.
(Technically we ought to talk about variables assigned in "for all" style or "there exists" style bindings. There's still a sense of holding under all variations, but the "thing" that "holds" changes.)
On the other hand, constant is different in each domain. Pi, the constant, is emphatically not a variable in mathematics.
Actually, the brunt of the confusion is not the variable, but the '=' sign, which in mathematics means 'is equal to', while in a programming language means 'assign to'. This indirectly changes the semantics of the variable within the statement, and confuses people.
This is why `x + 5 = 10` makes sense in mathematics, but not in a programming language.
"In mathematics, the equals sign can be used as a simple statement of fact in a specific case (x = 2), or to create definitions (let x = 2), conditional statements (if x = 2, then...), or to express a universal equivalence (x + 1)2 = x2 + 2x + 1."
In most programming languages, the equals sign is reserved only for definition.
If you wanted to be explicit about this in maths, you can use := and I think that notation would solve a lot of beginner and early programmer problems.
I think it took me three years to understand what a variable was. And I still don't know why it took me so long to understand and why I suddenly understood it.
It's not that I didn't know that assigning '1' to 'a' would result in 'a' having a value of '1', but I didn't understand the concept and workings behind it. I just thought it was magic.