Introduction to Variables

 

Variables Fundamentals

 

Introduction

To process the information of your program or the requests that your program makes to the user, the computer uses two types of storage spaces. The hard drive is a static storage area that keeps its information all the time, almost regardless of what happens to your computer. Another type of storage used by the computer is referred to as Random Access Memory (RAM). This storage area keeps its information only when the computer is turned on. This means that the RAM looses its Information when the computer is turned off. In the strict sense, when the user “opens” or launches a program, part of the program “goes” into the RAM; if the application is not used anymore, which means that if the user “closes” the application, the part of memory that the application was using in the RAM is gone and the memory space that the application was using becomes available (this is the way it is supposed to be).

As an application developer, you will decide what types of information are necessary for your application and how these things would be used. When creating an application, you provide these pieces of information to the computer. The computer then puts them together. When your program opens, part of your application gets “loaded” into the RAM. When the user is using your application, the information that your application requests also goes into the RAM while your application is processing such requests. Because your program will be made of many of these things, the computer needs to know what these things would be and how much space each one of them would need. Because such a thing can change (vary) throughout your program, it is called a variable.

The amount of memory space necessary to store a variable is also referred to as a data type. A data type provides two valuable pieces of information to the compiler: the amount of space the variable will use and the kind of information that will or can be stored in that space.

After declaring a variable, the compiler reserves a space in memory for that variable:

Variable

A variable, any variable, occupies more that one small “cell” of space in memory. It will always be your responsibility to decide how much space a variable needs, based on your goal.

Variables Names

When using the various necessary variables in your programs, you will need to identify each one of them. The Object Pascal computer language provides rules for naming items in your program. The name of a variable:

  • Can start with an underscore “_” or a letter, lowercase or uppercase, such as a letter from a to z or from A to Z. Examples are Name, gender, _Students, pRice
  • Can include letters, underscore, or digits. Examples are: keyboard, Master, Junction, Player1, total_grade, _Score_Side1
  • include special characters such as !, %, ], or $
  • Cannot include an empty space
  • be any of the reserved words
  • not be longer than 32 characters (although allowed)
  • name can consist of one word such as country. A name could also be a combination of more than one word, such as firstname or dateofbirth.

The Pascal (and Object Pascal) language has a list of words reserved for its own use and you must not use any of these words to name your variables. Do not use the words in the following list to name anything in your program:

Object Pascal Reserved Words
absolute
abstract
and
array
as
asm
assembler
automated
automatic
begin
case
cdecl
class
const
constructor
contains
default
destructor
dispinterface
div
do
downto
dynamic
else
end
except
export
exports
external
far
file
finalization
finally
for
forward
function
goto
if
implements
implementation
in
index
in
inherited
initialization
inline
interface
is
label
library
message
mod
name
near
nil
nodefault
not
object
of
or
out
overload
override
package 
packed
pascal
private
procedure
program
property
protected
public
published
raise
read
readln
readonly
record
register
reintroduce
repeat
requires
resident
resourcestring
safecall
set
shl
shr
stdcall
stored
string
then
thread
var
to
try
type
unit
until
uses
var
virtual
while
with
write
writeln
writeonly

Pascal is not case-sensitive; this means that the words CASE, Case, case, and Case are equivalent. To make your programming experience easier and personal, you can add your own rules to those above. Some (most) companies also adopt a naming convention throughout their projects.

In this site, a name will:

  • Start in uppercase, whether it is a variable, a function, a structure, or a class. In some situations, a name will start with an underscore, when useful. Examples are Country, Printer, _Number
  • Start each element of its combined names in uppercase. Examples are FirstName, DateOfBirth 

 

 

Variable Declaration

A variable is an area of computer memory used to store data. Before using a variable, you must first let the compiler know. Letting the compiler know about a variable is referred to as “Declaring” the variable. The compiler needs two pieces of information concerning each variable: a name for the variable and the amount of memory space the variable needs.

The syntax of declaring a variable is:

var VariableName : DataType;

A variable is declared outside of (and before) the begin and end block in which the variable would be used. The var keyword is required; it lets the computer know that you are declaring a variable. The VariableName represents the name of the variable and must abide to certain rules.

When declaring a variable, the VariableName is followed by a colon. In fact, the colon lets the compiler know that a variable has been declared and you are now going to specify the data type. The DataType lets the compiler know how much memory space the variable would need and the DataType ends the declaration of a particular variable. If you plan to use just one variable of a certain type, you can use the syntax seen earlier. In the same way, if you plan to declare other variables, you can use each declaration with the var keyword. The syntax you use would be:

var OneName : ADataType;
var AnotherVariablee : ItsDataType;
var OneMoreVariableName : AnotherDataType;

On the other hand, if you are planning to use various variables of the same type, you can declare more than one after the var keyword but before the colon. The variables must be separated with a semi-colon. After the colon, specify the data type that those variables are using. Therefore, the syntax to use would be:

var Variable1, Variable2 : DataType;

or

var Variable1, Variable2, Variable_n : DataType;

Instead of typing the var keyword for each declaration, Pascal allows you to create a declaration section. Such a section starts with the var keyword followed by the desired declarations. By convention, the var stands on its line and the formal declarations follow on the subsequent line. By tradition also, the declarations are indented by 4 characters. The declarations above can be resumed as:

var
OneName : ADataType;
AnotherVariable : ItsDataType;
OneMoreVariableName : AnotherDataType;

The declaration can also be done as follows:

var
AVariable : ADataType; 
Variable1, Variable2 : DataType;
Variable3 : ItsDataType 
Variable4, Variable5, Variablen : DataType;

User Input: The read and readln Procedures

Besides the write and writeln procedures, Pascal is equipped with two procedures to perform opposite assignments. While the write and writeln procedures are used to display something on the screen, Pascal uses the read and the readln procedures to get values from the outside. This is usually done by the user typing values using the keyboard. The read procedure requests a value from the user and stores it in the computer memory. The syntax of the read procedure is:

read(VariableName)

The readln procedure also requests a value from the user but sends the cursor to the next line. Its syntax is:

readln(VariableName)

So far, we have learned to use the writeln procedures to display things on the screen. Besides the strings we were displaying, you can pass the name of a variable. For example, if you have a variable called HourlySalary ant want to display its value on the screen, you can type its name between the parentheses of the write or writeln procedures. The compiler would take care of the rest. This is done as follows:

write(HourlySalary);

If you wish to append a new line after displaying the variable name, you can use the writeln procedure instead of write.


Previous Copyright © 2004 FunctionX Next