Skip to main content

Local variables

Each G code file can declare local variables. For main programs, the declarations have to be inserted at the very beginning; for subprograms, directly after the subprogram declaration.

Local variables are visible only in the program or subprogram where they are declared. (No dynamic scope)

Tip

Local variables work in the online decoder only (not in the CNC editor).

Number of local variables

  • Before version 4.18.0.0: The number of local variables is restricted to 21 per subprogram and in the main program.

  • Version 4.18.0.0 and higher: The number of local variables is limited only by the memory. The maximum value can be changed using the library parameter SMC_CNC_LibParams.MAX_SUBPROGRAM_PARAMS.

    For more information, see: Library Parameters

Syntax for the declaration

The syntax is similar to the syntax used to declare subprogram parameters. One variable can be declared per block. The block does not start with an N-word. The variable can be provided with an optional initial value when it is declared. Otherwise, it is assigned a default value depending on the data type (LREAL: 0, BOOL: FALSE, STRING: ‘‘).

Syntax of the declaration: LET <FormalParam> [:= <InitialValue>].

<FormalParam> ::= <ParamName> : <ParamType>
<ParamName>   ::= #[a-zA-Z0-9_]+

<ParamType> ::= LREAL | BOOL | STRING ; String mit maximaler Länge von 255 Bytes

<InitialValue>: Expression that has a value and a type that matches the variable. The expression may also use local variables (and in subprograms the parameters of the subprogram), but only those declared above in the program code.

Examples

•   LET #x : LREAL            (* Variable #x, Typ LREAL, Initialwert 0 *)
•   LET #y : LREAL := #x + 1  (* Variable #y, Typ LREAL, Initialwert #x+1 = 1 *)
•   LET #b : BOOL := #x >= #y (* Variable #b, Typ BOOL, Initialwert FALSE *)

As with subprogram parameters, local variables are not case-sensitive. (Both #x and #X indicate the same variable.) The names of all local variables declared in a program/subprogram have to be different. They have to differ from the names of the formal parameters of the subprogram.

Local variables can be used in G code like parameters of subprograms.

Examples

•   N10 G01 X#x Y#y
•   N20 G20 L10 K#b