2.4 Vacant or Empty Variables
In many cases, a variable may also be undefined. In this case, the variable is set to #0, which identifies a null variable (empty / not initialized). Indeed #0 is a read-only variable used mainly for two purposes:
- check if a variable has been initialized
- reset a variable
At the beginning of the compilation every non system variable is set to #0.
A null variable has no value, it should not be confused with a variable that has a zero value.
#101 = 0 ; Variable #101 has a zero value #102 = #0 ; Variable #102 is vacant (empty), has no value and cannot be used for some operations
The following piece of G-code provide an example of the operations that can be used with an empty variable
( © 2018 by RosettaCNC Motion ) ( NULL paramater handling example: ) ( Every parameter is set to NULL when not initialized ) ( Parameter #0 stores the value NULL and cannot be written ) ; Comparing a null to a null variable: ; Define #1 as null (that means that is is empty/not initialized) #1 = #0 IF[#1 EQ #0] THEN M109 P"IF[#1 EQ #0] Should return TRUE" IF[#1 NE #0] THEN M109 P"IF[#1 NE #0] Should return FALSE" IF[#1 GT #0] THEN M109 P"IF[#1 GT #0] Should return FALSE" IF[#1 GE #0] THEN M109 P"IF[#1 GE #0] Should return TRUE" IF[#1 LT #0] THEN M109 P"IF[#1 LT #0] Should return FALSE" IF[#1 LE #0] THEN M109 P"IF[#1 LE #0] Should return TRUE" ; Comparing a zero to a null variable: ; Define #1 as zero that means #1 is equal to 0 #1 = 0 IF[#1 EQ #0] THEN M109 P"IF[#1 EQ #0] Should return FALSE" IF[#1 NE #0] THEN M109 P"IF[#1 NE #0] Should return TRUE" IF[#1 GT #0] THEN M109 P"IF[#1 GT #0] Should return FALSE" IF[#1 GE #0] THEN M109 P"IF[#1 GE #0] Should return TRUE" IF[#1 LT #0] THEN M109 P"IF[#1 LT #0] Should return FALSE" IF[#1 LE #0] THEN M109 P"IF[#1 LE #0] Should return TRUE" ; Comparing a null variable to a zero: ; #1 is defined as null (that means #1 is vacant) #1 = #0 IF[#1 EQ 0] THEN M109 P"IF[#1 EQ 0] Should return FALSE" IF[#1 NE 0] THEN M109 P"IF[#1 NE 0] Should return TRUE" IF[#1 GT 0] THEN M109 P"IF[#1 GT 0] Should return FALSE" IF[#1 GE 0] THEN M109 P"IF[#1 GE 0] Should return TRUE" IF[#1 LT 0] THEN M109 P"IF[#1 LT 0] Should return FALSE" IF[#1 LE 0] THEN M109 P"IF[#1 LE 0] Should return TRUE" ; Comparing a zero to a zero: ; #1 is defined as zero (that means #1 is equal to 0) #1 = 0 IF[#1 EQ 0] THEN M109 P"IF[#1 EQ 0] Should return TRUE" IF[#1 NE 0] THEN M109 P"IF[#1 NE 0] Should return FALSE" IF[#1 GT 0] THEN M109 P"IF[#1 GT 0] Should return FALSE" IF[#1 GE 0] THEN M109 P"IF[#1 GE 0] Should return TRUE" IF[#1 LT 0] THEN M109 P"IF[#1 LT 0] Should return FALSE" IF[#1 LE 0] THEN M109 P"IF[#1 LE 0] Should return TRUE" ; Comparing a positive number to a null variable: #1 = 0.1 IF[#1 EQ #0] THEN M109 P"IF[#1 EQ #0] Should return FALSE" IF[#1 NE #0] THEN M109 P"IF[#1 NE #0] Should return TRUE" IF[#1 GT #0] THEN M109 P"IF[#1 GT #0] Should return TRUE" IF[#1 GE #0] THEN M109 P"IF[#1 GE #0] Should return TRUE" IF[#1 LT #0] THEN M109 P"IF[#1 LT #0] Should return FALSE" IF[#1 LE #0] THEN M109 P"IF[#1 LE #0] Should return FALSE" ; Comparing a negative number to a null variable: #1 = -0.1 IF[#1 EQ #0] THEN M109 P"IF[#1 EQ #0] Should return FALSE" IF[#1 NE #0] THEN M109 P"IF[#1 NE #0] Should return TRUE" IF[#1 GT #0] THEN M109 P"IF[#1 GT #0] Should return FALSE" IF[#1 GE #0] THEN M109 P"IF[#1 GE #0] Should return FALSE" IF[#1 LT #0] THEN M109 P"IF[#1 LT #0] Should return TRUE" IF[#1 LE #0] THEN M109 P"IF[#1 LE #0] Should return TRUE" m2 ( Program End)