---
title: "M98 and M99: Subprogram Call and Return, With an Example"
description: "M98 calls a subprogram and M99 returns from it, letting you reuse a block of code. Here is how the pair works on Fanuc-style controls, with an example."
url: https://gcodepractice.com/journal/m98-and-m99-subprogram-fanuc-example/
canonical: https://gcodepractice.com/journal/m98-and-m99-subprogram-fanuc-example/
author: "Lawrence Arya"
authorUrl: https://www.linkedin.com/in/vibecoding/
published: 2026-06-02
updated: 2026-06-02
category: "Code reference"
tags: ["m98", "m99", "subprogram", "fanuc"]
lang: en
---

# M98 and M99: Subprogram Call and Return, With an Example

> **TL;DR** On Fanuc-style controls, M98 calls a subprogram by its program number (M98 P1000), optionally repeating it, and M99 placed at the end of that subprogram returns to the line after the call. Together they let you write a routine once and reuse it. The exact P and repeat syntax varies by control, so confirm it against your machine manual.

`M98` and `M99` are how you stop writing the same moves over and over. If a part has the same feature in four places, you write the routine once as a subprogram and call it four times. The pair is common on Fanuc-style controls, including many that follow the same conventions.

## How the pair works

- **`M98`** calls a subprogram by its program number. The control jumps to that subprogram, runs it, and comes back.
- **`M99`**, placed at the end of the subprogram, returns control to the line right after the `M98` call.

You can also repeat the call. A common form looks like `M98 P1000 L3`, meaning run subprogram `O1000` three times. The exact way the program number and repeat count are written differs between controls, so this is the place to check your machine manual rather than assume.

## A simple example

```
O0001 (MAIN PROGRAM)
...
M98 P1000 L3   (call subprogram O1000, repeat 3 times)
...
M30            (end main program)

O1000 (SUBPROGRAM)
...            (the reused routine)
M99            (return to the main program)
```

The main program calls `O1000` three times, and each time `M99` sends control back so the main program continues.

## M99 vs M30

| Code | What it does | Where it lives |
| --- | --- | --- |
| `M99` | Returns from a subprogram | End of a subprogram |
| `M30` | Ends and rewinds the program | End of the main program |
| `M98` | Calls a subprogram | In the main program |

Beginners sometimes confuse `M99` (return) with `M30` (end). The breakdown of program-end codes is in [common M-codes for CNC beginners](/journal/common-m-codes-for-cnc-beginners/).

## Where this fits

`M98` and `M99` are a step past the [common M-codes](/journal/common-m-codes-for-cnc-beginners/), the kind of topic a [journeyman machinist test](/journal/journeyman-machinist-g-code-test-questions/) expects. Make the basics automatic first with [beginner CNC code practice](/journal/beginner-cnc-code-practice/), then layer on subprograms. On LinuxCNC, the same job is done with [O-word subroutines](/journal/o-word-subroutines-linuxcnc-examples/) instead of M98/M99. A free tool like [G-Code Sprint](/g-code-practice/) drills the foundational codes. This is an educational overview; confirm exact syntax and behavior against your control's manual.

## Bottom line

`M98` calls a subprogram, `M99` returns from it, and together they let you reuse code. Repeats are added to the call, with syntax that varies by control. Do not confuse `M99` (return) with `M30` (program end).

## Sources

- [Wikipedia: G-code (M98, M99)](https://en.wikipedia.org/wiki/G-code)
- [CNCCookbook: G-code and M-code cheat sheet](https://www.cnccookbook.com/g-code-m-code-cnc-list-cheat-sheet/)
- [FANUC (control manufacturer)](https://www.fanuc.com/)

## Frequently asked questions

### What do M98 and M99 do?
`M98` calls a subprogram by its program number, so the control runs that separate routine and then comes back. `M99`, placed at the end of the subprogram, returns control to the line right after the `M98` call. They let you reuse a block of code instead of repeating it.

### How do you repeat a subprogram with M98?
You add a repeat count to the call, commonly written like `M98 P1000 L3` to run subprogram 1000 three times. The exact way repeats are specified differs between controls, so check your machine's manual.

### What is the difference between M99 and M30?
`M99` returns from a subprogram back to the calling program. `M30` ends the main program entirely and rewinds it. `M99` hands control back; `M30` stops the show.

*G-Code Sprint is a study and practice tool only. It is not a CNC simulator, machine controller, or safety authority. Always follow your instructor, employer, machine manual, and shop safety procedures.*

---

Source: https://gcodepractice.com/journal/m98-and-m99-subprogram-fanuc-example/
Author: Lawrence Arya — https://www.linkedin.com/in/vibecoding/
