Subprograms with M98 and M99
A subprogram is a small program that the main program calls, runs and returns from. Use it when the same moves are repeated: several identical grooves, a pattern of holes on several parts in a fixture, or a finishing pass you want to run again.
Fanuc: M98 and M99
M98 P1000 (run O1000 once) M98 P1000 L4 (run O1000 four times)
Many Fanuc controls also accept the repeat count inside P: M98 P41000 runs O1000 four times (the last four digits are the program number, the digits before them the count). Which format your control uses depends on the model and parameters, so check the manual.
The subprogram is a normal program that ends with M99 instead of M30. M99 returns to the block after the M98.

Example: four grooves, 10 mm apart
The trick is to write the subprogram with incremental moves (U and W on a lathe), so each call starts where the last one ended.
(MAIN PROGRAM) T0303 G97 S800 M03 G00 X42. Z0 M08 M98 P1000 L4 G00 X100. Z100. M09 M30 O1000 (ONE GROOVE) G00 W-10. G01 U-6. F0.05 G04 X0.3 G00 U6. M99
Each call moves 10 mm towards the chuck, plunges 3 mm deep (6 mm on diameter), dwells and comes out. Four calls: grooves at Z-10, -20, -30 and -40.
Nesting
A subprogram can call another subprogram. Fanuc controls allow several levels (often four or more). Keep it simple: deep nesting makes programs hard for the next operator to follow.
Okuma OSP and Haas
- Okuma OSP:
CALL O1000 Q4calls O1000 four times, and the subprogram ends withRTS. See Fanuc vs Okuma OSP codes. - Haas: M98 and M99 work like Fanuc. Haas also has
M97, which calls a block number inside the same program, so you do not need a separate program file.
Common mistakes
- Absolute moves inside a repeated subprogram: every call cuts in the same place.
- M30 at the end of the subprogram: the main program stops.
- Subprogram changed for one job and breaks another job that also calls it. Give each job its own numbers.
- Coordinate mode (G90/G91) left changed when returning. Set the mode you need after the call.
Run a new subprogram in single block once: watch that each call starts from the right place.
Calculators, process sheet to program, simulation, codes and lessons. Free to start, no sign-in, on phone and laptop.
Open the app

