SmallBASIC GuideThe language | Programming Tips | Commands | System | Graphics & Sound | Miscellaneous | File system | Mathematics | 2D Algebra | Strings | Console | Glossary |
Using LOCAL variables
When a variable is not declared it is by default a global variable.
A usual problem is that name may be used again in a function or procedure.
FUNC F(x)
FOR i=1 TO 6
...
NEXT
END
FOR i=1 TO 10
PRINT F(i)
NEXT
In this example, the result is a real mess, because the i of the main loop will
always (except the first time) have the value 6!
This problem can be solved if we use the LOCAL keyword to declare the i
in the function body.
FUNC F(x)
LOCAL i
FOR i=1 TO 6
...
NEXT
END
FOR i=1 TO 10
PRINT F(i)
NEXT
It is good to declare all local variables on the top of the function.
For compatibility reasons, the func./proc. variables are not declared as 'local' by default.
That it is WRONG but as I said ... compatibility.
[Prev] [Next]