Bird
Raised Fist0
CNC Programmingscripting~15 mins

Subprogram call (M98) and return (M99) in CNC Programming - Deep Dive

Choose your learning style10 modes available
Overview - Subprogram call (M98) and return (M99)
What is it?
Subprogram call (M98) and return (M99) are commands used in CNC programming to run a separate block of code, called a subprogram, from the main program. M98 tells the machine to jump to a subprogram and start executing it. M99 tells the machine to finish the subprogram and return to the main program where it left off. This helps organize complex machining tasks into smaller, reusable pieces.
Why it matters
Without subprogram calls, CNC programs would be long and repetitive, making them hard to write, read, and maintain. Subprograms let programmers reuse code for repeated tasks, saving time and reducing errors. This makes machining more efficient and flexible, especially for complex parts or batch production.
Where it fits
Before learning M98 and M99, you should understand basic CNC programming commands and program structure. After mastering subprogram calls, you can learn about parameter passing, nested subprograms, and advanced program flow control to create more dynamic CNC programs.
Mental Model
Core Idea
M98 calls a mini-program to run, and M99 ends it and sends control back to the main program.
Think of it like...
It's like calling a friend to do a small task for you and then waiting for them to finish before continuing your work.
Main Program
  │
  ├─ M98 Call Subprogram ──▶ Subprogram Start
  │                         │
  │                         ├─ Executes subprogram commands
  │                         │
  │                         └─ M99 Return to Main Program
  │
  └─ Continues main program after return
Build-Up - 6 Steps
1
FoundationUnderstanding CNC Program Structure
🤔
Concept: Learn how CNC programs are organized into main programs and subprograms.
A CNC program is a list of instructions that tell the machine what to do step by step. The main program controls the overall machining process. Subprograms are smaller sets of instructions that perform specific tasks, like drilling a hole or cutting a shape. They help keep the main program clean and organized.
Result
You can identify main program lines and subprogram blocks in CNC code.
Knowing the difference between main and subprograms is essential to understand how M98 and M99 work.
2
FoundationBasic Use of M98 and M99 Commands
🤔
Concept: Learn what M98 and M99 commands do in CNC programming.
M98 is used to call a subprogram by its number or name. When the machine sees M98, it pauses the main program and starts the subprogram. M99 is placed at the end of the subprogram to tell the machine to stop the subprogram and return to the main program where it left off.
Result
You can write a simple main program that calls a subprogram and returns after it finishes.
Understanding these commands lets you break down complex tasks into smaller, reusable parts.
3
IntermediateCalling Subprograms with Repeat Counts
🤔Before reading on: Do you think M98 can run a subprogram multiple times automatically? Commit to your answer.
Concept: M98 can call a subprogram multiple times by specifying a repeat count.
You can add a number after the subprogram call to repeat it. For example, 'M98 P1000 L3' calls subprogram 1000 three times in a row. This saves writing the same call multiple times and is useful for repeated operations like drilling multiple holes.
Result
The subprogram runs the specified number of times before returning to the main program.
Knowing repeat counts helps automate repetitive machining steps efficiently.
4
IntermediatePassing Parameters to Subprograms
🤔Before reading on: Can M98 pass different values to a subprogram to change its behavior? Commit to your answer.
Concept: M98 can pass parameters to subprograms to customize their actions.
Some CNC controls allow passing parameters with M98, like 'M98 P1000 Q5' where Q5 is a parameter. The subprogram can use this value to adjust things like hole depth or feed rate. This makes subprograms flexible and reusable for different tasks.
Result
Subprograms can perform different actions based on parameters passed during the call.
Parameter passing turns static subprograms into dynamic tools, reducing code duplication.
5
AdvancedNested Subprogram Calls and Returns
🤔Before reading on: Do you think a subprogram can call another subprogram using M98? Commit to your answer.
Concept: Subprograms can call other subprograms, creating nested calls with multiple returns.
A subprogram can include M98 to call another subprogram. Each M99 returns control to the caller, not necessarily the main program. This allows building complex program flows but requires careful management to avoid confusion or infinite loops.
Result
Programs can have multiple layers of subprogram calls and returns, enabling modular design.
Understanding nested calls is key to managing complex CNC programs without errors.
6
ExpertCommon Pitfalls and Control Flow Issues
🤔Before reading on: What happens if a subprogram lacks M99? Will the program stop or continue? Commit to your answer.
Concept: Missing or misplaced M99 commands can cause the machine to hang or run unintended code.
If a subprogram does not end with M99, the machine may continue running into the next program lines, causing errors or crashes. Also, incorrect nesting or infinite loops with M98 calls can freeze the machine. Proper use of M98 and M99 ensures safe and predictable program flow.
Result
Programs with correct M98/M99 usage run smoothly; errors cause machine stops or crashes.
Knowing these pitfalls prevents costly machine downtime and safety hazards.
Under the Hood
When the CNC controller reads M98, it saves the current program line position and jumps to the subprogram's start address. It executes the subprogram line by line until it encounters M99, which signals the controller to restore the saved position and continue the main program. This call-return mechanism uses a program stack internally to track return points, similar to how computer functions work.
Why designed this way?
M98 and M99 were designed to simplify program structure and reuse code in CNC machines, which have limited memory and processing power. Using subprogram calls reduces program size and complexity. The call-return model mirrors early computer programming concepts, making it intuitive for programmers familiar with functions or procedures.
┌───────────────┐
│ Main Program  │
│  Line N       │
│     │         │
│    M98 Call   │
└─────┼─────────┘
      │
      ▼
