Introduction to Pascal | |
Overview of the Programming Process | |
Basic Input and Output | |
The CRT Unit | |
Conditional Statements | |
Loops | |
File Handling | |
Subprograms | |
Data types | |
BGI Graphics |
Index of Pascal Reserved Words | |
Index of CRT Colours | |
Index of Acronyms | |
Useful Links | |
Table of ASCII Codes | |
Tutorial Tree |
In the late 1950s, an international committee led by Peter Naur developed ALGOL, a high-level language intended for scientific computing. This language was designed to be platform-independent, making it more flexible, but also making it harder to write compilers for it on the various platforms it supported. ALGOL was mostly abandoned by programmers, except to describe algorithms (hence the name, ALGOrithmic Language).
During the next decade, several computer scientists worked to develop ALGOL to improve it and eliminate its drawbacks. Among them was Dr. Niklaus Wirth of the Swiss Federal Institute of Technology, who in 1971 published his specification of Pascal, a new high-level language named after Blaise Pascal, the 17th-century mathematician and philosopher who in 1642 invented an adding and subtracting machine, i.e. one of the first forms of computer.
Nowadays, Pascal has mostly withered from the professional programming scene. However, although obsolete compared to the languages there are today, Pascal is nowhere as forgotten as ALGOL, from which it was forged. Its syntax, mainly based on simple English, makes it very easy to understand, and thus Pascal is mainly used nowadays to introduce students to programming.
What you will be writing in Pascal is called the source code, i.e. it is the code written by the programmer in a particular programming language.
The source code needs to be translated into machine language (executional code or object code) that the computer can understand in order to be executable. The source code alone is just text; the actual program is the translated code brought to life in machine language.
A compiler is a program which translates source code to object code. Therefore you will need a compiler to program in Pascal. There are a number of free compilers around on the internet (such as Free Pascal or Dev-Pascal), but the one used as standard is Borland Turbo Pascal. Pascal code that works with Turbo Pascal is not guaranteed to work with other compilers.
Borland has released up to version 5.5 of Turbo Pascal for free at their software museum, but you will need to create a Borland user account to be able to download it. The latest version of Turbo Pascal is 7.0 (which is obviously much better than 5.5), but it is not free.
Once you write your source code in the compiler, you need to compile the source code to translate it into machine language. After compilation, you may run the program to test it. If compilation is not successful and an error is shown, you will need to debug the code to make the program work.
Note that if you get a message like "Error 200: Division by Zero"
,
then the problem is with the compiler, not with your code. Search for
"Pascal patch" in your favourite search engine and download the patch. Run it
to fix the error.
There are three kinds of errors encountered when programming:
Syntax errors are errors in the code you write so that the compiler cannot understand your code, and the code will not compile. This usually involves misspelling a command or forgetting a semicolon at the end of a statement. | |
Runtime or execution errors are errors halting the flow of the program after it is compiled and run. This usually happens when the wrong type of variable is given in input (e.g. inputting text where an integer should be entered). | |
Logical errors are errors in the sequence of instructions or in the methods of calculation which do not halt the program but produce the wrong results. |
When any error is encountered, you will need to debug the program, finding and eliminating errors. The compiler will usually tell you the line in which the error occurred, and it will also tell you what went wrong. That will help you find errors and fix them.
Any computer language allows you to write comments in your code, i.e. to make notes about what each part of the code does. This will not increase the size of the output program, and is very useful. If you go back to look at your code after a long time, it will be very difficult to understand what you did; comments help you to recognise what your code does.
There are two ways to write comments in Pascal:
{this is a comment}
(*
this is another comment
*)
Both methods work the same way and may be used on single or multiple lines
alike. {
or (*
begins the comment, and }
or *)
ends it.
The basic program begins with actually giving the program a name (optional),
using the program
keyword followed by the name
you want:
program calculator;
Almost every statement ends with a semicolon.
The actual instructions of the program go between the begin
and end
keywords:
program calculator;
begin
end.
Putting a semicolon after begin
is optional,
because it is the beginning of the main function; it is not a statement or
command that is used on one line. The program always ends with a full stop.
The write()
or writeln()
functions are used to write
text on the screen. The only difference between them is that writeln()
skips a line after what it displays, while write()
keeps going
on the same line.
To display text, put a text string in the brackets, enclosed by single quotes:
writeln('What is your favourite song?');
write('Nevermind. I do not want to know.');
You can use writeln
to skip lines. Writing it without the brackets
will skip a line without writing anything to the screen:
writeln;
The fact that text is enclosed in single quotes means that you cannot use an apostrophe within the string, or the string will end prematurely and give an error.
Write the apostrophe twice if you want to use it within the string:
write('It''s Raining!');
You may concatenate or join two separate strings within the
write()
or writeln()
functions using a comma or
plus sign. It is best to use the comma though, because while the plus sign will
concatenate text, it will add if you are writing a numeric variable.
In the space between the program
name and the
begin
point go many important declarations about
variables, constants and other things to be used by the program. Among these
are, of course, constants. These are numbers of text strings
whose value never changes, but is simply used without changing.
Constants go in the const
block, and have a
value assigned to them using the 'equals' sign. They may later be used with
write()
or other functions.
program constdemo;
const
pi = 3.141592654;
e = 2.718281828;
begin
writeln('The value of pi is ',pi);
end.
Note that in the writeln()
or write()
functions,
single quotes are only used to contain text. Numbers, constants, variables
and other data types should not be enclosed in quotes or will be treated as
text.
Variables are much like constants, with the difference that their value is subject to change.
Like constants, they are declared in the area between program
and begin
, and what you define is not their content
but their type.
A Pascal variable can be of any of the following types:
integer - whole number within the range -32768≤x<32766 | |
longint - larger integers | |
real - decimal numbers | |
char - a single character (requires quotes) | |
string - string of characters of variable length (up to 255 characters, requires quotes) | |
string[x] (where x is an integer less than or equal to 255) - fixed length string (requires quotes); string can be shorter than the specified character length, but will be truncated to the first x characters if the string is longer | |
boolean - boolean values involving true and false values |
Note that these data types can also be specified for constants:
const
users_age:integer=20;
pi :real =3.141592654
The following example demonstrates how to declare variales and use them in output.
program outputpi;
var
author_name:string[4];
pi :real;
begin
author_name := 'John';
pi := 3.141592654;
write('Hi, my name is ', author_name, '!');
write('The value of pi is ', pi, '.');
end.
As you can see above, values are assigned to variables with the
assignation operator (:=
).
Note that you can save space by declaring multiple variables of the same type on the same line, using a comma to separate them.
var
day, year, age : integer;
pi, e : real;
name, month : string;
Note also that you do not have to stay declaring π or e.
A predefined function for π exists so all you have to do is
write()
it as follows. Another function exists for e,
which we will see later.
write(pi);
The read()
and readln()
functions work for input
pretty much like how write()
and writeln()
work for
output. A variable is specified in the brackets, and the user input is stored
in that variable:
var name : string;
begin
writeln('What is your name?');
readln(name);
writeln('Pleased to meet you, ', name, '.');
end.
Note that if the program vanishes before showing you the final output, add
another read
statement to ask for input before ending the program.
With numerical input, you can have several numbers inputted at a time; the user
will enter each number and press ENTER. This is done by entering
multiple variables in the read()
or readln()
function,
separating them with commas. Here is a practical example of its use:
program average_of_three_numbers;
var x,y,z:integer;
begin
writeln('Please enter three numbers:');
readln(x,y,z);
writeln('Their average is ', ((x+y+z)/3));
end.
Like any real programming language, Pascal can be used to perform arithmetic
operations. There are several symbols involved; we have already seen the use of
the assignation operator (:=
) and two of the arithmetic operations
are shown in the previous example. These are the arithmetic operators available
in Pascal:
Operator | Operation | Data type | Example | Answer to Example |
---|---|---|---|---|
+ | Addition | Real/Integer | 5+3 | 8 |
- | Subtraction | Real/Integer | 15-6 | 9 |
* | Multiplication | Real/Integer | 4*5 | 20 |
/ | Real Division | Real/Integer | 2.2/1.1 | 2.0000000000E+00 |
div | Integer Division | Integer | 81 div 9 | 9 |
mod | Modulus (Remainder) | Integer | 5 mod 2 | 1 |
The first three operators should be very straightforward. The Real Division
(/
) divides two numbers and gives the answer as a decimal,
irrespective of whether they were integers or decimals. The Integer Division
(div
), on the other hand, is used to divide two integers and
gives the answer as an integer. The Modulus (mod
) operator
returns the remainder of the division of two numbers.
You may use brackets to perform some operations before others. The following example illustrates the use of arithmetic operations within a program.
program tracknums;
var a,b,c:integer;
begin
a := 3; {a=3, b=0, c=0 }
c := (5*a); {a=3, b=0, c=15}
b := (c-a); {a=3, b=12, c=15}
a := (c mod b); {a=0, b=12, c=15}
c := ((c div 5) + (b-9)); {a=0, b=12, c=6 }
end.
Note that if you are outputting numbers which are to undergo arithmetical
operations, putting the operations in brackets in the writeln()
function is just as good as using the assignation operator:
a := 5;
b := (a+5);
writeln(b); {b=10}
a:=5;
writeln((a+5)); {10 is written}
integer
and char
are called ordinal
data types. Each integer or character has another before it (predecessor)
and another after it (successor). For example, the successor of
'a'
is 'b'
and the predecessor of 20
is
19
.
For the purpose of finding these predecessors and successors, you may use the following functions:
Function | Description | Examples | Output of Examples |
---|---|---|---|
chr() | Displays the character corresponding to the ASCII value specified |
| a |
ord() | Displays the ordinal value of the specified character or integer. In the case of integers, the integer itself is returned. In the case of characters, the corresponding ASCII value is returned. |
| 5 |
pred() | Returns the predecessor |
| 38 |
succ() | Returns the successor |
| 9 |
A number of advanced mathematical functions are available in Pascal, as illustrated below. Don't expect to understand them without post-secondary Mathematical knowledge.
Function | Parameter | General Output | Example | Result |
---|---|---|---|---|
sin() | angle in radians | sin x | sin(pi); | 0 |
cos() | angle in radians | cos x | cos(pi); | -1 |
arctan() | angle in radians | tan-1 x | arctan(0); | 0 |
exp() | number | ex | exp(1); | 2.7182818285E+00 |
ln() | positive number | ln x | ln(exp(1)); | 1.0000000000E+00 |
sqr() | number | x2 | sqr(-5); | 25 |
sqrt() | positive number | √x | sqrt(390625); | 625 |
round() | number | x rounded to nearest integer | round(2.7); | 3 |
trunc() | number | x rounded down to previous integer | trunc(2.7); | 2 |
abs() | number | |x| | abs(-5); | 5 |
It is often necessary to generate random numbers, especially when games are
involved. To generate random numbers, first initialise the generation of random
numbers using the randomize
function, then use the
random()
function to generate a random number between zero and
one less than the parameter of random()
, i.e. if the parameter is
n, then the range in which the generated random number lies is
0≤x<n and it is between zero and (n-1).
program guessnum;
var a,b:integer;
begin
randomize;
a:=(random(6)+1); {random number between 1 and 6}
writeln('Random number generated: ',a);
readln;
end.
You can specify a width for whatever you write()
by putting a
colon after the output:
writeln('Hi':10);
The above example will make the word 'Hi' take up ten spaces, so that eight extra spaces will be added before the word:
Hi
If you are using decimal (real
type) numbers, you can specify
the number of decimal places to show with an additional colon:
writeln(52.234567:1:3);
The 'whole number' part of the real number is displayed as is, no matter
whether it exceeds the first width (in this case 1
) or not, but
the decimal part is truncated to the number of decimal places specified in the
second width. The previous example will result in 52.234 being written.
One of the best things about high-level programming languages is that the range of functions available to you can be extended by using one or more libraries which contain the definitions of the additional functions they let you use.
In Pascal, these libraries are called units, and they are actually .TPU files in the UNITS folder of your Turbo Pascal compiler.
You can use units with the uses
keyword, which
goes in the area between program
and
begin
:
uses crt;
Once the unit us used, you may then use its functions.
One of the easiest units to use is the CRT unit. CRT stands for 'Cathode Ray Tube', and refers to the screen; it allows you to manipulate some features of the text screen such as text colour, text background colour, etc.
You can use the clrscr
function to clear the screen (equivalent
to the DOS cls command):
clrscr;
Simple as that. This statement clears the screen.
Most of the next functions we will be seeing use one or more parameters.
A parameter is what goes in the function's brackets, for example 'Hi'
is the parameter in writeln('Hi')
.
The textcolor()
and textbackground()
functions are
used to set the colour and background colour respectively of the text you
output. As a parameter you can specify either the name (doesn't need quotes)
or the corresponding number of the colour you want. There are
sixteen colours
you can choose from. Notes:
0
, 16
and 32
are all black).The following is an example of the use of these two functions.
program colourexample;
uses crt;
var a,b:integer;
begin
clrscr;
writeln('Enter text colour number you want');
readln(a);
textcolor(a); {Text colour changes to the value of 'a'}
writeln('Now enter background colour number');
readln(b);
textbackground(b); {Background colour changes to 'b'}
readln;
end.
The textattr
variable is like a combination of textcolor()
and textbackground()
. Its general format is thus:
textattr := (backgroundcolour*16)+textcolour
backgroundcolour and textcolour represent the background and text colour numbers you want.
The gotoxy()
function will move to a particuar position on
the screen, so that whatever you write will be written there. Its syntax is
gotoxy(x,y)
, where x is the
distance from the left side of the screen and y is the distance from
the top of the screen.
gotoxy(12,9);
This will move 12 spaces right and 9 spaces down from the top-left point of the screen.
The delay()
function will cause the program to wait for
s milliseconds between one command and the next, where s
is the parameter of the delay()
function:
delay(3000); {Wait for 3000 milliseconds (3 seconds)}
You can change the size of text using the textmode()
function.
This does not allow you to change the font size as you please, but rather
manipulates the width and height in characters of the screen, resulting in
stretched text.
The normal dimensions of the screen are 80 characters (wide) by 25
characters (high). Thus textmode(co80)
is the normal text mode.
Using textmode(co40)
will make only 40 characters fit in one line,
so text will be wider. You can also use textmode(font8x8)
, which
normally fits 50 lines instead of 25, resulting in text being squashed vertically.
Finally, textmode(lastmode)
switches to the last mode you used.
Here is a summary of parameters available for the textmode()
function:
co80 - 80×25 characters (normal) | |
co40 - 40×25 characters (200% text width) | |
font8x8 - (80 or 40)×50 characters (50% text height) | |
lastmode - previous text mode |
Note that the width of font8x8
depends on which other mode was
previously set.
readkey
is a kind of variable that is given a value when a key
is pressed.
uses crt;
var c:char;
begin
c := readkey;
writeln(c);
end.
When the user presses a key, the corresponding ASCII value of that key is
passed to readkey
, and is then assigned to the character variable
we declared earlier. When we write it, the ASCII value is converted back to
character format and written to the screen.
The halt
command can be used to end the program before it reaches
the end of the instructions. Whatever is after halt
will not be
executed.
halt
may also be used as a function, with an integer as a
parameter. This parameter is used as an exitcode, so that it is returned when
the program halts. Zero is the default exitcode if you use it as a command
(i.e. without brackets or parameters).
halt; {Halt with exitcode 0}
halt(1); {Halt with exitcode 1}
Note that halt
does not require the CRT unit to work.
With the CRT Unit, you can play with sounds. Not complex sound and
breathtaking music, but simple DOS beeps. You just have to use the sound()
function with a frequency as its parameter, delay()
the sound for
a number of milliseconds specified as its parameter, and then use nosound
to end the sound (otherwise it will continue beeping until you close the compiler).
It's fun. I tried making the 'Ready, get set, go' beeps they used to do in old racing games:
program racesnd;
uses crt;
begin
sound(100); delay(200); nosound;
delay(1000);
sound(100); delay(200); nosound;
delay(1000);
sound(400); delay(400); nosound;
readln;
end.
The if
statement allows you to determine the
program's next course of action depending on whether one or more conditions are
true.
Imagine the program asks the user for his age, and displays a message depending on what he entered. If his age is zero, then write "Are you sure?" If the age is between 0 and 40, then write "You're still young!" Otherwise, write "You're really old!"
program askage;
var age, absage:integer;
begin
writeln('Enter your age below.');
readln(age);
absage := abs(age); {To handle negative age values}
if (absage=0) then writeln('Are you sure?');
else if ((absage > 0) and (absage < 40))
then writeln('You''re still young!')
else writeln('You''re really old!');
readln;
end.
You should think of the conditional statement as one big command, so that a
semicolon is only used to end it all in the final else
statement, and not the statements after each then
.
Each single if
statement as it is only accepts
one command. If we were to use other commands besides writeln()
,
you would need to have a begin..end
block enclosing them.
if (absage=0) then
begin
writeln('Are you sure?');
writeln('Enter your age again');
readln(age)
end;
Note that in this case, each statement except the last one in the
begin..end
block uses a
semiconol, and a semicolon after end
ends the
block of commands. This end
ends with a semicolon
because it is only ending a block of commands. The end
that ends with a full stop only ends like that because it is ending the whole
program.
As we have seen, commands are executed depending on whether a condition is
true. The condition usually depends on whether one or more variables are equal
to a given value (like absage = 0
) or within a given range
(like absage > 39
). The variable, in each case, is
compared with the given value using a logical
operator.
The following logical operators may be used:
Operator | Description |
---|---|
< | is less than |
> | is greater than |
<= | is less than or equal to |
>= | is greater than or equal to |
= | is equal to |
<> | is not equal to |
Note that the logical operator =
is different from the
assignation operator :=
. One of the most common mistakes in every
programming language (apart from forgetting semicolons) is to mistake one for
the other. :=
is used to assign a value to a variable; =
compares a given value with that of a variable.
Four other boolean operators may be used to check whether a condition is true. They may be used to join multiple conditions or invert a condition, and the conditions they are associated with depend on whether the variable matches the specified value. In the following examples, numbers are used instead of variables to make things clearer.
not - returns true if the condition is false,e.g.
| |||||||
and returns true if both conditions are true, e.g.
| |||||||
or returns true if at least one condition is true, e.g.
| |||||||
xor (exclusive 'or') returns true if one condition is true and the other is false, e.g.
|
program askage2;
var mage, fage : integer;
begin
writeln('Enter your mother''s age below.');
readln(mage);
mage := abs(mage);
writeln('Now enter your father''s age.');
readln(fage);
fage := abs(fage);
if ((fage<20) and (mage<20)) then
writeln('Your parents are both really young.');
else if ((fage>=20) xor (mage>=20)) then
writeln('One of your parents is really young.');
else writeln('None of your parents are really young.');
readln;
end.
Using the if
statement can sometimes be
cumbersome, especially if there are lots of different possivilities of output
for each instance of the variable.
The case
statement is much neater in such cases, as it lists
instructions for each instance or range of the variable. Its general syntax is:
case variable of
instance1 : instruction1;
instance2 : instruction2;
else default_instruction;
end;
Here's an example of its use:
var mark:integer;
begin
writeln('What was your Physics mark?');
readln(mark);
case mark of
0: writeln('Hopeless!');
1..19: writeln('You''re serious?');
20..39: writeln('That''s a bad fail.');
40..49: writeln('You failed, but it was close.');
50: writeln('You just passed!');
51..59: writeln('You got a D.');
60..69: writeln('You got a C.');
70..79: writeln('Well done! That''s a B.');
80..99: writeln('Excellent!');
100: writeln('Perfect!');
else writeln('Huh?');
end;
readln;
end.
There are three ways to specify values in the case
statement:
Specific values (e.g. 100 ) | |
A range of values (e.g. 70..79 ) - in general, a..b means a≤x≤b | |
Multiple specific values (e.g. 70,75 ) - these are separated by a comma |
The else
part is optional, but is useful to handle
input outside the range we want it in (e.g. a mark greater than 100).
Note that if you want more than one instruction to be executed for each case,
you need to use a begin..end
block, just like with if
statements.
Sometimes it is necessary to repeat a command for a number of times. This is called looping, and there are three kinds of loop.
for..do
LoopThe for..do
loop repeats a
command while counting from one number to another. Its general syntax is:
for variable:=value1 to value2 do
instruction;
The program will count from value1 to value2 while executing the instruction.
Let's say we want to list all of the ASCII characters from zero to 255. Along with
the for..do
loop, we are going to
use the chr()
function. We have already said that its parameter is an
integer, and it returns the corresponding ASCII character of its parameter.
var charnum : integer;
begin
for charnum:=0 to 255 do
writeln(chr(charnum));
readln;
end.
If you want to count down (e.g. from 255 to 0), replace to
with downto
.
Note that a begin..end
block
is required for multiple commands, just like with conditional statements.
while..do
LoopThe while..do
loop is similar
to the for..do
loop. It repeats
the given instructions while a condition is true. This condition is usually
associated with a variable lying within a certain range, and it is up to you
to increase or decrease the variable in order to eventually break out of the
range and end the loop. If you never break out of the range, the loop will be
infinite.
program whiledoexample;
var x,c:integer;
begin
writeln('Enter a number');
readln(x);
x:=abs(x); {To take negative 'x' as positive}
c:=0;
while c<x do
begin
writeln('*');
c:=c+1;
end;
readln;
end.
This simple program takes an integer, x, as input, and outputs x asterisks.
repeat..until
LoopThe repeat..until
loop is
similar to the while..do
loop,
with the difference that the condition you specify is that which should break
the loop (as opposed to that which should keep the loop going, which you specify
with the while..do
loop), and that
this condition is written after the loop's instructions.
The while..do
block in the
previous example could be rewritten with
repeat..until
as follows:
repeat
begin
write('*');
c:=c+1;
end;
until c>=x;
To read from or write to text files, there are four main things you need to do:
assign()
a text file to a variable.close()
the file.So you first need to declare a variable of type text
. This is a
special data type purposely used for text files. You will be directly using this
variable when opening, reading/writing and closing the file.
var datafile:text;
Once your variable is declared, use the assign()
function to
associate your variable with a specific file. This function takes two parameters;
the first is the variable and the second is the name of the file.
assign(datafile,'data.txt');
Okay, so the first step is done. The next thing to do is decide what you are going to do with the file, because you will be using a different function depending on that (for all three functions the parameter is the file variable):
reset() opens the file for reading. | |
rewrite() opens the file for writing; all contents of the file are erased and new stuff is written. If the file does not exist, it will be created. | |
append() opens the file for writing; new content is added to the end of the file and the original contents of the file are not lost. |
After using one of these functions to open the file in a specific mode, you
will want to do whatever you planned to do with the file (read/write data). This
is done using the same functions you used for writing text to the screen
(read()
, readln()
, write()
and writeln()
),
with the only difference that as a first parameter they will take the file
variable. Note also that reading functions can only be used if the file was opened
with reset()
, and writing functions can only be used if either
rewrite()
or append()
was used.
write(datafile,'Hello!');
Once you're done, you need to close()
the file. Here's a full
example.
program email_address_book;
uses crt;
var datafile:text;
counter:integer;
name,email:string;
begin;
clrscr;
assign(datafile,'data.txt');
rewrite(datafile);
for counter:=1 to 3 do
begin;
{user inputs data}
writeln('Enter name ',counter,' out of 3');
readln(name);
writeln('Enter that person''s email.');
readln(email);
{data is written to file}
writeln(datafile,name);
writeln(datafile,email);
end;
close(datafile);
end.
This program asks you to input 3 people's name and email address. These are
written to a file in successive lines. Organising them by line makes it easier
to read them, as you can read by line too. Note that since rewrite()
is used, the contents of the data file are erased and replaced with new data.
If you want to read the entries, you can use the following example program.
program readfile;
var datafile:text;
counter:integer;
line:string;
begin;
assign(datafile,'data.txt');
reset(datafile);
for counter:=1 to 3 do
begin;
readln(datafile,line);
writeln('Name ',counter,': ',line);
readln(datafile,line);
writeln('Email ',counter,': ',line);
writeln;
end;
close(datafile);
end.
eof()
functionWhat if my program allows new entries to be appended to the file so that I
don't know how many records there are? In such a case, using a for
loop between two constant values (line 1
and 3
above)
is useless. Instead, you can read line by line until you reach the end of the
file, which is where the eof()
(end of file) function returns true.
The example program below is a rewrite of the program above, using the
eof()
function.
program readfile2;
var datafile:text;
line:string;
linecounter:integer;
begin;
assign(datafile,'data.txt');
reset(datafile);
linecounter := 1;
while eof(datafile) = false do
begin;
readln(datafile,line);
linecounter := linecounter + 1;
{Odd-numbered lines are names; even-numbered lines are addresses}
if ((linecounter mod 2) = 0) then
write('Name ')
else
write('Email ');
{Write line number followed by the actual name/email}
writeln((linecounter div 2),': ',line);
{Skip a line after each email}
if ((linecounter mod 2) = 1) then
writeln;
end;
close(datafile);
readln;
end.
eoln()
functioneoln()
is similar to eof()
, but instead marks the
end of a line. The following program is an example of its use. Try using it with
the sample text file shown after it. After compiling and running the code as it
is, replace eoln
with eof
and see the difference.
program eoln_example;
uses crt;
var thefile:text;
filechar:char;
begin;
clrscr;
assign(thefile,'file.txt');
reset(thefile);
while eoln(thefile) = false do
begin;
read(thefile,filechar);
write(filechar);
end;
close(thefile);
readln;
end.
This line will be read. This line, on the other hand, will not.
In a program, there are several lines of code you might want to use more than once. Simply copying and pasting that code will bloat your source code, making it harder to develop the program. Code that needs to be reused may be written in subprograms (which are functions and procedures). Calling the required function/procedure will execute the necessary instructions, and save copying and pasting lots of lines of code.
If all you want is to execute multiple instructions in a given order, the
subprogram you need to use is the procedure. A procedure may have its own
variables and constants, and consists of a
begin..end
block, which
is where you put the instructions you want. Note that any variables or constants
belonging to the subprogram are not available in the program's main
begin..end
block - variables
(and constants) are restricted to a particular scope (the
particular subprogram, loop or main program). Any subprogram must be defined
at the beginning of a program before they can be used, and this is how it is
done (in general):
procedure Proc_Name
const
{constants go here}
var
{variables go here}
begin;
{do something}
end;
The definition goes before the main
begin..end
block. Proc_Name
can be any name you choose for the procedure. Later, in the main program, you
use the procedure by writing this name, followed by a semicolon, as shown below.
Note that a procedure definition can consist of just the
begin..end
block, as the sections
for constants and variables may be omitted if empty.
Proc_Name;
Variables from the main program flow (which are otherwise not accessible by the subprogram because they are outside its scope) may be used in a subprogram by passing them as parameters. This is done by listing the variables used within brackets after the name of the subprogram. Multiple variables of the same type are separated by commas, and different variable types are separated by semicolons. The following is a sample program using a procedure and a couple of parameters.
program dlg;
uses crt;
procedure talk(speaker:integer; dialogue:string);
begin;
textcolor(speaker);
if (speaker = 1) then
write('Shopkeeper')
else if (speaker = 2) then
write('Joe Black ');
write(': ');
textcolor(7);
writeln(dialogue);
writeln;
end;
begin;
clrscr;
writeln('Fictional dialogue.');
writeln;
writeln('-----');
writeln;
talk(1,'Good morning, sir. May I help you?');
talk(2,'I''m looking for a girl.');
talk(1,'Aren''t we all?');
talk(2,'No, I mean she works here. Do you know Sandra?');
talk(1,'Of course. Let me call her.');
readln;
end.
Functions are like procedures, but they return a value that can be used within the main program. This means that they are not just used to carry out instructions, as procedures are, but they are used to manipulate data and return it for use within the main program flow. Like procedures, they can take constants, variables and parameters. The only difference in definition is that the function name (or the parameters, if any) must be followed by the type of data that the function will return.
The following is an example of the classic function to multiply two numbers.
Of course, here the use of the function could easily be replaced by just using
a*b
within the writeln
, but this is just to show you
how it works.
program multiply_test;
var a,b:integer;
function multiply(x,y:integer):integer;
begin;
multiply := x*y;
end;
begin;
writeln('Enter the numbers you want to multiply.');
readln(a,b);
writeln(a,' x ',b,' = ',multiply(a,b));
readln;
end.
As you can see, the function is assigned a value which is later used in
the main program. Since the function consists of a value, calling it on its
own like a procedure would do nothing (even though code can be added
to the function to write the value, or whatever is needed). Instead, this value
is used with another function (in this case writeln
) to show the
data it represents.
Following are a few things you should know if you want to use subprograms properly in your programs.
Let's say you have two procedures, defined one after another. If you wanted both procedures to call each other, this would not be possible. The first procedure cannot call the second procedure, which is defined later. The solution is to declare your subprograms before they are defined, like this:
procedure First_Procedure; forward;
procedure Second_Procedure; forward;
As you can see, declaration involves writing the first line of your definition
(including the procedure
keyword, subprogram
name and any parameters; followed by the forward
keyword. This is called forward referencing.
Note that apart from calling other subprograms, subprograms may also call themselves. However, if this is done, it should be restricted by the condition of a loop. Otherwise, the subprogram would call itself infinitely, causing an infinite loop.
There is a set of reserved words in Pascal that cannot be used as identifiers, i.e. you cannot name variables, constants, functions, etc. with the name of these reserved words. You will notice if a word is reserved because it will display in white rather than the normal yellow.
Reserved Word | Use |
---|---|
and | Boolean Operator |
asm | |
array | |
begin | Beginning of a code block |
case | Conditional Statement |
const | Declaration of Constants |
constructor | |
destructor | |
div | Arithmetic Operator |
do | Performs the commands in Loops |
downto | Counts down in for..do loops |
else | "Otherwise" command in Conditional Statements |
end | End of a code block |
exports | |
file | |
for | Loop |
function | |
goto | |
if | Conditional Statement |
implementation | |
in | |
inherited | |
inline | |
interface | |
label | |
library | |
mod | Arithmetic Operator |
nil | |
not | Boolean Operator |
object | |
of | Specifies each case of a case statement |
or | Boolean Operator |
packed | |
procedure | |
program | Specifies the program's name (see Basic Input and Output) |
record | |
repeat | Loop |
set | |
shl | |
shr | |
string | Data type (see Variables) |
then | Performs the commands in If statements |
to | Counts up in for..do loops |
type | |
unit | |
until | Specifies the break condition of the repeat..until loop |
uses | Includes units (see The CRT Unit) |
var | Declaration of Variables |
while | Loop |
with | |
xor | Boolean Operator |
There are 16 CRT colours in Pascal. They are the same 8 colours, but 0-7 are low-intensity and 8-15 are high-intensity. If you exceed 15, the colours start over (e.g. 0, 16 and 32 are all black).
Number | Sample | Name |
---|---|---|
0 | black | |
1 | blue | |
2 | green | |
3 | cyan | |
4 | red | |
5 | magenta | |
6 | brown | |
7 | lightgray | |
8 | darkgray | |
9 | lightblue | |
10 | lightgreen | |
11 | lightcyan | |
12 | lightred | |
13 | lightmagenta | |
14 | yellow | |
15 | white |
You can use the following program to list the colours in Pascal.
program crt_colour_list;
uses crt;
var counter:integer;
begin;
clrscr;
for counter:=0 to 15 do
begin;
textcolor(counter);
writeln(counter);
end;
readln;
end.
Acronym | Full Term | Description |
---|---|---|
ALGOL | Algorithmic Language | High-level language developed in the late 1950s by a committee led by Peter Naur (see Introduction to Pascal) |
ASCII | American Standard for Character Information Interchange | The ASCII Codes are numbers corresponding to a character. See Table of ASCII Codes for a list or Ordinal Functions for the chr() function which displays ASCII codes |
BGI | Borland Graphical Interface | The Pascal graphics library provided by Borland. |
CRT | Cathode Ray Tube | The Cathode Ray Tube is what makes a monitor or screen. Look it up in a Physics book to see how it works. |
Learn Pascal - good as a Pascal reference, though not detailed enough to serve as a tutorial | |
Roby's Programming Tutorial - goes into the complex parts of Pascal, not for beginners | |
Pascal Programming - tutorial by a Maltese student, not very user-friendly but can be a good tutorial for beginners | |
Blaise Pascal - about the mathematician after who the language is named |
Please refer to www.asciitable.com/ until I put up this section.