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
Objects And Lifetimes

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



  1. The Computer - What's The Big Idea?
  2. The Memory Principle - Computer Memory and Pigeonholes
  3. Principles of Execution - The CPU
  4. The Essence Of Programming
  5. Variables - Scope, Lifetime And More*
  6. Binary Arithmetic
  7. Hexadecimal
  8. Binary - Negative Numbers
  9. Floating Point Numbers
  10. Inside the Computer - Addressing
  11. The Mod Function
  12. Recursion
  13. The Lost Art Of The Storage Mapping Function *
  14. Hashing - The Greatest Idea In Programming
  15. XOR - The Magic Swap
  16. Programmer's Introduction to XML
  17. From Data To Objects*
  18. What Exactly Is A First Class Function - And Why You Should Care*
  19. Stacks And Trees
  20. The LIFO Stack - A Gentle Guide*
  21. Data Structures - Trees
  22. Inside Random Numbers
  23. The Monte Carlo Method
  24. Cache Memory And The Caching Principle
  25. Data Compression The Dictionary Way
  26. Dates Are Difficult*
  27. Sequential Storage*
  28. Magic of Merging
  29. Power of Operators
  30. The Heart Of A Compiler
  31. The Fundamentals of Pointers
  32. Public Key Encryption
  33. Quick Median
  34. Functional And Dysfunctional Programming*

* 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


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,


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 )