---
title: M-code function override and user M-code functions
slug: m-code-function-override-and-user-m-code-functions
docTags: 
createdAt: 2023-04-18T14:13:13.000Z
---

The system allows M functions in the range of `M1` to `M999`
This means there are many M-Functions that can be user-defined.
The user can create his own M-Function by creating a subroutine for it in the macro file
e.g:

:::BlockQuote
sub m100
&#x9;;Machine-specific code here
&#x9;Msg "custom M100"
&#x9;M54 P1 ;switch on AUX output 1
endsub
:::



So if the G-Code file performs `M100`, the subroutine will be called.
Parameters are possible with syntax such as `M100 S100`.
In the subroutine the parameter can be accessed using [variable](docId\:oqHjs3LLCfQkSac_cemc2) `#19` (`#1` .. `#26` accesses parameter A through Z), S corresponds to no. 19 as in, the 19th letter in the English alphabet.

It is possible to override existing M functions as well.
As an example, additional action can be made during the [M3 command (tool on)](docId\:LCGnVkPx_hp4rq3G5m9QV).

If the machine's spindle has an output that signals that the target speed has been reached, the machine can wait for that output, as an example:

:::BlockQuote
sub m3
&#x20; msg "my customized m3"
&#x20; m3 s#19 ;Inside overridden subroutine this executes the standard M3
&#x20; m56 P1 L2 Q60 ;Wait max. 60 seconds for AUX input 1 to change to low
endsub
:::


When an M-command is called from inside the subroutine that also overrides the same M-command, then the overridden subroutine is not called recursively, but the standard `M3` implementation is executed instead.

There are some limitations to using the M Function override feature:

- There must be no other statements on the same line i.e.
  `N2000 M3 S1000`  is valid.
  `N2000 M3 M8 G1X100` is invalid
- The variables `#1` through `#26` will be used to hold parameter values and will be overwritten when M-Function subroutines are used.&#x20;
  Take this into account and do not use variables `#1`..`#26` in your macro program if you use this functionality.&#x20;
  Variables `#1` through `#26` correspond to the letter of the English alphabet, `#1` will get the value of parameter A, and `#26` will get the value of parameter Z.&#x20;


