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 and several other properties, 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, how to define parameters, 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

are listed in Reference topic. In code they have this color.

User-defined functions

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

 

See also: Function tips

 

To define function's type and parameters, use function statement. To return a value, use ret statement. You can get function address with & operator, and this allows you to use functions as callback functions. You can also start an 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

User-defined member functions belong to classes. They are similar to simple 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 any dll functions (Windows API, C run-time library and other) in QM. To declare a dll function, use the dll statement. For information about a dll function, press F1 and search in MSDN library or Internet.

COM functions

You also can use COM functions.