Think of functions as mini-programs. By default, any variable you use within a function is local to that function, even if it uses the same name as a variable you used outside. By declaring the variable as global, they are the same variable.
Try this code, first running it as-is, then running it after putting the word 'global' at the start of the first line and see the effect:
a = 1
print "a is "; a; " at the top scope"
MyFunction()
print "a is "; a; " at the top scope"
wait key
end
function MyFunction()
print "Entered the function"
print "a is "; a; " within the function scope"
a = 2
print "a is "; a; " within the function scope"
print "Leaving the function"
endfunction
Thought I don't understand why you don't know what's going on when Delphi/Pascal use almost exactly the same system