---
title: "O-Word Subroutines in LinuxCNC: Examples and How They Work"
description: "LinuxCNC uses O-words, not Fanuc-style M98/M99, for subroutines, loops, and conditionals. Here is how o-sub, o-call, while, and if work, with examples."
url: https://gcodepractice.com/journal/o-word-subroutines-linuxcnc-examples/
canonical: https://gcodepractice.com/journal/o-word-subroutines-linuxcnc-examples/
author: "Lawrence Arya"
authorUrl: https://www.linkedin.com/in/vibecoding/
published: 2026-06-02
updated: 2026-06-02
category: "Code reference"
tags: ["linuxcnc", "o-word", "subroutine", "advanced"]
lang: en
---

# O-Word Subroutines in LinuxCNC: Examples and How They Work

> **TL;DR** LinuxCNC uses O-words for program flow instead of Fanuc-style M98/M99. A subroutine is defined with 'o100 sub' and 'o100 endsub' and run with 'o100 call'; loops use 'o101 while' and 'o101 endwhile'; conditionals use 'o102 if' and 'o102 endif'. You can pass parameters and use them as #1, #2, and so on. It is a more programming-like model than M98/M99.

If you have used Fanuc-style subprograms with [M98 and M99](/journal/m98-and-m99-subprogram-fanuc-example/), LinuxCNC does the same job a different way: with O-words. They cover subroutines, loops, and conditionals, and the model feels more like a small programming language than a pair of M-codes.

## The building blocks

- **Subroutine:** define a reusable block between `o100 sub` and `o100 endsub`, then run it with `o100 call`. The number labels the block.
- **Parameters:** values passed on the `call` line become `#1`, `#2`, and so on inside the subroutine.
- **Loop:** `o101 while [condition]` ... `o101 endwhile` repeats a block.
- **Conditional:** `o102 if [condition]` ... `o102 endif` runs a block only when the condition holds.

## A simple example

```
o100 sub          (define subroutine 100)
  G0 Z5
  G1 Z-1 F100
o100 endsub

o100 call         (run subroutine 100)
M2                (end program)
```

The subroutine is defined once and run with a single `call`. Loops and conditionals let you repeat or branch without rewriting code.

## LinuxCNC O-words vs Fanuc M98/M99

| Task | LinuxCNC | Fanuc-style |
| --- | --- | --- |
| Define a subroutine | `oNNN sub` / `oNNN endsub` | A separate program number |
| Call it | `oNNN call` | `M98 P...` |
| Return | implicit at `endsub` | `M99` |
| Loop | `oNNN while` / `endwhile` | macro / repeat count |
| Conditional | `oNNN if` / `endif` | macro B (optional) |

## Where this fits

O-words are an advanced topic, well past the [common G-codes](/journal/common-g-codes-for-cnc-beginners/). Make the basics automatic first with [beginner CNC code practice](/journal/beginner-cnc-code-practice/), and if you also work on Fanuc-style controls, compare with [M98 and M99 subprograms](/journal/m98-and-m99-subprogram-fanuc-example/). If you are building a LinuxCNC machine, note that the [Mesa card](/journal/mesa-card-linuxcnc-g-code-practice/) is hardware that does not change these codes, and there is a wider set of [open-source G-code practice software](/journal/open-source-g-code-practice-software/) to run and visualize them. A free tool like [G-Code Sprint](/g-code-practice/) drills the foundational codes; confirm O-word details against the LinuxCNC documentation, since this is an educational overview.

## Bottom line

LinuxCNC uses O-words, `o-sub`/`endsub` with `o-call`, plus `while` and `if`, for subroutines, loops, and conditionals. It is the LinuxCNC equivalent of Fanuc M98/M99, with more flexibility and different syntax.

## Sources

- [LinuxCNC O-code (subroutines, loops, conditionals)](https://linuxcnc.org/docs/html/gcode/o-code.html)
- [LinuxCNC G-code reference](https://linuxcnc.org/docs/html/gcode/g-code.html)
- [Wikipedia: G-code](https://en.wikipedia.org/wiki/G-code)

## Frequently asked questions

### What are O-words in LinuxCNC?
O-words are LinuxCNC's program-flow commands: subroutines, loops, and conditionals. A subroutine is defined with `o100 sub` and `o100 endsub` and called with `o100 call`. They are LinuxCNC's equivalent of, and more flexible than, Fanuc-style M98/M99 subprograms.

### How do you call a subroutine in LinuxCNC?
Define it between `oNNN sub` and `oNNN endsub`, then run it with `oNNN call`, optionally passing parameters that become `#1`, `#2`, and so on inside the subroutine. The number NNN labels that block.

### Is LinuxCNC O-word the same as Fanuc M98?
They do the same job, reusing a block of code, but the syntax is different. Fanuc uses `M98` to call and `M99` to return; LinuxCNC uses o-sub/endsub and o-call, plus while and if for loops and conditionals. Confirm the exact behavior in the LinuxCNC docs.

*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/o-word-subroutines-linuxcnc-examples/
Author: Lawrence Arya — https://www.linkedin.com/in/vibecoding/
