Subprogram call (M98) and return (M99) in CNC Programming - Time & Space Complexity
When using subprogram calls in CNC programming, it's important to know how the program's running time changes as the number of calls grows.
We want to understand how calling and returning from subprograms affects the total steps the machine takes.
Analyze the time complexity of the following code snippet.
N10 G00 X0 Y0
N20 M98 P100 L5
N30 M30
O100
N10 G01 X10 Y10 F100
N20 M99
This code calls subprogram O100 five times using M98, then ends. The subprogram moves the tool and returns with M99.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The subprogram call M98 repeats the movement commands inside O100.
- How many times: The subprogram runs 5 times as specified by L5.
Each time the subprogram is called, the machine executes the commands inside it once.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 x commands in subprogram |
| 10 | 10 x commands in subprogram |
| 100 | 100 x commands in subprogram |
Pattern observation: The total steps grow directly with the number of subprogram calls.
Time Complexity: O(n)
This means the total execution time grows linearly with the number of subprogram calls.
[X] Wrong: "Calling a subprogram multiple times runs all commands only once."
[OK] Correct: Each call runs the subprogram commands fully, so total steps add up with each call.
Understanding how subprogram calls affect execution time helps you write efficient CNC code and explain program behavior clearly.
"What if the subprogram itself calls another subprogram? How would that change the time complexity?"
Practice
M98 command do in CNC programming?Solution
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.Step 2: Differentiate from M99
M99 is used to end a subprogram and return to the main program, not to call it.Final Answer:
Calls a subprogram by its number, optionally repeating it -> Option DQuick Check:
M98 calls subprogram [OK]
- Confusing M98 with M99
- Thinking M98 stops the machine
- Assuming M98 starts spindle
Solution
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.Step 2: Check other options for errors
M99 is for return, not call; order matters; M100 is not a command.Final Answer:
M98 P100 L3 -> Option BQuick Check:
Call syntax = M98 P100 L3 [OK]
- Using M99 instead of M98 to call
- Swapping P and L parameters
- Using invalid command like M100
N10 M98 P200 L2 N20 M30 O200 N10 G01 X10 Y10 N20 M99What will happen when the program runs?
Solution
Step 1: Analyze M98 call with L2
M98 P200 L2 calls subprogram 200 two times.Step 2: Understand M99 in subprogram
M99 ends the subprogram and returns to main program after each call.Step 3: Main program ends with M30
After subprogram calls, M30 ends the main program.Final Answer:
The subprogram 200 runs twice, then the main program ends -> Option AQuick Check:
M98 L2 calls twice, M99 returns, M30 ends [OK]
- Thinking M99 causes error
- Assuming infinite loop without M99
- Ignoring L parameter repeat count
N10 M98 P300 N20 M30 O300 N10 G01 X20 Y20 N20 M30
Solution
Step 1: Check subprogram termination
Subprogram 300 ends with M30, which ends the entire program, not returns to main.Step 2: Identify missing M99
Subprograms must end with M99 to return control to main program after call.Final Answer:
Subprogram 300 does not have M99 to return -> Option AQuick Check:
Missing M99 causes no return from subprogram [OK]
- Using M30 inside subprogram instead of M99
- Thinking L parameter is mandatory
- Confusing subprogram number
Solution
Step 1: Confirm main program calls subprogram 5 times
M98 P150 L5 calls subprogram 150 five times, correct for repetition.Step 2: Check subprogram ends with M99
Subprogram ends with M99 to return control after each call.Step 3: Main program ends with M30
M30 properly ends the main program after calls.Final Answer:
N10 M98 P150 L5
N20 M30
O150
N10 G01 X0 Y0
N20 M99 -> Option CQuick Check:
M98 with L5 and M99 in subprogram [OK]
- Ending subprogram with M30 instead of M99
- Using M99 in main program instead of M30
- Omitting repeat count L5
Ready to prove it?
Pass the test and this pattern moves unknown
