Functions

A function is a named piece of code executed as a unit. It can receive several values (arguments) and return some value. For example, function win receives window name, finds the window and returns window handle. Beside predefined functions (QM intrinsic functions, dll and COM functions), you can create your own (user-defined) functions. Usually, you create a function when you want to have a piece of code that can be executed more than once, in any macro. Instead of placing the same code in each macro, you place it in a function, and then call the function by name from any macro.

 

See also: Function as QM item type, function tips, declaration (parameters etc), various ways of calling.

Function call syntax

Global functions (intrinsic, user-defined and dll functions) are called using syntax

 

func([a b ...])

 

Here func is function name; a, b and c are arguments.

 

If function's return value is not used, parentheses are optional. Function's return value can be assigned to a variable. Or, function can be an argument of another function or part of an expression with operators. Examples:

 

Func a b
variable = Func(a b)
Func2(a Func(b c))
d = e + Func(b c) / 10

 

 

Member functions of str, OLE types, user-defined classes and COM interfaces are called using syntax:

 

var.Func([a b ...])

 

Here var is variable for which is called function Func. Type of var is type to which belongs function Func. For example, to use str functions, you declare str variable:

 

str s
s.format("%i %i" a b)

 

User-defined and dll functions also can be called by address.

QM intrinsic functions

Reference.

 

In editor QM intrinsic functions have this color.

User-defined functions

A user-defined function is a macro that can be called from other macros. In editor user-defined functions have this color.

 

See also: Function tips, sub-functions.

 

To define function's type and parameters, use function. To return a value, use ret. You can get function address with & operator and use it as callback function. You can also start a user-defined function like a macro (not from code). Functions can be recursive (call itself, directly or through other function). Every running function instance has its own local variables.

 

Below is shown how function is called and executed. Red lines - execution flow direction. Green lines - passing and returning values. The second parameter is declared as reference (&), therefore the function receives address of variable e and can modify its value.

 

User-defined member functions

Class member functions are similar to other user-defined functions, but can be called only with a variable of that type. There are no other ways to execute member functions.

Dll functions

You can use Windows API and other dll functions. To declare a dll function: dll. To find dll function help: type/click its name in editor, then press F1 and search in MSDN library or Internet.

COM functions

You also can use COM functions.