Scope of Variables

When a variable is created, it is scoped to the current context. That mean it is visible to anything in the current scope or greater. The widest scope is the module:

Dim newVar = 12

The next scope is the routine:

Function myRoutine()
    Dim newVar = 12
End Function

When sdlBasic encounters a variable in a routine, it checks to see if there is a variable scoped to the routine of the same name and use that.

Dim myVar = "module variable"     ' This is declared at the module level
Function myRoutine()
   Dim myVar = "routine variable" ' This is declared at the routine level
   myVar = "new value"           ' Use the local routine version of myVar
   return myVar
End Function
print myRoutine()
print myVar

If not, it checks to see if there is a module variable and uses that.
 
Dim myVar = "module variable" ' This is declared at the module level
Function myRoutine()
   myVar = "new value"   ' use the module version of myVar
   return myVar
End Function
print myRoutine()
print myVar
 
If there is no routine or module version of the variable, one will be created in the current scope.

Function myRoutine()
   myVar = "new value"      ' create a variable scoped to the routine
End Function
 
Variables scoped to routines are only visible within the routines that they are scoped to:

Function myRoutine()
   myVar = 12
   print myVar   ' myVar is visible here
End Function

myRoutine()
print myVar  ' myVar is invisible here

You can prevent sdlBasic from creating variables with the Option Explicit statement.
 
With Option Explicit you will need to declare your variables before use:

Option Explicit
Dim newVar = "create me"
 
If you use Option Explicit your module level variables will be hidden from your routines unless you specifically declare them visible with the Shared keyword or if they are declared as Common ( or Global ):

Option Explicit
Dim myVar = "module variable"
 
Function myFunction()
   Shared myVar
   myVar = "new value"
End Function
 
or:

Option Explicit
Dim Common myVar = "module variable"
 
Function myFunction()
   myVar = "new value"
End Function