Function

See also: function tips, about functions, declaration (parameters etc), programming in QM, class member functions, sub-functions

 

A user-defined function is a macro that can be called from other macros.

 

Differences between macros and functions:

  1. Functions can be called from macros and functions. It allows you to reuse the same code in multiple places: use function's name instead of all the code. A function can receive arguments and return some value. In code, function names have this color.
  2. Like macros, functions also can be started by the user or a trigger. Function can run simultaneously with other functions or macro.
  3. When a function runs, the QM tray icon does not become red.
  4. You cannot stop a running function by pressing Pause key. Use the Threads dialog (in the tray icon menu) or the Running Items list (menu Run -> View Active Items). Also, you can use special code, like ifk(C) break (if Ctrl pressed, exit for or rep loop).
  5. Initial macro speed is 100. Initial function speed is 0 (no autodelays). It can be changed with spe.

By default, functions can be launched by the user or a trigger, like macros. Often you want to prevent launching a function accidentally, because the function is designed to be called from code. Use the Properties dialog, which inserts special line at the beginning of function's text. If the line begins with space and slash ( / ), the function runs only if it is called as function (from macro, other function, or as callback function). If you start it using the Run button, trigger, mac, etc, it does not run. Example (beginning of function's text):

 

 /
function ...
...

 

If function (or macro, or member function) begins with space, slash and name of other item, then, when you press the Run button, runs that item. This is useful when debugging a function that must be called from code. Example (beginning of function's text):

 

 /test
function ...
...

 

When you launch this function, runs macro "test", that may call this function. If backslash ( \ ) is used, it only prevents starting the function when you press the Run button, but the function can be started using e.g. mac or trigger.

 

QM compiles functions and macros on demand.

 

It is possible to change (edit and save) function's code at run time, but changes are not applied while that function (or a function it called) is running. For example, if you edit/save a running function that repeatedly executes code using rep, the changes are applied only when you run the function next time. But if you edit/save a running function that contains a dialog procedure, the changes are applied immediately, because the function is called repeatedly and usually quickly returns.