┌───────────────┐
│ Subprogram    │
│  Executes     │
│  Lines 1..M99 │
└─────┼─────────┘
      │
      ▼
┌───────────────┐
│ Return to     │
│ Main Program  │
│  Line N+1     │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does M99 always end the entire CNC program? Commit to yes or no.
Common Belief:M99 ends the whole CNC program.
Tap to reveal reality
Reality:M99 only ends the current subprogram and returns control to the calling program; it does not stop the entire CNC program.
Why it matters:Misunderstanding this can cause programmers to mistakenly place M99 expecting the program to stop, leading to unexpected machine behavior.
Quick: Can M98 call any program number without restrictions? Commit to yes or no.
Common Belief:M98 can call any program number, even if it doesn't exist or is not a subprogram.
Tap to reveal reality
Reality:M98 must call a valid subprogram that exists in memory or storage; calling invalid numbers causes errors or machine stops.
Why it matters:Calling non-existent subprograms wastes time troubleshooting and can cause machine faults.
Quick: If a subprogram calls another subprogram, does M99 return to the main program directly? Commit to yes or no.
Common Belief:M99 always returns to the main program regardless of nesting.
Tap to reveal reality
Reality:M99 returns control to the immediate caller, which could be another subprogram, not necessarily the main program.
Why it matters:Ignoring this can cause confusion in program flow and unexpected machine actions.
Quick: Does repeating a subprogram with M98 L parameter run the subprogram multiple times without reloading it? Commit to yes or no.
Common Belief:The subprogram is loaded once and repeated internally without restarting each time.
Tap to reveal reality
Reality:Each repeat of the subprogram starts fresh from its first line; the program is re-entered each time.
Why it matters:Assuming continuous execution can cause errors if the subprogram depends on state changes between repeats.
Expert Zone
1
Some CNC controls support local variables within subprograms, allowing parameter passing and temporary data storage, which is not universal.
2
Stack overflow can occur if subprograms call each other recursively without proper exit conditions, causing machine crashes.
3
Different CNC brands may have variations in M98/M99 syntax and capabilities, requiring careful adaptation when switching machines.
When NOT to use
Avoid using M98/M99 for very simple or one-off tasks where inline code is clearer. For highly dynamic or conditional logic, consider using macro programming or parametric programming features instead.
Production Patterns
In production, subprograms are used for repeated machining features like drilling patterns, tool changes, or finishing passes. They are often stored separately and called by main programs for different parts, enabling modular and maintainable CNC codebases.
Connections
Function calls in programming languages
M98/M99 in CNC programming work like function calls and returns in software programming.
Understanding function calls helps grasp how CNC subprograms manage control flow and reuse code.
Modular design in engineering
Subprograms embody modular design by breaking complex tasks into smaller, manageable units.
Recognizing modularity in CNC programming connects to broader engineering principles of efficiency and maintainability.
Workflow delegation in project management
Calling a subprogram is like delegating a task to a team member and waiting for completion before continuing.
This connection highlights how breaking work into parts and managing returns is a universal strategy for handling complexity.
Common Pitfalls
#1Forgetting to include M99 at the end of a subprogram.
Wrong approach:Omitting M99: N1000 G01 X10 Y10 N1010 G01 X20 Y20 (End of subprogram without M99)
Correct approach:Including M99: N1000 G01 X10 Y10 N1010 G01 X20 Y20 N1020 M99
Root cause:Not understanding that M99 signals the end of a subprogram and return to the main program.
#2Calling a subprogram number that does not exist or is not loaded.
Wrong approach:M98 P9999 (where program 9999 is missing)
Correct approach:M98 P1000 (where program 1000 exists and is loaded)
Root cause:Assuming any program number can be called without verifying its presence.
#3Using M99 in the main program instead of only in subprograms.
Wrong approach:Main program ends with M99 instead of M30 or M02.
Correct approach:Main program ends with M30 (program end and rewind) or M02 (program end).
Root cause:Confusing the role of M99 as a subprogram return command with program end commands.
Key Takeaways
M98 and M99 commands let CNC programs call and return from subprograms, enabling modular and reusable code.
Subprogram calls reduce repetition and simplify complex machining tasks by breaking them into smaller parts.
Proper use of M98 and M99 ensures smooth program flow; missing M99 or invalid calls cause errors or machine stops.
Advanced use includes repeating subprograms, passing parameters, and nesting calls for flexible and powerful CNC programming.
Understanding subprogram calls connects CNC programming to general programming concepts like function calls and modular design.

