TScriptTFunc Declaration |
||||
Homepage TScript TFunc Declaration |
BasicsTScript comes with some built-in functions. But what to do if you need own functions/procedures? For example a function calculating the square root of a number: a := sqrt (2.0); // a -> 1.4142.. To add such a function add a TFunc component to your form. To tell TScript how this function is called and what arguments it takes you have to define the property "Declaration" of the TFunc component: sqrt (float) : float; This declaration string names the function "sqrt" and determines that it takes one argument of type float and returns a value of type float. Possible types are void, boolean, integer, float, string. Unassigned variables or functions without return value are of type void. The syntax of this declaration string is defined on the syntax page. Parameters with Multiple Types In difference to many programing languages it's possible to declare parameters with more than one type: ToStr (integer float boolean) : string; // Valid calls of this function tostr (1); tostr (3.1415); tostr (true); There are three reserved words to shorten often used sets of types: all = integer float
string boolean void Open Types If you don't want to fix the number of parameters it's possible to use "open" types: sum (integer open) : integer; sqrsum (mult open) : float; send (string, string open, integer); concat (integer float string boolean open); // possible calls: i := sum (1, 2, 6, 43, 99, 1234, 34); i := sum (); f := sqrsum (23, 4.5, 9.24343, 1004); send ('ATZ', 'ATDT 004439485785', 'ATH', 12); send ('ATI6', 3); concat (45, 'asdf', 1.2, false); "Open" means zero or more arguments of this type. If you need one or more arguments use a declaration similar to "send". It's possible to declare functions that can't be satisfied by any arguments, because the arguments are compared to the parameters from left to right: // TScript reports "To less arguments" baddeclaration (integer open, integer); baddeclaration2 (mult open, float); But: opendemo (string open, integer open); opendemo ('a', 'b'); // valid opendemo ('a', 'b', 1); // valid opendemo (1, 2); // valid opendemo ('a', 2, 'b'); // invalid! opendemo (2, 'c'); // invalid! Var-, Func-, Array-Types It's possible to restrict the set of valid arguments further. The reserved word "var" means that only variables are valid as arguments. The reserved word "func" means that only functions are valid as arguments. set (var void, all); The reserved word "array" works the same way as in Delphi except that there is no need for the reserved word "of": sum (a : array of integer);
// Delphi |