SmallBASIC GuideThe language | Programming Tips | Commands | System | Graphics & Sound | Miscellaneous | File system | Mathematics | 2D Algebra | Strings | Console | Glossary |
Units (SB libraries)

* Linux ONLY for now * Units are a set of procedures, functions and/or variables that can be used by another SB program or SB unit. The main section of the unit (commands out of procedure or function bodies) is the initialization code. A unit declared by the use of UNIT keyword.
UNIT MyUnit
The functions, procedure or variables which we want to be visible to another programs must be declared with the EXPORT keyword.
UNIT MyUnit
EXPORT MyF
...
FUNC MyF(x)
...
END
*, Keep file-name and unit-name the same. That helps the SB to automatically recompile the required units when it is needed.} To link a program with a unit we must use the IMPORT keyword.
IMPORT MyUnit
To access a member of a unit we must use the unit-name, a point and the name of the member.
IMPORT MyUnit
...
PRINT MyUnit.MyF(1/1.6)
Full example: file my_unit.bas:
UNIT MyUnit

EXPORT F, V

REM a shared function
FUNC F(x)
    F = x*x
END

REM a non-shared function
FUNC I(x)
    I = x+x
END

REM Initialization code
V="I am a shared variable"
L="I am invisible to the application"
PRINT "Unit 'MyUnit' initialized :)"
file my_app.bas:
IMPORT MyUnit

PRINT MyUnit.V
PRINT MyUnit.F(2)


[Prev] [Next]