Macro constants

Top  Previous  Next

By using the #DEFINE macro functionality, it is possible to implement constant values and constant strings that can be referenced with symbolic names in the program. This increases program maintainability and reduces programming errors as a given value will only need to be changed in one central location instead of being scattered throughout the entire program code.

 

Constants can be very useful in VPL programming whenever you have any value that is repeated in your program. Declaring a constant allows one to quickly and easily change a value that is used throughout your code simply by changing the declaration.

 

 
Declaring constants

 

#DEFINE MY_NAME         "Candy Girl"

#DEFINE AGE                18
 

 
The #DEFINE directive is followed by the name of the symbols being defined - MY_NAME and AGE.

These symbols are named like variables, although using all caps for constants allows one to easily identify constants versus variables in your source code. The symbol must all be one word. Following the symbol is a space and then the value that the symbol represents - in this case the string "Candy Girl" and value "18".

 

 

Using constants in your code

The example below demonstrates the use of the constants declared earlier:

 

INCLUDE rtcu.inc
 
PROGRAM test;
 
DebugMsg(message:="My name is "+MY_NAME);
DebugFmt(message:="My age is \1 years",v1:=AGE);
 
END_PROGRAM;

 

This simple example will write out the debug messages: "My name is Candy Girl" and "My age is 18 years".