97 of 158
91 The syntax for defining external routines is very similar to defining a ZBasic subroutine or function as shown below. Declare Sub <name> ( [<parameter-list>] ) Declare Function <name> ( [<parameter-list>] ) as <type> Parameters of the external routines may be given a default value as described in Section 3.20. In cases where the external routine name is a ZBasic keyword or is not a valid ZBasic identifier, you can use the Alias clause to specify the actual name of the external routine as a quoted string while using the specified <name> to refer to it in ZBasic code. Declare Sub <name> ( [<parameter-list>] ) Alias "<ext-name>" Declare Function <name> ( [<parameter-list>] ) as <type> Alias "<ext-name>" Note that, with the exception of the String type, ZBasic data types are generally compatible with their similar counterparts in C and assembly language. To pass a string to an external routine, it is recommended that the string’s characters be copied to a Byte array and then pass the array by reference to the external routine. Depending on the requirements of the external routine, the length of the string must also be passed or the string’s characters must be null-terminated in the Byte array. Example Declare Sub extSub(ByVal ival as Integer) Call extSub(5) The Declare keyword may be preceded by Public or Private. If neither is present, the declaration defaults to being public meaning that it will be visible in other modules. The primary use for declaring a private external routine is when you define a routine using inline C or assembly code in the same module. Similarly, external variables may be declared so that they may be accessed in ZBasic code. Declare <name> As <type> [ Alias "<ext-name>" ] Likewise, for an external array: Declare <name>(<dim-spec-list>) As <type> [ Alias "<ext-name>" ] Making these declarations does not result in any variable space being allocated. It is up to you to provide additional code to make the external variables available. As with subroutine and function declarations, variable declarations may be Public or Private. In the absence of either attribute, a data declaration defaults to being private. For external variables that are to be treated as constant, you append the Constant attribute to the declaration. With this attribute, the compiler will generate an error if you attempt to modify the variable or to pass it to a subroutine or function by reference. Note, also, that the external variable should actually be defined with the const attribute if it is defined in C code. Example Declare index as Integer Attribute(Constant) If the external variable declaration has both an Alias and and Attribute specified, they may occur in either order. For compatibility with the Declare statement in Visual Basic, the Alias specification for an external subroutine or function declaration may be placed between the routine name and the opening parenthesis of the parameter list. The form shown above, however, is the recommended syntax because it is more consistent with the other uses of the Alias keyword.