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

knowcover

Contents

  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. Advanced Hashing
  16. XOR - The Magic Swap*
  17. Programmer's Introduction to XML
  18. From Data To Objects*
  19. What Exactly Is A First Class Function - And Why You Should Care*
  20. Stacks And Trees*
  21. The LIFO Stack - A Gentle Guide*
  22. Data Structures - Trees
  23. Inside Random Numbers
  24. The Monte Carlo Method
  25. Cache Memory And The Caching Principle
  26. Data Compression The Dictionary Way
  27. Dates Are Difficult*
  28. Sequential Storage*
  29. Magic of Merging*
  30. Power of Operators
  31. The Heart Of A Compiler*
  32. The Fundamentals of Pointers
  33. 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.

 

Banner

 

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 )