Bird
Raised Fist0
CNC Programmingscripting~10 mins

Subprogram for repeated patterns in CNC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Subprogram for repeated patterns
Start Main Program
↓
Call Subprogram
↓
Execute Subprogram Steps
↓
Return to Main Program
↓
Repeat Call if Needed
↓
End Main Program
The main program calls a subprogram to perform repeated steps, then returns and repeats calls as needed.
Execution Sample
CNC Programming
O1000 (Main Program)
G00 X0 Y0
M98 P2000 L3 (Call Subprogram 3 times)
M30

O2000 (Subprogram)
G01 X10 Y10 F100
M99
Main program moves to start, calls subprogram 3 times to move tool, then ends.
Execution Table
StepActionProgram LineSubprogram Call CountPosition (X,Y)Notes
1Start Main ProgramO10000(0,0)Program begins at origin
2Rapid move to X0 Y0G00 X0 Y00(0,0)Tool moves quickly to start
3Call SubprogramM98 P2000 L31(0,0)First call to subprogram
4Subprogram move to X10 Y10G01 X10 Y10 F1001(10,10)Cutting move in subprogram
5Return from SubprogramM991(10,10)Return to main program
6Call SubprogramM98 P2000 L32(10,10)Second call to subprogram
7Subprogram move to X10 Y10G01 X10 Y10 F1002(10,10)Cutting move repeated
8Return from SubprogramM992(10,10)Return to main program
9Call SubprogramM98 P2000 L33(10,10)Third call to subprogram
10Subprogram move to X10 Y10G01 X10 Y10 F1003(10,10)Cutting move repeated
11Return from SubprogramM993(10,10)Return to main program
12End Main ProgramM303(10,10)Program ends after 3 calls
💡 Subprogram called 3 times as specified by L3, then main program ends.
Variable Tracker
VariableStartAfter Call 1After Call 2After Call 3Final
Position (X,Y)(0,0)(10,10)(10,10)(10,10)(10,10)
Subprogram Call Count01233
Key Moments - 3 Insights
Why does the position stay at (10,10) after each subprogram call instead of changing?
Because the subprogram moves the tool to the same coordinates each time (X10 Y10), so the position does not change between calls as shown in execution_table rows 4,7,10.
What does the L3 mean in the call M98 P2000 L3?
L3 tells the CNC to repeat the subprogram 3 times, which is why the subprogram is called three times in the execution_table at steps 3,6,9.
What happens when M99 is executed in the subprogram?
M99 ends the subprogram and returns control to the main program, as seen in execution_table steps 5,8,11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the position after the second subprogram call?
A(10,10)
B(0,0)
C(20,20)
D(5,5)
💡 Hint
Check the Position column at step 8 in the execution_table.
At which step does the main program end?
AStep 10
BStep 12
CStep 9
DStep 5
💡 Hint
Look for the M30 command in the Program Line column in execution_table.
If L3 was changed to L2 in the call M98 P2000 L3, how many times would the subprogram run?
A3
B1
C2
D4
💡 Hint
L value controls repeat count, see key_moments about L3 meaning.
Concept Snapshot
Subprograms let you repeat code blocks easily.
Use M98 Pxxxx Ln to call subprogram n times.
Subprogram ends with M99 to return.
Positions stay as moved inside subprogram.
Main program continues after subprogram returns.
Full Transcript
This lesson shows how CNC programs use subprograms to repeat patterns. The main program calls a subprogram using M98 with a repeat count L. The subprogram runs its commands, then ends with M99 to return. The example moves the tool to X10 Y10 three times by calling the subprogram three times. The execution table traces each step, showing the position and call count. Key points include understanding the repeat count L, the return command M99, and that the tool position stays at the moved location after each call. The visual quiz tests understanding of position after calls, program end step, and changing repeat count. The quick snapshot summarizes the syntax and behavior for easy reference.

Practice

(1/5)
1. What is the main purpose of using a subprogram in CNC programming?
easy
A. To stop the machine immediately
B. To speed up the machine spindle
C. To change the tool automatically
D. To repeat a set of instructions multiple times without rewriting them

Solution

  1. Step 1: Understand subprogram concept

    Subprograms are blocks of code that can be reused multiple times in CNC programming.
  2. Step 2: Identify main purpose

    They help avoid rewriting the same instructions repeatedly, making code shorter and easier to manage.
  3. Final Answer:

    To repeat a set of instructions multiple times without rewriting them -> Option D
  4. Quick Check:

    Subprograms repeat code = D [OK]
