58
Programming with TIDE
©2000-2008 Tibbo Technology Inc.
dim
x
as
byte
' this creates the variable x in the global scope.
sub
foobar(x
as
byte
)
' here we create x once more, in the scope of the
local sub (x as an argument).
dim
f, y
as
byte
x =
5
' right now, only the locally-created x (foobar agrument)
equls 5; global x remains unchanged.
y = x *
30
for
f =
1
to
y
dim
x
as
byte
x =
30
' the argument x outside the for... next statement
still equals 5. only this local x equals 30.
next
f
end
sub
Tibbo Basic supports several scopes:
Global Scope
Every compilation unit has, in itself, one global scope. Variables declared in this
scope are accessible from within any sub or function in this compilation unit.
dim
s
as
string
sub
foo
s =
"foo"
' assigning a value to the global string variable s.
end
sub
sub
bar
dim
i
as
short
i =
0
s =
""
' initialize s, in case it contains anything already (such
as 'foo').
for
i =
1
to
5
s = s +
"bar"
' note
next
i
end
sub
' at this point, s contains 'barbarbarbarbar'.
Local Scope
This is the scope which is between the beginning and the end of each of the
following statements:
Beginning
End
Notes
sub
end sub
Cannot be nested.
function
end function
Cannot be nested.
for
next
while
wend
if... then... else
end if
No exit statement for
if... then... else.
do
loop
Variables declared in this scope are accessible from within the construct in which
they were declared. Local scopes may be nested, for example, for...next scope
inside sub...end sub scope.