Using Variables and Constants in Smart Pascal


A variable is named memory location in which a program can store one or more data values. On this page we describe some basic types of variable in which a program can store only one value. Later we will cover composite data types such as arrays and records. Commonly used basic types include integer, real, Boolean and string.

When we declare a variable we reserve a space in memory for it. We must indicate its type so that the correct amount of space is reserved. For example,

  FrameNumber : integer;

We can initialise a variable when we declare it with code such as:

  FrameNumber: integer = 1;

When we give a variable a value we assign a value to it, usually using the assignment operator :=. For example,

Gravity := 10;

Integer Variables

Integer variables hold whole numbers. They are useful for storing counters and positions of shapes, images and text on the screen. Although modern graphical routines such as FillRectF can handle fractional, floating point (real) coordinates, integers can be processed in ways that cannot be applied to numbers of type real.

For example, program Motion1 has the declaration:

  MobX : integer; //Distance of mobile from left of GameView is variable.

Floats (Real Variables)

A number having a decimal part is known in Computing as a real number or a float. If a number may not be a whole number (for example when its value is the result of division) then you must declare it as being of type float or type real. (If you declare it as real you need to have System.Types in the uses clause). In PlatformDemo, Steven Binns declares the real variables x, y, xSpeed, ySpeed and grav.

String Variables

String variables are used to hold single characters or multiple characters. Literal strings are usually enclosed in single quotes as in these initialisations and in Smart Mobile Studio you can use a double quote also.

  InvaderColour: string = 'pink';
  ScoreText: string = 'Score: '; //The score will be converted to an integer and appended
                                 //to ScoreText before it is displayed.

Boolean Variables

Boolean variables take only the values true and false. If your object is either moving left or moving right then you will set the variable MovingRight to false or true, respectively.

Where to declare a variable

A variable declared just above the implementation keyword (in the interface section) can be accessed from another unit. A variable declared below the implementation keyword cannot be accessed from another unit. Both of these are kinds of global variable. A better place for most variables to be declared (but less easy for beginners) is in the private section of a class (as in InvaderJS). In this case the var keyword is not required and may not be accepted.


It is good practice to declare constants for unchanging values that you use in programs rather than hard code the values wherever they are used. For example, in program Motion1:

const  //Write constants in upper case
  MOB_WIDTH = 20;    //Width of mobile stays at 20 pixels
  MOB_HEIGHT = 15;
  MOB_Y = 100;  //Distance from top of GameView to mobile is 100 pixels

It is easy to locate and change these values in the source code when they are named constants.

Rules for Naming Identifiers

The same rules as for Free Pascal and Delphi apply. This table gives error messages provided by the Smart Pascal compiler.

Erroneous Variable Declaration Error Message
2LittleBirds: string; Number, point or exponent expected (found "L")
begin: Boolean; END expected
repeat: Boolean; END expected
box#: string; Invalid character (found "#")
enough?: Boolean;

Colon ":" expected
END expected

Programming - a skill for life!

How to learn the Smart Pascal language the fun way by making games. Use Blockly blocks at first if coming from Scratch.