Hint: Subprograms repeat code blocks to save writing [OK]
Common Mistakes:
  • Confusing subprograms with tool changes
  • Thinking subprograms control spindle speed
  • Assuming subprograms stop the machine
2. Which of the following is the correct syntax to call a subprogram numbered 100 in CNC code?
easy
A. M99 P100
B. M98 P100
C. M100 P98
D. M98 L100

Solution

  1. Step 1: Recall subprogram call command

    M98 is the standard command to call a subprogram in CNC programming.
  2. Step 2: Match syntax with subprogram number

    The correct syntax is M98 followed by P and the subprogram number, e.g., M98 P100.
  3. Final Answer:

    M98 P100 -> Option B
  4. Quick Check:

    Call subprogram = M98 P[number] [OK]
Hint: Use M98 P[number] to call subprograms [OK]
Common Mistakes:
  • Using M99 to call instead of return
  • Swapping M98 and M99 commands
  • Using L instead of P for subprogram number
3. Given the following CNC code snippet, what will be the output movement commands executed?
O1000;
G01 X10 Y10 F100;
M99;

N10 M98 P1000 L3;
M30;
medium
A. Moves to X10 Y10 three times in a row
B. Moves to X10 Y10 once only
C. Moves to X10 Y10 infinitely
D. No movement commands executed

Solution

  1. Step 1: Understand subprogram call with repetition

    M98 P1000 L3 calls subprogram 1000 three times in a row.
  2. Step 2: Analyze subprogram content

    Subprogram 1000 moves the tool to X10 Y10 with feedrate 100, then returns with M99.
  3. Final Answer:

    Moves to X10 Y10 three times in a row -> Option A
  4. Quick Check:

    M98 P1000 L3 repeats subprogram 3 times = C [OK]
Hint: M98 with L repeats subprogram calls multiple times [OK]
Common Mistakes:
  • Ignoring the L3 repeat count
  • Confusing M99 as a stop command
  • Assuming infinite loop without L parameter
4. Identify the error in this CNC subprogram call:
O2000;
G01 X20 Y20 F150;
M99;

N20 M98 P2000 L0;
M30;
medium
A. M99 should be M98 inside subprogram
B. P2000 is invalid subprogram number
C. L0 causes no repetition, so subprogram never runs
D. Missing G00 command before M98

Solution

  1. Step 1: Check repetition count in subprogram call

    L0 means repeat zero times, so the subprogram is not executed at all.
  2. Step 2: Confirm other commands are correct

    M99 is correct to return from subprogram, P2000 is valid, and G00 is not mandatory before M98.
  3. Final Answer:

    L0 causes no repetition, so subprogram never runs -> Option C
  4. Quick Check:

    L0 means zero repeats = B [OK]
Hint: L0 means no repeats, so subprogram won't run [OK]
Common Mistakes:
  • Thinking M99 is wrong inside subprogram
  • Assuming P2000 is invalid
  • Believing G00 is required before M98
5. You want to drill 5 holes spaced 10mm apart along the X-axis starting at X0 Y0 using a subprogram. Which code snippet correctly uses a subprogram with a loop to achieve this?
hard
A.
O3000;
G81 X#1 Y0 Z-5 R1 F100;
M99;

N30 G91;
M98 P3000 L5;
M30;
B.
O3000;
G81 X0 Y0 Z-5 R1 F100;
M99;

N30 G90;
M98 P3000 L5;
M30;
C.
O3000;
G81 X10 Y0 Z-5 R1 F100;
M99;

N30 G91;
M98 P3000 L5;
M30;
D.
O3000;
G81 X#1 Y0 Z-5 R1 F100;
M30;

N30 G91;
M98 P3000 L5;
M30;

Solution

  1. Step 1: Understand incremental positioning for spacing

    G91 sets incremental mode, so each call moves relative to last position, spacing holes 10mm apart.
  2. 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.
  3. Final Answer:

    Option A correctly uses incremental mode, subprogram, and loop for 5 holes spaced 10mm apart -> Option A
  4. Quick Check:

    Incremental + M98 with L5 = 5 spaced holes [OK]
Hint: Use G91 for incremental moves and M98 L5 to repeat 5 times [OK]
Common Mistakes:
  • Using G90 (absolute) instead of G91 (incremental)
  • Forgetting M99 to return from subprogram
  • Using M30 inside subprogram instead of M99
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