What does the M97 code do?
The M97 CNC code is a subprogram call by line number.
This means that the machine will run a subprogram contained within the main program.
The machine will jump to the line number in the current program that is called out with the M97 code.
At the end of the subprogram, the M99 code will be used. This signifies the end of the subprogram.
Once the machine reads the M99 code, it will return to the line in the program after the initial M97 code.

Ready to master CNC programming?
Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!
Other codes used with the M97 code
- P – Selects the subprogram number to run. When used with M97, the P code selects the line in the current program to run
- N – Identifies the line number
- M99 – Ends the subprogram, returns to the line after where subprogram was run
- L – Number of times to repeat the subprogram
Format for using an M97 code
Here is an example of an M97 code in use:
M97 P1234 L5
This line of code will tell the machine to move to line N1234 of the current program.
The L code tells the CNC machine to run the subprogram five times.
The L code can be left out if the subprogram will only be run once.
Line 1234 will be after the program end command (M30).
Once the machine reaches the M99 code it will return to the line after the example shown above.
What are subprograms used for?
Subprograms are a separate CNC program selected to run from within another program.
They are used to perform repetitive machining operations or sequences such as drilling, counterboring and countersinking a hole.
They can also be used for machine related functions such as preparing for a tool change.
CNC machines run the lines of code in a program in order.
Using subprograms allows the programmer to jump around to different sections of the current program or to run a different program and come back to the current program.
Differences between a subprogram and the main program
The commands to end the main program and a subprogram are different.
The main CNC program ends with either M30 code (for most machines) or M02 (for older machines). No further code will be run after either of these codes.
A subprogram ends with the M99 command.
When using the M97 command, M99 returns the machine to the line which called the subprogram. The machine will continue to run code in the program from that point.
Let’s look at that program flow again to see how the machine moves from the subprogram call (M97) to the subprogram and back to the main program after the M99 code.

Benefits of using a subprogram
The greatest benefit of using subprograms is reducing the lines of code which makes the program easier to read and edit.
Subprograms can also reduce the number of errors in the program. Less lines of code means less possible sources of problems.
Drawbacks to using a subprogram
Subprograms are meant to make the program easier to work with for both the programmer and anyone using it. If not done correctly, they can have the opposite effect.
Programmers should remember that part of their job is making sure that the program is easy to use for the operator.
It can also be easy to have the wrong modal commands or offsets chosen when starting or ending a subprogram.
Following a good program format that uses safety lines or blocks of code can protect from this happening.
If it is important that a mode or offset is needed in a specific section of your program, it is best to use the necessary code to make sure things are set correctly.
Assuming your machine is in the correct mode already is dangerous. Set the correct modes when you need them.
Making subprograms inside your subprograms (nesting) also has the potential to cause confusion. Repeatedly switching between programs can be confusing for both programmers and operators.
M97 vs M98
There are two ways to run subprograms: M97 and M98.
The difference between M97 and M98 is the program location they move to.
M97 will jump to a new line in the current program.
M98 will run an entirely different program.

Frequently asked questions about the M97 code
How do modal commands work in subprograms?
Modal commands work the same way as they do in the main program. This means they stay on until changed or turned off.
If a modal command is on when starting the subprogram, it will stay on while running the subprogram. The same is true when switching from the subprogram to the main program.
The safest thing to do is make sure your safety blocks cover the required codes for each section of code.
Ready to master CNC programming?
Try the free 30 minute intro course to see how simple and easy G code can be. Take the shortcut to becoming a G Code Master today!