5. Variables, Scope, Global, Local

A variable is a location in memory that holds one or more values. That location is given a name by the programmer. The software code uses this name to modify the contents of the RAM location(s) being referenced.

The simplest type of variable is a single value such as

z = 1;

A more complicated variable uses a number of memory locations to hold its data.

For example an array variable would have many RAM locations to hold its entire data set.

Or consider a complicated data structure such as

myDetails {
       name: String
       Age: Integer
       Married: Boolean

So variables can be single valued, or use a number of contiguous memory locations such as an array or the most complex ones having multiple data types making up the data structure.

Another important concept to understand is how 'visible' that variable is to the software code. This visibility is called its 'scope'

Global Variable

This type of variable is available for any part of the code to read and modify. This is both a good thing and a bad thing.

Good: No need to pass its value as an argument or parameter to subroutines, so very efficient in terms of memory. Subroutines can change or read its value directly.

Bad: Any subroutine can change the value of a global variable, so plenty of chances to get it wrong and worse still it can be very difficult to find exactly which bit of code changed its value at any given time. Because of this, it is generally frowned upon to use global variables in complicated programs.

Local Variable

This type of variable can only be read or modified by the subroutine itself. Hence the word 'local'. For example a subroutine might need some local storage to process data

procedure CalculateMyProfit(GrossMoney, TaxDue) {
     var TaxRate : Number
     TaxRate = 25%

In this case a local variable called TaxRate is of Number data type. The next line sets its value and then some further processing code is carried out.

The point is, the variable TaxRate only has a value as long as the subroutine is running. As soon as the subroutine exits, that RAM location containing TaxRate is freed and is of no relevance to any other part of the program.

Good: It cannot be affected by any other part of the software and so it is easy to find a problem within the subroutine

Bad: It does depend on the memory location being freed after use. If this 'garbage collection' process fails then 'memory leaks' happen that eventually chokes the computer (i.e. consumes more and more memory until it stops completely).


A small clip:

global variable X;

function A() {
   var y
   y = 10
   X = 2 + y;

In this clip there is a global variable called X. The subroutine / function name 'A' is called. This uses a local variable ' y' and adds 10 to the global variable 'X'.

Key terms to remember

  • Variable
  • Global variable
  • Local Variable


Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: variable scope



Copyright © www.teach-ict.com