GLOBAL variableName {variableName}

The GLOBAL command declares that its arguments are global. That means that its arguments, which are the names of constant or variable vectors, will be accessible directly by name within subroutines (see NEWCMD) without having to be passed through the subroutine's argument list. String variables (which are created by the STRING command) and arrays (which are created by the NEWARRAY command) may also be declared GLOBAL.

Examples of the GLOBAL command's syntax:

NAME (1 2 3 4) clubs spades hearts diamonds
GLOBAL clubs spades hearts diamonds
NAME (1 2 3 4) clubs spades hearts diamonds
GLOBAL clubs
GLOBAL spades
GLOBAL hearts
GLOBAL diamonds

By default, variables and constants declared in the main program, outside a subroutine, are not visible within subroutines. As a result, references inside a subroutine to an outside variable or constant will produce an error message. The GLOBAL command overrides the default behavior for those constants and variables that it lists, making them available by name inside subroutines.

The best way to use the GLOBAL command is to declare only those constants (created with the NAME command) to be global that need to be shared with a subroutine. This makes those constants available to all subsequent subroutines throughout the program and does not cause any "side effects". A side effect of a subroutine is a change to some variable outside the subroutine that is not listed in the subroutine's argument list. A side effect will be introduced if you declare a variable to be GLOBAL and then change its value in a subroutine. Good programming practice suggests eliminating or minimizing side effects.

If you are not using subroutines, then you don't need to use the GLOBAL command. If you are using subroutines, try to use GLOBAL only for NAMEd constants and pass any variables via the subroutine's argument list.

General points about the GLOBAL command:

  • A program may have any number of GLOBAL commands.

  • The GLOBAL command is not allowed inside a subroutine.

  • The declaration of a constant or variable to be GLOBAL must precede the use of that constant or variable in any subroutine.

  • If a name used in a GLOBAL command is not previously defined, the GLOBAL command will define it as a vector. A string variable must be defined by a STRING command prior to its being declared GLOBAL. A NAMEd constant must be defined before being declared GLOBAL. An array must be defined by a NEWARRAY command before being declared GLOBAL.

  • If a dummy argument of a subroutine has the same name as a global constant or variable, then the global will not be visible within the subroutine even though it is declared GLOBAL. This is because the dummy argument name has precedence and will hide a global of the same name. Therefore, it is good practice to avoid using the same name for both a global and a subroutine dummy argument.

  • Redundant GLOBAL declarations are not considered errors. That is, the same name may be declared to be GLOBAL more than once in the same program without causing an error message.

Once an array has been declared by a NEWARRAY command, it can be made GLOBAL by using the array name, without indexes, in a GLOBAL command, like this:

GLOBAL myArray

The entire array is either global or not global. You cannot declare an individual array entry (such as myArray[3]) to be global.

Here's a simple example of a subroutine that computes the log base 10 of a vector. Compare this to the similar example at NEWCMD.

NAME 2.772588722239781 log16  'log base e of 16
GLOBAL log16

'Subroutine to compute log base 16
NEWCMD log16 input result
   LOG input logInput ' log base e of input
   DIVIDE logInput log16 result

'Main program using the subroutine
COPY 1,16 A
LOG16 A log16A
PRINT A log16A

Here's a more elaborate example:

From "Fifty Challenging Problems in Probability" by Frederick Mosteller
"To encourage Elmer's promising tennis career, his father offers
him a prize if he wins (at least) two tennis sets in a row in a 
three-set series to be played with his father and the club champion
alternately: father-champion-father or champion-father-champion,
according to Elmer's choice. The champion is a better player than
Elmer's father. Which series should Elmer choose?"

'(Call the champion "pro".) 
'Assign arbitrary probabilities to the pro and the father vs. the son, 
'with the restriction that the pro's probability of winning is 
'higher than the father's. Create two universes based on those
'probabilities, one for the pro vs. the son and one for the 
'father vs. the son.

NAME sonLoses sonWins
GLOBAL sonLoses sonWins

COPY 7#sonLoses 3#sonWins proGames     'p = 0.7 (pro has higher prob than father)
COPY 6#sonLoses 4#sonWins fatherGames  'p = 0.6 

'Subroutine to determine and score the results of 
'a three-set tournament.
NEWCMD tournament firstGame secondGame thirdGame successCount 
   IF firstGame = sonWins
      IF secondGame = sonWins
         ADD 1 successCount successCount
   ELSEIF secondGame = sonWins
      IF thirdGame = sonWins
         ADD 1 successCount successCount

'Main program
NAME 10000 rptCount
COPY 0 successCount
'Compute probability of set with father in middle
REPEAT rptCount
   SAMPLE 1 proGames firstGame
   SAMPLE 1 fatherGames secondGame
   SAMPLE 1 proGames thirdGame
   TOURNAMENT firstGame secondGame thirdGame successCount
DIVIDE successCount rptCount probFatherMiddle
PRINT probFatherMiddle

'Compute probability of set with pro in middle
COPY 0 successCount
REPEAT rptCount
   SAMPLE 1 fatherGames firstGame
   SAMPLE 1 proGames secondGame
   SAMPLE 1 fatherGames thirdGame
   TOURNAMENT firstGame secondGame thirdGame successCount
DIVIDE successCount rptCount probProMiddle
PRINT probProMiddle