Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


3.1.4 Global and local variables (and procedure parameters)

There are two kinds of variable: global and local. Data stored by global variables can be read and changed by all procedures, but data stored by local variables can only be accessed by the procedure to which they are local. Global variables must be declared before the first procedure definition. Local variables are declared within the procedure to which they are local (i.e., between the PROC and ENDPROC). For example, the following code declares a global variable w and local variables x and y.

DEF w

PROC main()
  DEF x
  x:=2
  w:=1
  fred()
ENDPROC

PROC fred()
  DEF y
  y:=3
  w:=2
ENDPROC

The variable x is local to the procedure main, and y is local to fred. The procedures main and fred can read and alter the value of the global variable w, but fred cannot read or alter the value of x (since that variable is local to main). Similarly, main cannot read or alter y.

The local variables of one procedure are, therefore, completely different to the local variables of another procedure. For this reason they can share the same names without confusion. So, in the above example, the local variable y in fred could have been called x and the program would have done exactly the same thing.

DEF w

PROC main()
  DEF x
  x:=2
  w:=1
  fred()
ENDPROC

PROC fred()
  DEF x
  x:=3
  w:=2
ENDPROC

This works because the x in the assignment in fred can refer only to the local variable x of fred (the x in main is local to main so cannot be accessed from fred).

If a local variable for a procedure has the same name as a global variable then in the rest of the procedure the name refers only to the local variable. Therefore, the global variable cannot be accessed in the procedure, and this is called descoping the global variable.

The parameters of a procedure are local variables for that procedure. We've seen how to pass values as parameters when a procedure is called (the use of WriteF in the example), but until now we haven't been able to define a procedure which takes parameters. Now we know a bit about variables we can have a go:

DEF y

PROC onemore(x)
  y:=x+1
ENDPROC

This isn't a complete program so don't try to compile it. Basically, we've declared a variable y (which will be of type LONG) and a procedure onemore. The procedure is defined with a parameter x, and this is just like a (local) variable declaration. When onemore is called a parameter must be supplied, and this value is stored in the (local) variable x before execution of onemore's code. The code stores the value of x plus one in the (global) variable y. The following are some examples of calling onemore:

  onemore(120)
  onemore(52+34)
  onemore(y)

A procedure can be defined to take any number of parameters. Below, the procedure addthem is defined to take two parameters, a and b, so it must therefore be called with two parameters. Notice that values stored by the parameter variables (a and b) can be changed within the code of the procedure, since they are just like local variables for the procedure. (The only real difference between local and parameter variables is that parameter variables are initialised with the values supplied as parameters when the procedure is called.)

DEF y

PROC addthem(a, b)
  a:=a+2
  y:=a*b
ENDPROC

The following are some examples of calling addthem:

  addthem(120,-20)
  addthem(52,34)
  addthem(y,y)

Global variables are, by default, initialised to zero. Parameter variables are, of course, initialised by the actual values passed as parameters when a procedure is called. However, local variables are not initialised. This means that a local variable will contain a fairly random value when the code of a procedure is first executed. It is the responsibility of the programmer to ensure no assumptions are made about the value of local variables before they have been initialised. The obvious way to initialise a local variable is using an assignment, but there is also a way of giving an initialisation value as part of the declaration (see 10.2 Initialised Declarations). Initialisation of variables is often very important, and is a common reason why programs go wrong.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.