Numerical Control Programming
Chapter 2
How Does Numerical Control Work?

Subroutines and Loops

Often a series of identical geometric configurations, such as grooves, pockets, and hole patterns must be machined at various locations around the workpiece. Before the advent of CNC, the cutter motion had to be programmed step-by-step at each of these locations, making for some rather lengthy programs that were tedious to write.

CNC controllers have an ability to "branch out" from the main program to another part of the program, execute a group of commands (a subsection or subroutine, often called a macro), and then return to the main program at exactly the same place from which it branched out, and resume executing the main program.

Branching Commands and Subroutines

A branching command to execute a subroutine is analogous to a reader being told to look at a table (e.g., "see Table 2.1"). The reader "branches out" from that point in the text (hopefully remembering where he or she left from), looks at Table 2.1 (the subroutine), and then returns to the text and continues reading. The table must have a name (2.1) so the reader knows what to look for. The table must also be "set off" from the main body of the text so the reader can clearly identify its beginning and end.

The sections of the program that are the subroutines must be identified (or defined) in the program. One method is to place a name or symbol (such as #1 or #2, etc.) or the letter S and a number (such as S3, etc.) for each subroutine immediately in front of the first command in that subroutine. The end of the subroutine must also be identified by using a different symbol (such as a dollar sign $ or the letter E). The commands between the #1 symbol and the $ symbol (or the S and the E) constitute the subroutine. (Some controllers require all subroutines to be physically placed beyond the end of the main program.) Each make of controller has its own set of symbols to use. Whatever the make of controller, a method must exist to (1) identify the subroutine (its "name," which could be a symbol or the sequence number of its first block); (2) specify the subroutine's content, usually by identifying the first and last blocks in the subroutine; and (3) a means to call up the subroutine for execution.

A subroutine is simply a section of the program that is "marked" and set aside for later execution. The controller skips over a subroutine and does not execute it until a certain command is issued that calls up the subroutine for execution.

Another symbol, such as the equals sign (=) or the letter C, is used to call up or execute the subroutine. At whatever point in the main program subroutine number 1 needs to be executed, simply enter a =#1 or C1 command. The controller will branch out from the main program (remembering where it left from) to the specified subroutine and execute all of the commands from the # symbol to the $ symbol. The controller will then return to the main program at the block following the point from which it left.

A subroutine can have embedded within it a command to call up yet another different subroutine. This is called nesting. Depending on the controller, subroutines can be nested up to three or more levels "deep."

Back to the top of this page


A loop is a command or a series of commands that are repeatedly executed continuously a specified number of times. Suppose you were writing a program for a CNC machine to drill a linear array of fifty 1/8-inch-diameter holes 1/4 inch apart. This would require fifty positioning commands, right? Not with CNC! Simply enter the G81 hole-drilling command and move the spindle to the location of the first hole, drilling a hole there. Then enter a G91 command to change the positioning to the incremental mode and "tell" the controller to move the cutter over 1/4 inch--and do it a total of 49 times. Each time the spindle is moved over a quarter inch, a hole will be drilled. Fifty holes with only two commands!

A direction could have been placed at the end of this chapter directing the reader of this textbook to walk 100 feet to the south and reread the chapter, and repeat the sequence until the chapter had been read four times. The net result is that the chapter would be read in four different locations, each 100 feet apart. This would be an example of a loop command. The direction could just as well be placed at the beginning of the chapter, directing the reader to read the chapter, then move 100 feet to the south, and read it again, repeating the sequence until the chapter had been read four times. Irrespective of where the command was located in the chapter, both methods would accomplish the same result.

A looping command in a CNC program must tell the controller either (1) where to loop back to, if the command is placed at the end of the loop or (2) where to loop back from, if it is placed at the beginning of the loop. The location of the looping command establishes the beginning (or end) of the loop. The looping command must specify the end (or beginning) of the loop. In addition, the command must specify the number of times the loop is to be executed, not the number of "repeats." The number of executions is always one greater than the number of repeats.

Most CNC controllers permit loops to be embedded (nested) within loops, up to three or more levels deep (depending on the controller). Likewise, loops can have nested subroutines and subroutines can have loops. Loops within loops, subroutines within subroutines, loops within subroutines, and subroutines within loops--all can be nested to a total level of three or thirty-two deep, or whatever the particular control's limit is.

Back to the top of this page

Next:  Four Essential N/C Program Elements

Back to Contents Page

Updated Jan. 9, 2002
Copyright 1988-2002 by George Stanton and Bill Hemphill
All Rights Reserved