• TDG

Use of Local v. Common Variables

Updated: Sep 7, 2018


I write a lot of macro and parametric programs during the course of my daily work. As part of my operator and machine training, I always spend the extra time to introduce users to the use of Common and Local variables and the advantages/disadvantages of using them as part of their programming conventions. You’ll seldom see any CNC program with my name associated with it that isn’t littered with variables; primarily with the purpose of simplifying complex operations or controlling machine movement to eliminate potential crashes. That said, you’ll seldom ever see me use a COMMON VARIABLE as part of my program for anything other than a confirmation of a local variable calculation. To expand on this, I will use the following code as an example:


BORE = 1.0

XFIN = 2.0

DIFF = [XFIN – BORE]

V1 = DIFF


The BORE, XFIN and DIFF are examples of an Okuma local variable. Unlike the #5001 format used on FANUC and similar style controls, Okuma allows the programmer to create a label and define that label with a value. I’ll skip the lesson on passing values into sub-programs from a macro call for another day and stick to the specifics of this topic.


The values for these local variables in this example will be commanded directly from the G-Code program as shown in this example where BORE = 1.0, XFIN = 2.0 and DIFF is a calculation of the difference between XFIN minus BORE. Since the value of DIFF is a calculation, there is no direct value output location to view the results of the calculation using local variables; this would be the disadvantage point of the local variable. While this example reflects a simple math calculation, when more complex computations are required, it is useful to see your calculation values as an output. In this example, I am using the COMMON VARIABLE, V1 to display my calculation of the DIFF local variable. V1 serves no purpose to my actual cutting file but does allow me to see that the difference between XFIN minus BORE is = 1. The advantage to the local variable is in the protection of the calculation such that if I use the value of V1, there is nothing protecting that value from an external change.


If you browse the internet for macro program examples, you will find countless examples of creative and extremely well written macro programs using COMMON VARIABLES as their calculation engine. I’m certainly not proclaiming the programmer or method are incorrect in any way; however, having values that can be inadvertently over-written either by CNC code, direct operator input, or via an application writing directly to the COMMON VARIABLE by the Okuma API can create problems.


As a general rule, I use the following methods when at all possible (and it isn’t always possible)

  • I use local variables in all of my CNC programs unless there is just absolutely no other solution

  • I use COMMON VARIABLES in my CNC programs strictly for diagnostics or confirmation of a calculation

  • If I write a value to a COMMON VARIABLE as a last resort, I immediately capture that value into my program as a local variable

  • If my local variable need to be changed per program cycle, I use a G/M code macro and pass that value through to my program as a parameter of the macro (I will cover this subject another time)

Like I said, there is nothing wrong with either method, I just prefer one method over another and that is the point of this post. If you are going to define a COMMON VARIABLE value inside your CNC program and then read it into your code, you are just as well to use a local variable and avoid the potential risks of using a COMMON VARIABLE.


Thanks for reading!

27 views