Define function parameters and return type

Syntax

function[[c]][functype] [parameters]

 

Parameters

functype - return type. Default - int.

parameters - list of parameters.

c - __cdecl calling convention. Use for C callback functions. Default: __stdcall.

 

Remarks

Defines user-defined function's return type and parameters.

 

It must be the first statement in the function or sub-function. Only comments can be before.

 

This statement is optional if don't need parameters. If not used, function's return type is int.

 

Parameters are local variables. When calling the function, they receive values that are passed to the function (arguments).

 

Parameters enclosed in [ ] are optional. When calling the function, values of unused optional parameters will be 0.

 

If a parameter is declared as pointer or reference, you must pass address of a variable of that type. Then function can 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 parameter is declared as reference, operator & is optional. Some functions may also accept 0 for pointer/reference parameters (usually, for optional parameters).

 

Parameters of type byte* can receive pointers of any type, also str, lpstr and interface pointers of any type. For parameters 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 as callback functions. If a function is started using some other way (Run button, trigger, etc), usually arguments are not passed, and values of all parameters are 0. Arguments can be passed with mac and command line.

 

See also: about functions, Function as QM item type, function tips, various ways of calling, variables, pointers.

 

Examples

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 parameters 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 parameters (lpstr and str reference) and returns value of type int.
function# lpstr'sFile str&sr
if(!&sr) end "invalid argument"
sr.from("$Desktop$\" sFile)
sr.searchpath()
ret s.len

 Call it. Pass string constant and address of str variable. If function returns nonzero value then show s, else show error-string.
str s
if(Func("my file.txt" &s)) out s
else out "file not found"

 

Example 3:

 Function accepts 2 parameters (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 parameters. Same as 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).