Table of Contents
decl
declare - a non-executable statement that declares references to SharpBASIC procedures and invokes argument type checking
syntax
decl {sub | func} name [(parameterlist)] * name is the identifier that will be used to call the procedure * parameterlist indicates the number and type of arguments that will be used to call the procedure
details
A procedure's name is limited to 40 (ascii) characters, equivalent to 40 bytes. The parameterlist must match exactly, both the parameter names and types.
In SharpBASIC procedures must always be declared; the compiler will not assume anything in this respect, irrespective of the use of the keyword call. This is different from other and older BASIC languages.
A decl statement also causes the compiler to check the number and type of arguments used to invoke the procedure. decl statements can only appear in module level code (not in a procedure). In the main module, marked by the main statement, decl statements must appear in the prologue section. In other modules and libraries, decl statements must be placed before the actual procedures. A common practice is to use include or header files for declarations and definitions to be reused by different projects.
decl statements can refer to procedures in other modules or libraries. See also the keywords global and extern.
parameters
The parameterlist in a decl statement must match the procedure's header definition exactly. A parameterlist has the following syntax:
variable:type[, variable:type]...
A variable is any valid SharpBASIC identifier name. If the variable is an array, it may be followed by the number of dimensions expressed in comma's, whereby one comma means two dimensions, two comma's means three dimensions, etc:
decl sub saveText(s[,]:str)
'
'
dim text[100, 5]:str;
'
'
call saveText(text[])
The number of dimensions is optional.
Argument types can be any numeric type, a variable length string or a user-defined type (records, structures and enumerations). You cannot have fixed length strings or buffer types in decl statements.
example
The following example declares a function in the prologue section and defines that function in the epilogue section. The function is called in the program's main block.
' SharpBASIC declaration programming example
' ------------------------------------------
incl "lib/sys.sbi";
decl func addone(n:int):int;
dim x, y:int = 2;
main do
x = addone(y);
print("x and y are: " + cstr(x) + ", " + cstr(y));
end
func addone(n:int):int
do
addone = n + 1;
end
Output:
x and y are: 3, 2