Bird
Raised Fist0
CNC Programmingscripting~15 mins

Subprogram for repeated patterns in CNC Programming - Deep Dive

Choose your learning style10 modes available
Overview - Subprogram for repeated patterns
What is it?
A subprogram in CNC programming is a smaller set of instructions that can be called multiple times within a main program. It helps to repeat a pattern or a sequence of machining steps without rewriting the same code again. This makes the program shorter, easier to read, and simpler to change. Subprograms are like mini-programs inside the big program that do specific tasks.
Why it matters
Without subprograms, CNC programs would be very long and repetitive, making them hard to write and prone to mistakes. If you want to change a repeated pattern, you would have to edit every copy manually. Subprograms save time, reduce errors, and make the machining process more efficient and flexible. They allow programmers to reuse code and focus on the unique parts of the job.
Where it fits
Before learning subprograms, you should understand basic CNC programming commands and how to write simple tool paths. After mastering subprograms, you can learn about advanced programming techniques like macros, parametric programming, and conditional logic to create even smarter CNC programs.
Mental Model
Core Idea
A subprogram is a reusable block of CNC code that you call whenever you need to repeat a machining pattern.
Think of it like...
Think of a subprogram like a recipe card you keep in your kitchen. Instead of writing the full recipe every time you want to bake cookies, you just grab the card and follow the steps. If you want to change the recipe, you only update the card once, and every time you use it, the new recipe applies.
Main Program
  │
  ├─ Call Subprogram 1 ──> Executes repeated pattern
  │
  ├─ Other machining steps
  │
  └─ Call Subprogram 1 ──> Executes repeated pattern again

Subprogram 1:
  ├─ Step A
  ├─ Step B
  └─ Step C
  (Returns to main program)
