Subprogram for repeated patterns in CNC Programming - Time & Space Complexity
When using subprograms in CNC programming, it's important to know how the time to run the program changes as we repeat patterns.
We want to see how the number of repeated calls affects the total work done.
Analyze the time complexity of the following code snippet.
O1000 (Main program start)
M98 P1000 L5 (Call subprogram 1000 five times)
M30 (End program)
O1000 (Subprogram start)
G01 X10 Y10 F100 (Move command)
M99 (Return from subprogram)
This code calls a subprogram that moves the tool, repeating it 5 times from the main program.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The subprogram call that moves the tool.
- How many times: The subprogram runs 5 times as specified by the L5 parameter.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 subprogram call |
| 5 | 5 subprogram calls |
| 10 | 10 subprogram calls |
Pattern observation: The total work grows directly with the number of times the subprogram is called.
Time Complexity: O(n)
This means the total time increases in a straight line as you repeat the subprogram more times.
[X] Wrong: "Calling a subprogram multiple times runs in constant time because it's just one command."
[OK] Correct: Each call runs the subprogram fully, so the total time adds up with each repetition.
Understanding how repeated subprogram calls affect execution time helps you explain program efficiency clearly and confidently.
"What if the subprogram itself contained a loop that ran m times? How would the time complexity change with both n and m?"
Practice
Solution
Step 1: Understand subprogram concept
Subprograms are blocks of code that can be reused multiple times in CNC programming.Step 2: Identify main purpose
They help avoid rewriting the same instructions repeatedly, making code shorter and easier to manage.Final Answer:
To repeat a set of instructions multiple times without rewriting them -> Option DQuick Check:
Subprograms repeat code = D [OK]
- Confusing subprograms with tool changes
- Thinking subprograms control spindle speed
- Assuming subprograms stop the machine
Solution
Step 1: Recall subprogram call command
M98 is the standard command to call a subprogram in CNC programming.Step 2: Match syntax with subprogram number
The correct syntax is M98 followed by P and the subprogram number, e.g., M98 P100.Final Answer:
M98 P100 -> Option BQuick Check:
Call subprogram = M98 P[number] [OK]
- Using M99 to call instead of return
- Swapping M98 and M99 commands
- Using L instead of P for subprogram number
O1000; G01 X10 Y10 F100; M99; N10 M98 P1000 L3; M30;
Solution
Step 1: Understand subprogram call with repetition
M98 P1000 L3 calls subprogram 1000 three times in a row.Step 2: Analyze subprogram content
Subprogram 1000 moves the tool to X10 Y10 with feedrate 100, then returns with M99.Final Answer:
Moves to X10 Y10 three times in a row -> Option AQuick Check:
M98 P1000 L3 repeats subprogram 3 times = C [OK]
- Ignoring the L3 repeat count
- Confusing M99 as a stop command
- Assuming infinite loop without L parameter
O2000; G01 X20 Y20 F150; M99; N20 M98 P2000 L0; M30;
Solution
Step 1: Check repetition count in subprogram call
L0 means repeat zero times, so the subprogram is not executed at all.Step 2: Confirm other commands are correct
M99 is correct to return from subprogram, P2000 is valid, and G00 is not mandatory before M98.Final Answer:
L0 causes no repetition, so subprogram never runs -> Option CQuick Check:
L0 means zero repeats = B [OK]
- Thinking M99 is wrong inside subprogram
- Assuming P2000 is invalid
- Believing G00 is required before M98
Solution
Step 1: Understand incremental positioning for spacing
G91 sets incremental mode, so each call moves relative to last position, spacing holes 10mm apart.Step 2: Check subprogram and loop usage
Subprogram O3000 uses G81 drilling cycle with X#1 (variable), M99 returns correctly, and M98 P3000 L5 calls it 5 times.Final Answer:
Option A correctly uses incremental mode, subprogram, and loop for 5 holes spaced 10mm apart -> Option AQuick Check:
Incremental + M98 with L5 = 5 spaced holes [OK]
- Using G90 (absolute) instead of G91 (incremental)
- Forgetting M99 to return from subprogram
- Using M30 inside subprogram instead of M99
Ready to prove it?
Pass the test and this pattern moves unknown
