Description: |
| | A Constant Declaration is a compiler directive that declares that an identifier has a specific value. |
Syntax: |
| | The following shows the format of the Constant Declaration syntax.
| Basic | Const <constname> = <constvalue> |
| C | const <constname> = <constvalue>; |
| Java | final <constname> = <constvalue>; |
The following table lists the elements of the Constant Declaration syntax.
| Element | Description |
| <constname> | Name of the constant. Follows standard naming conventions. |
| <constvalue> | Value to be assigned to each place in program code that the <constname> is encountered. |
|
Operation: |
| | During compilation, when a Constant Declaration is encountered in the program's source code, the identifier specified by the <constname> and the value specified by the <constvalue> element are added to a list of constant values. Then, each subsequent time that the constant's identifier is encountered in the program's source code, the specified value is substituted in the compilation. The resulting compiled code would be the same as if the text of each occurrence of the identifier in the program's source code was replaced with the text of the value. |
Remarks: |
| | Constants can make your programs self-documenting and easy to modify. Unlike variables, constants can't be inadvertently changed while your program is running and they do not take up any RAM storage space. |
Example: |
| | In the following example programs, a Constant Declaration is shown that declares "DelayValue" to be equal to 100. This constant is then used to control the rate that an I/O Line changes state. If the rate needs to change, all the programmer needs to do is to change the value in the Constant Declaration, and both places that it is used will now use the new value.
| Visual Basic Syntax | C Syntax |
Dim A As New oDio1
Const DelayValue = 100
Sub Main()
A.IOLine = 31
A.Direction = cvOutput
Do
A.Set
ooPIC.Delay = DelayValue
A.Clear
ooPIC.Delay = DelayValue
Loop
End Sub | oDio1 A = New oDio1;
Const DelayValue = 100;
Sub void Main(void){
A.IOLine = 31;
A.Direction = cvOutput;
Do{
A.Set;
ooPIC.Delay = DelayValue;
A.Clear;
ooPIC.Delay = DelayValue;
} while (1);
} |
| Basic Syntax | Java Syntax |
A As oDIO1(31,cvOutput)
DelayValue Con 100
Do
A.Set
ooPIC.Delay = DelayValue
A.Clear
ooPIC.Delay = DelayValue
Loop | oDio1 A = New oDio1;
Final DelayValue = 100;
Sub void Main(void){
A.IOLine = 31;
A.Direction = cvOutput;
Do{
A.Set;
ooPIC.Delay = DelayValue;
A.Clear;
ooPIC.Delay = DelayValue;
} while (1);
} |
In the following examples, a constant value of 5 is defined with the name MaxNum.
| Visul Basic | Const MaxNum = 5 |
| Basic | MaxNum Con 5 |
| C | Const MaxNum = 5; |
| Java | Final MaxNum = 5; |
|
Version History and Bug List: |
| | Firmware Ver A1: Introduced. Bugs: No known bugs. |