Build-Up - 7 Steps
1
FoundationUnderstanding CNC Program Structure
🤔
Concept: Learn the basic parts of a CNC program and how instructions are executed step-by-step.
A CNC program is a list of commands that tell the machine how to move the tool. It usually starts with setup commands, then moves the tool along paths, and ends with stopping commands. Each line is executed in order from top to bottom.
Result
You can write a simple program that moves the tool to cut a shape once.
Knowing the linear flow of CNC programs helps you see why repeating code manually is inefficient.
2
FoundationIdentifying Repeated Patterns
🤔
Concept: Recognize when parts of the program repeat and why repeating code is a problem.
Look at a program that drills multiple holes with the same steps repeated for each hole. Copying the same lines many times makes the program long and hard to fix if changes are needed.
Result
You see the same code blocks repeated multiple times in the program.
Spotting repetition is the first step to knowing when to use subprograms.
3
IntermediateCreating a Basic Subprogram
🤔Before reading on: do you think a subprogram is written inside the main program or separately? Commit to your answer.
Concept: Learn how to write a subprogram with its own code block and how to call it from the main program.
A subprogram starts with a unique number (like O1000) and ends with an M99 command to return. In the main program, you call it using M98 P1000. This runs the subprogram code and then returns to continue the main program.
Result
The repeated pattern runs each time the subprogram is called, reducing code duplication.
Understanding the call and return mechanism is key to using subprograms effectively.
4
IntermediatePassing Parameters to Subprograms
🤔Before reading on: do you think subprograms can change behavior based on input values? Commit to yes or no.
Concept: Learn how to make subprograms flexible by passing values like coordinates or counts.
You can pass parameters using variables or special codes (like #1, #2) to tell the subprogram where or how many times to repeat actions. This lets one subprogram handle different holes or patterns by changing inputs.
Result
One subprogram can perform different tasks depending on the parameters it receives.
Parameterizing subprograms makes your code more powerful and adaptable.
5
IntermediateLooping with Subprogram Calls
🤔Before reading on: do you think loops are done inside subprograms or by calling subprograms multiple times? Commit to your answer.
Concept: Understand how to repeat subprogram calls multiple times using loops or counters.
You can use a loop in the main program to call a subprogram repeatedly, changing parameters each time. This is useful for drilling a row of holes or machining repeated features.
Result
The machine performs repeated patterns efficiently without copying code.
Combining loops and subprograms maximizes automation and reduces errors.
6
AdvancedNested Subprograms and Modular Design
🤔Before reading on: do you think subprograms can call other subprograms? Commit to yes or no.
Concept: Learn that subprograms can call other subprograms to build complex, modular programs.
A subprogram can include calls to other subprograms, creating layers of reusable code. This helps organize complex machining tasks into smaller, manageable pieces.
Result
Programs become easier to maintain and extend by reusing smaller subprograms.
Knowing nested calls helps design scalable CNC programs for complex parts.
7
ExpertAvoiding Common Subprogram Pitfalls
🤔Before reading on: do you think forgetting M99 in a subprogram causes problems? Commit to yes or no.
Concept: Understand subtle errors like missing return commands or parameter misuse that cause program crashes or unexpected behavior.
If a subprogram lacks M99, the machine may continue running into unintended code. Passing wrong parameters can cause tool crashes or wrong cuts. Careful testing and clear structure prevent these issues.
Result
Reliable, safe CNC programs that run as expected without crashes or errors.
Knowing common pitfalls prevents costly mistakes and downtime in production.
Under the Hood
When the CNC machine encounters a subprogram call (M98), it saves the current position in the main program and jumps to the subprogram's code block. It executes the subprogram line by line until it reaches the return command (M99), then it returns to the saved position in the main program and continues. Parameters passed to the subprogram are stored in variables that the subprogram reads to adjust its behavior.
Why designed this way?
Subprograms were designed to reduce repetitive code and simplify program management. Early CNC machines had limited memory, so reusing code saved space. The call-return mechanism mimics traditional programming functions, making it easier for programmers to think modularly. Alternatives like copying code were inefficient and error-prone.
Main Program
  │
  ├─ Execute commands
  ├─ M98 P1000 (Call Subprogram 1000)
  │     ↓
  │  Subprogram 1000
  │  ├─ Execute subprogram commands
  │  └─ M99 (Return)
  │     ↑
  └─ Resume main program commands
Myth Busters - 4 Common Misconceptions
Quick: Do you think a subprogram runs independently and does not return to the main program? Commit to yes or no.
Common Belief:A subprogram runs on its own and does not return control to the main program.
Tap to reveal reality
Reality:A subprogram always returns control to the main program after finishing, unless it lacks the return command (M99), which causes errors.
Why it matters:Believing this can lead to missing M99 commands, causing the machine to run unintended code and potentially damage the part or tool.
Quick: Do you think you must write separate subprograms for every repeated pattern even if they are similar? Commit to yes or no.
Common Belief:Each repeated pattern requires its own unique subprogram.
Tap to reveal reality
Reality:One subprogram can handle multiple patterns by using parameters to change behavior, reducing code duplication.
Why it matters:Not using parameters leads to many similar subprograms, increasing program size and maintenance effort.
Quick: Do you think subprograms can modify variables in the main program directly? Commit to yes or no.
Common Belief:Subprograms can change variables in the main program freely.
Tap to reveal reality
Reality:Subprograms have their own scope; changes to variables inside may not affect the main program unless explicitly designed to do so.
Why it matters:Assuming shared variable scope can cause unexpected program behavior and bugs.
Quick: Do you think calling a subprogram multiple times always speeds up the program execution? Commit to yes or no.
Common Belief:Using subprograms always makes the CNC program run faster.
Tap to reveal reality
Reality:Subprograms reduce code size and errors but calling them repeatedly can add overhead; optimization depends on machine and program design.
Why it matters:Overusing subprogram calls without considering machine cycle time can reduce efficiency.
Expert Zone
1
Some CNC controllers support local variables in subprograms, allowing safer parameter passing without affecting global variables.
2
Stack depth for nested subprogram calls is limited; deep nesting can cause program crashes or memory errors.
3
Using subprograms with parametric programming enables dynamic toolpath generation, reducing the need for manual edits.
When NOT to use
Avoid subprograms for very simple, one-off operations where overhead is unnecessary. Instead, write direct code. For highly dynamic or conditional logic, use macro programming or parametric programming features instead of fixed subprograms.
Production Patterns
In production, subprograms are used to machine repeated features like holes, pockets, or threads. They are combined with loops and parameter passing to handle arrays of features efficiently. Nested subprograms modularize complex parts, and version-controlled subprogram libraries enable reuse across multiple jobs.
Connections
Functions in Programming Languages
Subprograms in CNC are like functions in programming languages that encapsulate reusable code blocks.
Understanding functions helps grasp subprogram calls, parameters, and returns, bridging CNC programming with general software development.
Modular Design in Engineering
Subprograms reflect modular design principles where complex systems are built from smaller, reusable modules.
Recognizing modularity in CNC programming connects to broader engineering practices that improve maintainability and scalability.
Recipe Instructions in Cooking
Subprograms are like reusable recipe steps that can be repeated or adjusted with different ingredients.
This connection shows how breaking tasks into reusable steps is a universal strategy for efficiency and clarity.
Common Pitfalls
#1Forgetting the return command (M99) at the end of a subprogram.
Wrong approach:O1000 G01 X10 Y10 G01 X20 Y20 M30
Correct approach:O1000 G01 X10 Y10 G01 X20 Y20 M99
Root cause:Confusing program end command (M30) with subprogram return command (M99) causes the machine to not return to the main program.
#2Copying repeated code instead of using subprogram calls.
Wrong approach:G01 X10 Y10 G01 X20 Y20 G01 X10 Y10 G01 X20 Y20 G01 X10 Y10 G01 X20 Y20
Correct approach:M98 P1000 M98 P1000 M98 P1000 O1000 G01 X10 Y10 G01 X20 Y20 M99
Root cause:Not recognizing repetition leads to longer, error-prone programs.
#3Passing parameters incorrectly or not updating them between calls.
Wrong approach:M98 P1000 M98 P1000 O1000 G01 X#1 Y#2 M99
Correct approach:#1=10 #2=20 M98 P1000 #1=30 #2=40 M98 P1000 O1000 G01 X#1 Y#2 M99
Root cause:Failing to assign or update parameter values causes the subprogram to repeat the same action unintentionally.
Key Takeaways
Subprograms let you write a block of CNC code once and reuse it multiple times, saving effort and reducing errors.
They work by calling the subprogram with M98 and returning with M99, allowing the main program to continue smoothly.
Passing parameters to subprograms makes them flexible to handle different machining tasks with the same code.
Nested subprograms and loops enable complex, modular CNC programs that are easier to maintain and scale.
Avoid common mistakes like missing return commands or incorrect parameter passing to ensure safe and reliable machining.

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