Lesson 3 -- Constants, Types  & Variables

In the next few chapters we will cover a large chunk of material. Much of it may not make immediate sense to you. You will find that as you continue on in later chapters the material covered in these chapters will begin to make more sense. For this reason you should not skip over this chapter even if it doesn't all sink in at once. Later, you can refer back to these chapters to help you fully grasp the concepts. This lesson covers the following subjects:

  • Constants
  • Types
  • Variables


Constants are the method by which you can assign a name to a value that you will use throughout your source code. There are two main reasons you would use a constant. The first reason is that it makes it easier to read your code. For example, the internal unit used within DCAL and DataCAD is 1/32 of an inch. If you wish to convert a value to inches you would need to multiply it by 32. If you want to convert it to feet it needs to be multiplied by 384. It is a common practice in DCAL to name constants in your code to correspond to these values. This way when you are reading your code it is more obvious why a value is being multiplied.

Here is a sample of some code:

  CONST ! here is the Constants section
    tofeet = 384.0;
    toinches = 32.0;
    x := 3.0 * tofeet;
    x := 3.0 * 384.0;

Each of the lines in this partial listing mean the same thing. You can use either method but the line that relies on the constant 'tofeet' is easier to read and determine why you are multiplying the value.

The other reason to use a constant would be to represent a value that will be used in many places in the code and may need to be changed later. For example lets say you write a macro that draws circles. You want to limit it so that it only draws 3 circles. You could declare a constant called maxCircles and set it to 3. Anywhere in your program that you need to check the number of circles it should draw it would use the constant maxCircles instead of the number 3. This isn't that big a deal until you later decide to up the number of circles you can draw to 5. If you had used the number 3 in your code you would need to hunt down every place the number 3 appears and change it to a 5. If you use the constant you can simply change the constant declaration to equal 5 and recompile the program.

In the example above notice the CONST keyword is used to indicate to the compiler that you are about to declare some constants. Remember from the previous lesson that a keyword is reserved to indicate a specific thing to the compiler. After we tell the compiler we are declaring constants we then list each constant in the following format:

  constant name = value;

The key here is that unlike in a statement (as you will soon see) constants use the simple equal sign and not the assignment operator. Be sure to end each declaration with a semi-colon. Constant values may only be integers or reals.

You can declare your own constants for any reason you like. DCAL also has many predefined constants that you do not need to declare and may use in your code. Examples of predefined constants are pi and ltype_solid. Pi is the constant to represent the mathematical pi (3.141592654) and ltype_solid is the constant value that is used when you are in need of the solid line type. There are many more predefined constants in the DCAL manual.

< Previous   |   Continue >

Table of Contents