function[[c]][functype] [a b ... [x] [y] ...]
functype - function's return type (type of value, that function returns). Default - int.
a, b ... - arguments. Syntax for single argument: [argtype]argname . If argument type is int, argtype can be omitted.
[x], [y] ... - optional arguments.
c - function has __cdecl calling convention. This is used for C callback functions. Default: __stdcall.
This statement defines user-defined function's type and arguments. It must be first statement (only comments can precede) in the function's text. This statement is optional. If not used, function's type is int, and it cannot receive arguments.
Arguments are local variables. When calling the function, they receive values that are passed to the function. Arguments in square brackets will be optional. When calling the function, optional arguments can be omitted and will be 0.
If an argument is declared as pointer or reference, you must pass address of a variable of that type. Then function is able to modify that variable. This also can be used to pass str variables, arrays, (to avoid copying of whole string or array) and variables of user-defined types. To get address of a variable, prepend operator &. If an argument is declared as reference, operator & is optional. Some functions may also accept 0 for pointer/reference arguments (usually, for optional arguments).
Arguments of type byte* can receive pointers of any type, also str, lpstr and interface pointers of any type. For arguments of other types, must be passed values of same or compatible type (string to string, numeric to numeric, etc). If type casting is required, use operator +.
Functions can by called by other functions or macros, or they can by called by Windows as callback functions. If a function is started in some other way (not called from code), it cannot receive arguments, and function statement only declares local variables that all are 0. However, functions (and even a macros) can receive arguments when started using command line or mac.
See also: Functions Variables Pointers
Examples are given in pairs. The first example in a pair is function's text. The second one is how it can be called (code in another function or macro). Assume that function's name in all examples is "Func".
Example 1:
Function accepts 2 int arguments and returns value of type int. function# a b ret a+b/2 Call it. Pass values 10 and 20. Assign function's return value to variable i. i = Func(10 20)
Example 2:
Function accepts 2 arguments (lpstr and str reference) and returns value of type int. function# lpstr'file str&sr if(!&sr) end "invalid argument" sr.from("$Desktop$\" file) sr.searchpath() ret s.len Call it. Pass string constant and address of str variable. If function returns nonzero value then print s, else print error-string. str s if(Func("my file.txt" &s)) out s else out "file not found"
Example 3:
Function accepts 2 arguments (byte pointer) and returns value of type int. Function is called using __cdecl calling convention. function[c]# !*a !*b if(a[0]>b[0]) ret 1 if(a[0]<b[0]) ret -1 If function returns not through ret, return value is 0. Here we used type declaration character to define types of arguments. It is equivalent to function[c]# byte*a byte*b . Here we don't call it directly, but call dll function qsort, which calls it as callback function. str s="New York" qsort s s.len 1 &Func out s Now s is " NYekorw" (sorted characters).