The Working Programmer's Guide To Variables - Scope, Lifetime And More
Written by Mike James
Thursday, 22 August 2019
Article Index
The Working Programmer's Guide To Variables - Scope, Lifetime And More
Scope
Visibility

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. This explanation doesn't cover all of the possibilities but enough of them for you to understand the rest.

## What Programmers Know #### Contents

* Recently revised

When you first start programming a variable is introduced as 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 - not to mention the complications of type.

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.

In this introduction to slightly more advanced variables the topic is restricted to idea of when a name is valid and what exactly it means. This topic is often referred to as "scope" but the terminology used to describe variables and how they behave isn't 100% standard so its all the more important to understand the ideas.

## The variable makes us different

The basic idea of a variable is one of the great ideas of computing and it is 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 and the expression.

For example, in many languages you would write an assignment as

`Total=Total+Sum`

and it immediately becomes clear why variables and assignment statements are not like mathematical variables and equations.

If you write X=X+Y in math then the only conclusion you can draw is that Y must be zero!

In math there is no assignment (unless you study one of those areas where it is introduced specifically to explore the sorts of things that go on in programming).

The difference is sufficient to encourage languages such as Pascal, Algol and Ada and more to introduce a special assignment operation that doesn't look like the mathematical equals sign.

That is,

`Total:=Total+Sum`

is an attempt to make it clear that this is an instruction to store the result of adding the "contents" of 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.

It is important to think of a variable as a name that is bound to an entity. A variable is not the same thing as its value as its value can change. A variable is a more abstract idea that is best understood as a name that labels or is bound to some storage.

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 unit of code be it called a module/function/subroutine or whatever - 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.

Last Updated ( Thursday, 22 August 2019 )