📖 Scenario: You are programming a CNC machine to cut multiple identical parts. Instead of repeating the same cutting instructions many times, you want to use a subprogram to save space and make your program easier to manage.
🎯 Goal: Build a simple CNC program that uses a subprogram to perform a repeated cutting operation, showing how subprograms reduce the total program length.
📋 What You'll Learn
Create a main program with repeated cutting steps
Create a subprogram with the cutting instructions
Call the subprogram multiple times from the main program
Show the final program output with subprogram calls
💡 Why This Matters
🌍 Real World
CNC programmers use subprograms to avoid repeating the same cutting steps for multiple parts, saving memory and reducing errors.
💼 Career
Understanding subprograms is essential for CNC operators and programmers to write efficient and maintainable machine code.
Progress0 / 4 steps
1
Create the main program with repeated cutting steps
Write the main CNC program called O1000 that contains three repeated cutting steps using the exact lines: G01 X10 Y10, G01 X20 Y20, and G01 X30 Y30 repeated three times in sequence.
CNC Programming
Hint
Write the program header O1000 and repeat the three G01 lines three times.
2
Create a subprogram with the cutting instructions
Add a subprogram called O2000 that contains the three cutting instructions: G01 X10 Y10, G01 X20 Y20, and G01 X30 Y30, ending with M99.
CNC Programming
Hint
Define subprogram O2000 with the three G01 lines and end with M99.
3
Call the subprogram multiple times from the main program
Modify the main program O1000 to call the subprogram O2000 three times using the exact line M98 P2000 repeated three times, and remove the repeated cutting steps.
CNC Programming
Hint
Use M98 P2000 three times in O1000 to call the subprogram.
4
Print the final program showing subprogram calls
Print the entire CNC program including the main program O1000 with three calls to subprogram O2000 and the subprogram O2000 itself.
CNC Programming
Hint
Use print() statements to show each line of the final CNC program.
Practice
(1/5)
1. Why do subprograms help reduce the length of a CNC program?
easy
A. Because they make the machine run faster
B. Because they remove the need for tools
C. Because they add more lines of code
D. Because they let you reuse the same code multiple times
Solution
Step 1: Understand what subprograms do
Subprograms are small code blocks that can be called many times in a CNC program.
Step 2: See how reuse affects program length
Reusing code means you write it once and call it multiple times, reducing total lines.
Final Answer:
Because they let you reuse the same code multiple times -> Option D
Quick Check:
Code reuse = shorter program [OK]
Hint: Think: reuse code to avoid repeating lines [OK]
Common Mistakes:
Confusing speed with program length
Thinking subprograms add more code
Believing subprograms remove tools
2. Which of the following is the correct way to call a subprogram in a CNC program?
easy
A. G28 Z0
B. G01 X10 Y10
C. M98 P1000
D. M03 S1000
Solution
Step 1: Identify subprogram call syntax
In CNC, M98 Pxxxx calls a subprogram at number xxxx.
Step 2: Check options for correct call
Only M98 P1000 matches the subprogram call format.
Final Answer:
M98 P1000 -> Option C
Quick Check:
Subprogram call = M98 Pxxxx [OK]
Hint: Look for M98 P code to call subprograms [OK]
Common Mistakes:
Confusing movement commands with subprogram calls
Using spindle commands instead of subprogram calls
Mixing home commands with subprogram calls
3. Given a CNC program that calls a subprogram 5 times, each subprogram has 10 lines. Without subprograms, the repeated code would be written 5 times. How many lines does the main program save by using the subprogram?
medium
A. 40 lines
B. 50 lines
C. 45 lines
D. 10 lines
Solution
Step 1: Calculate lines without subprogram
Repeated code 5 times x 10 lines = 50 lines total.
Step 2: Lines with subprogram
Subprogram written once (10 lines) and called 5 times.
Hint: Subtract one copy of code from total repeats [OK]
Common Mistakes:
Counting subprogram calls as full code lines
Forgetting to subtract the single subprogram copy
Adding calls lines instead of subtracting
4. A CNC program uses subprograms but still has a very long code. What is the most likely reason?
medium
A. The subprograms are not reused enough times
B. The subprograms are too short
C. The machine is slow
D. The program uses too many tools
Solution
Step 1: Understand subprogram reuse effect
Subprograms reduce length mainly by reusing code multiple times.
Step 2: Analyze why program is still long
If subprograms are not called often, reuse benefit is low, so program stays long.
Final Answer:
The subprograms are not reused enough times -> Option A
Quick Check:
Reuse frequency affects program length [OK]
Hint: More reuse means shorter program [OK]
Common Mistakes:
Blaming machine speed for program length
Thinking subprogram size alone reduces length
Confusing tool count with program length
5. You have a CNC program with 3 different repeated tasks, each repeated 4 times, and each task's code is 8 lines. How many lines can you save by using subprograms for all tasks?
hard
A. 48 lines
B. 72 lines
C. 96 lines
D. 64 lines
Solution
Step 1: Calculate total repeated lines without subprograms
Each task repeated 4 times x 8 lines = 32 lines per task repeated. For 3 tasks: 32 x 3 = 96 lines.
Step 2: Lines with subprograms
Each task's code written once (8 lines x 3 tasks = 24 lines in subprograms), each called 4 times.
Step 3: Calculate lines saved
(4 - 1) x 8 lines x 3 tasks = 72 lines saved (avoids writing 3 duplicate copies per task).
Final Answer:
72 lines -> Option B
Quick Check:
(4-1)*8*3 = 72 lines [OK]
Hint: Calculate repeats minus one, times lines, times tasks [OK]
Common Mistakes:
Ignoring calls lines in calculation
Multiplying repeats instead of repeats minus one
Adding instead of subtracting repeated code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.