Scope
Scope refers to where a variable is visible and accessible in your code.
Global Variables
Variables declared outside any function or subroutine are global — they are accessible from anywhere in the program, including inside functions:
score = 0 ' global variable
Sub AddPoints(n)
score = score + n ' can read and write the global
End Sub
AddPoints(10)
Print score ' → 10
Local Variables
Variables declared inside a function or subroutine with Dim are
local — they exist only while that function is executing and are
invisible outside it:
Sub MyFunc()
Dim temp
temp = 42
Print temp ' → 42
End Sub
MyFunc()
' Print temp ← would cause an error; temp doesn't exist here
Best Practices
Prefer local variables for intermediate calculations to avoid accidental name collisions.
Use global variables sparingly — typically for game state (score, lives, level) that many parts of your code need to read.