Page 1 of 3
There is more to the humble variable than meets the eye. We look at scope, global and local.
Many programmers are confused by the range of variations that there are on the humble variable - mainly because the idea is so basic that we just "pick it up" as we go along.
When you first start programming a variable is something that you store data in until you need it again.
Later on a variable becomes something much more complicated with static, dynamic, local and global modifiers that can be used as part of the declaration.
Thinking about how variables can be implemented leads on to a more general topic of names and how they are bound to the things that they represent. This is an important topic that is central to object oriented programming.
The variable makes us different
The basic idea of a variable is one of the great ideas of computing and what distinguishes it from mathematics.
In computing a variable is a name bound to an area of storage. The principle method of manipulating a variable is the assignment statement. For example, in many languages you would write an assignment as
and it immediately becomes clear why variables and assignment statements are not like mathematical variables and equations.
If you write X=X+Y in maths then the only conclusion you can draw is that Y must be zero!
The difference is sufficient to encourage languages such as Pascal, Algol, C and Ada and more to introduce a special assignment operation that doesn't look like the mathematical equals sign.
is an attempt to make it clear that this is an instruction to store the result of adding Total and Sum back into Total.
Notice that the real difference between math and computing is that the assignment statement has time built into its interpretation. The "new value is the old value plus sum".
If you find this obvious you need to recall that functional programming is an attempt to remove or at least lessen the split between math and programming. In functional programming assignment is a one time operation - you can't reassign to the same variable and instructions like a=a+1 are illegal. Yes it can be done - but many think it's artificial and simply hiding the fact that programming is different.
This description of a variable and assignment is more or less what every programmer ends up understanding in a practical sense. If you work with variables you can't help but think about them as named units of storage even if you don't know how the underlying hardware works with them. However, as you carry on learning and programming you slowly start to realise that variables are slightly more complex. For example, if you use a name in more than one module/function/subroutine is it the same variable and if you leave a module/function/subroutine do all of the variables carry on existing and will they have the same values when you return?
These may sound like questions on an exam paper in existential philosophy but an understanding of how variables behave is vital to designing programs that work.
The most important characteristic of a variable is the scope of its name.
Scope refers to the section of the program that a variable name can be relied upon to label the same unit of storage.
For example, if you use a variable called A in a function and a variable called A in another function- it is clear that they share the same names but do they share the same values?
If the scope of the name A includes both functions then they do share the same values. If the scope of each name is restricted to the modules then they are different variables that just happen to have the same name.
One of the problems at this point in the discussion is that some programmers will never have encountered the idea of scope because some languages don't do a good job of supporting it. For example in most scripting languages the natural way to work is to have every variable that you define available for use throughout the program. In this case the variables are said to be "global" and the scope of a global variable is the entire program. Just to be different some languages call global variables "public" variables.