Simplest rule is:

Don't declare your vars = GLOBAL
Declare as GLOBAL, and the contents of the variable are visible to all parts of your program, including other functions, and scripts in other files executed by CALL commands.

Declare via DIM, and the contents of the variable are only visible in the segment where they were defined. This DOES include other scripts that are CALLed, but not inside functions.

For example, try this:
 Code:
Global $X
Dim $Y

$X = "X-Var"
$Y = "Y-Var"
 
; Show current contents
"X=" $X ?
"Y=" $Y ?

; Call a function - throw away return value
$ = TestFunc() 

; Show contents after calling TestFunc
"X=" $X ?
"Y=" $Y ?

; Call the function, passing an argument, and store the result in $Y
$Y = TestFunc($Y)
; Show contents after calling TestFunc again
"X=" $X ?
"Y=" $Y ?

; Define the function
Function TestFunc(OPTIONAL $Arg)  
  ; Show contents of vars
  "X=" $X ?      ; this should display the GLOBAL var
  "Y=" $Y ?      ; this should display nothing
  "Arg=" $Arg ?  ; This is passed to func

  $TestFunc = "Func-Val"  ; return data from the function

EndFunction


This simple example shows the relationship of DIM and GLOBAL.

Generally, you want to DIM everything (good practice), pass arguments to functions, and declare as GLOBAL only things that every function should be able to view AND MODIFY!

Glenn
_________________________
Actually I am a Rocket Scientist! \:D