Bird
Raised Fist0
CNC Programmingscripting~10 mins

Subprogram call (M98) and return (M99) in CNC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Subprogram call (M98) and return (M99)
Start Main Program
↓
Encounter M98 Call
↓
Jump to Subprogram
↓
Execute Subprogram
↓
Encounter M99 Return
↓
Return to Main Program
↓
Continue Main Program
↓
End Program
The main program runs until it hits M98, which calls a subprogram. The subprogram runs until M99, which returns control back to the main program to continue.
Execution Sample
CNC Programming
N10 G00 X0 Y0
N20 M98 P100
N30 G01 X10 Y10
N40 M30

O100
N100 G01 X5 Y5
N110 M99
Main program moves to X0 Y0, calls subprogram 100 with M98, then moves to X10 Y10. Subprogram 100 moves to X5 Y5 and returns with M99.
Execution Table
StepProgram LineActionPosition (X,Y)Program Pointer
1N10Rapid move to X0 Y0(0,0)Main N10
2N20Call subprogram 100 (M98)(0,0)Main N20 -> Subprogram O100 N100
3N100Linear move to X5 Y5(5,5)Subprogram N100
4N110Return from subprogram (M99)(5,5)Return to Main N30
5N30Linear move to X10 Y10(10,10)Main N30
6N40Program end (M30)(10,10)Main N40
7-Program stops(10,10)End
💡 Program ends after M30 at line N40
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
Position (X,Y)(unknown)(0,0)(0,0)(5,5)(5,5)(10,10)(10,10)
Program PointerMain N10Main N20Subprogram N100Subprogram N110Main N30Main N40End
Key Moments - 3 Insights
Why does the program jump to subprogram at M98 instead of continuing the main program?
At step 2 in the execution_table, M98 tells the machine to call the subprogram specified by P100, so the program pointer moves from main line N20 to subprogram line N100.
What happens when the program reaches M99 in the subprogram?
At step 4, M99 signals the end of the subprogram and returns control back to the main program at the line after the M98 call, which is N30.
Does the position change when returning from the subprogram?
No, as shown in the variable_tracker, the position remains at (5,5) after M99 before moving to the next main program line.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the position after executing the subprogram line N100?
A(5,5)
B(0,0)
C(10,10)
D(unknown)
💡 Hint
Check the Position column at Step 3 in the execution_table.
At which step does the program return from the subprogram to the main program?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the action 'Return from subprogram (M99)' in the execution_table.
If M99 was missing in the subprogram, what would happen to the program pointer after step 3?
AIt would return to main program immediately
BIt would continue in the subprogram lines
CIt would stop the program
DIt would jump to a different subprogram
💡 Hint
M99 is needed to return; without it, the subprogram continues executing.
Concept Snapshot
M98 calls a subprogram specified by P-code.
M99 ends the subprogram and returns to the main program.
Main program pauses at M98 until subprogram finishes.
Position and state carry over between main and subprogram.
Use M30 to end the main program.
Full Transcript
This visual execution shows how a CNC program uses M98 to call a subprogram and M99 to return. The main program runs lines N10 and N20, then calls subprogram O100 at N20. The subprogram runs lines N100 and N110, moving the tool and then returning control with M99. After return, the main program continues at N30 and ends at N40 with M30. The position updates as moves happen and stays consistent across calls and returns. This helps beginners see how subprogram calls pause the main program and how returns resume it.

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