Practice

(1/5)
1. What does the M98 command do in CNC programming?
easy
A. Stops the machine immediately
B. Ends the current subprogram and returns to the main program
C. Starts the spindle rotation
D. Calls a subprogram by its number, optionally repeating it

Solution

  1. Step 1: Understand the role of M98

    The M98 command is used to call a subprogram by its number and can repeat it multiple times if specified.
  2. Step 2: Differentiate from M99

    M99 is used to end a subprogram and return to the main program, not to call it.
  3. Final Answer:

    Calls a subprogram by its number, optionally repeating it -> Option D
  4. Quick Check:

    M98 calls subprogram [OK]
Hint: M98 calls subprogram; M99 returns from it [OK]
Common Mistakes:
  • Confusing M98 with M99
  • Thinking M98 stops the machine
  • Assuming M98 starts spindle
2. Which of the following is the correct syntax to call subprogram number 100 with 3 repetitions?
easy
A. M100 P98 L3
B. M98 P100 L3
C. M98 L3 P100
D. M99 P100 L3

Solution

  1. Step 1: Identify correct M98 syntax

    The correct syntax to call a subprogram is M98 followed by P (program number) and optionally L (repeat count), so M98 P100 L3 is correct.
  2. Step 2: Check other options for errors

    M99 is for return, not call; order matters; M100 is not a command.
  3. Final Answer:

    M98 P100 L3 -> Option B
  4. Quick Check:

    Call syntax = M98 P100 L3 [OK]
Hint: M98 P[number] L[repeat] calls subprogram [OK]
Common Mistakes:
  • Using M99 instead of M98 to call
  • Swapping P and L parameters
  • Using invalid command like M100
3. Given this CNC program snippet:

N10 M98 P200 L2
N20 M30

O200
N10 G01 X10 Y10
N20 M99
What will happen when the program runs?
medium
A. The subprogram 200 runs twice, then the main program ends
B. The subprogram 200 runs once, then the main program loops infinitely
C. The program will cause a syntax error at M99
D. The main program ends immediately without running subprogram

Solution

  1. Step 1: Analyze M98 call with L2

    M98 P200 L2 calls subprogram 200 two times.
  2. Step 2: Understand M99 in subprogram

    M99 ends the subprogram and returns to main program after each call.
  3. Step 3: Main program ends with M30

    After subprogram calls, M30 ends the main program.
  4. Final Answer:

    The subprogram 200 runs twice, then the main program ends -> Option A
  5. Quick Check:

    M98 L2 calls twice, M99 returns, M30 ends [OK]
Hint: M98 with L repeats subprogram; M99 returns to main [OK]
Common Mistakes:
  • Thinking M99 causes error
  • Assuming infinite loop without M99
  • Ignoring L parameter repeat count
4. Identify the error in this CNC code snippet:

N10 M98 P300
N20 M30

O300
N10 G01 X20 Y20
N20 M30
medium
A. Subprogram 300 does not have M99 to return
B. M98 command is missing the repeat count L
C. Main program should not have M30
D. Subprogram number should be 200, not 300

Solution

  1. Step 1: Check subprogram termination

    Subprogram 300 ends with M30, which ends the entire program, not returns to main.
  2. Step 2: Identify missing M99

    Subprograms must end with M99 to return control to main program after call.
  3. Final Answer:

    Subprogram 300 does not have M99 to return -> Option A
  4. Quick Check:

    Missing M99 causes no return from subprogram [OK]
Hint: Subprograms must end with M99 to return [OK]
Common Mistakes:
  • Using M30 inside subprogram instead of M99
  • Thinking L parameter is mandatory
  • Confusing subprogram number
5. You want to machine 5 identical pockets using subprogram 150. Which code snippet correctly calls the subprogram 5 times and returns properly?
hard
A. N10 M98 P150 L5 N20 M99 O150 N10 G01 X0 Y0 N20 M99
B. N10 M98 P150 N20 M99 O150 N10 G01 X0 Y0 N20 M30
C. N10 M98 P150 L5 N20 M30 O150 N10 G01 X0 Y0 N20 M99
D. N10 M98 P150 L5 N20 M30 O150 N10 G01 X0 Y0 N20 M30

Solution

  1. Step 1: Confirm main program calls subprogram 5 times

    M98 P150 L5 calls subprogram 150 five times, correct for repetition.
  2. Step 2: Check subprogram ends with M99

    Subprogram ends with M99 to return control after each call.
  3. Step 3: Main program ends with M30

    M30 properly ends the main program after calls.
  4. Final Answer:

    N10 M98 P150 L5
    N20 M30

    O150
    N10 G01 X0 Y0
    N20 M99
    -> Option C
  5. Quick Check:

    M98 with L5 and M99 in subprogram [OK]
Hint: Use M98 P[number] L5 and M99 to return [OK]
Common Mistakes:
  • Ending subprogram with M30 instead of M99
  • Using M99 in main program instead of M30
  • Omitting repeat count L5
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard