Language Overview
Free Pascal is a high level strongly typed compiled language.
Its immediate ancestors are Turbo Pascal for DOS and Delphi for Windows.
Free Pascal combines the high productivity of rapid application development (RAD) languages and the power of C++.
This overview page describes the essential features of Free Pascal, while other sections
delve into the rules of the language in more detail. The intent here is to
provide the reader with a brief introduction to Free Pascal that will guide
them when first starting to program with Free Pascal.
Your First Program
The canonical 'Hello World' program can be written in Free Pascal as follows:
program Hello; begin WriteLn('Hello World!'); end.
The source code for a Free Pascal program is typically stored in one or more text files with a file extension of .pas, as in Hello.pas. Using the Free Pascal compiler such a program can be compiled with a command line using:
pascaluser:~/examples$ fpc Hello.pas
Which produces a program named Hello (Hello.exe on Windows). The output produced by this program when run is:
pascaluser:~/examples$ ./Hello Hello World!
Upon examination of the program source a few items are worth noting. The source code begins with the declaration
program
followed by an identifier. This signifies to the compiler that the file is a program and the output should be a single executable named by the identifier. In our example the identifier is named Hello
resulting in a executable file of the same name.The identifiers
program
, begin
, and end
are highlighted in bold color denoting their status as keywords. Keywords have special meaning to the compiler and must appear in a certain order. Any program created with Free Pascal must start with the keyword program
and must close with end.
terminated by a period.You may also have noticed the use of the procedure
WriteLn
and be asking from where does it come. Every Free Pascal program, unit, library, and package implicitly uses the System unit. The System unit provides the definition and implementation of WriteLn
to our Hello program. Anytime while reading this guide you may refer to our documentation portal to read more about a specific, unit, function or procedure, constant, or type. For example here is the documentation entry for WriteLn procedure.Identifiers, Symbols, and Digits
Every character or group of characters, also known as a token, in Free Pascal falls into one of three categories: identifiers, symbols, or numbers. All tokens are separated by whitespace characters or symbols.
Whitespace
Whitespace is the collective name given to spaces, tabs, newline characters, and comments. Whitespace can serve to indicate where tokens start and end, but beyond this purpose, any surplus whitespace is discarded.
Identifiers
Identifiers are used to denote names for variables, types, or functions among other things. Identifiers names are case insensitive and can be of any length, but they must begin with an alphabetic character or
_
an underscore and cannot contain spaces or symbols. Digits are permitted after the first character.Keywords
Certain identifiers are reserved by to have special meanings. These reserved identifiers are known as keywords. Keywords cannot be repurposed to change their meaning. The following is a list of reserved keywords:and
array
as
asm
begin
case
class
const
constructor
destructor
dispinterface
div
do
downto
else
end
except
exports
file
finalization
finally
for
function
goto
if
implementation
in
inherited
initialization
inline
interface
is
label
library
mod
nil
not
object
of
or
packed
procedure
program
property
raise
record
repeat
resourcestring
set
shl
shr
string
then
threadvar
to
try
type
unit
until
uses
var
while
with
xor
Directives
Some identifiers have a specific meaning sensitive to their context within source code. These kinds of identifiers are known as directives. When these identifiers are used in their specific contexts they are treated like keywords. The following is a list of directives Free Pascal uses:absolute
abstract
assembler
cdecl
contains
default
delayed
deprecated
dispid
dynamic
export
external
forward
helper
implements
index
inline
library
local
message
name
nodefault
operator
out
overload
override
package
pascal
platform
private
protected
public
published
read
readonly
reference
register
reintroduce
requires
resident
safecall
sealed
static
stdcall
stored
strict
varargs
virtual
write
writeonly
In our Hello example program
program
, begin
, and end
are keywords, while Hello
and WriteLn
are idenfifiers.Symbols
Symbols are single or paired non-alphanumeric characters that have special meanings. The following single characters are symbols in Free Pascal:
# $ & ' ( ) * + , - . / : ; < = > @ [ ] ^ { }
The following character pairs are also symbols:
(* (. *) .) .. // := <= >= <>
Like whitespace, symbols indicate where tokens start and end. Below is a brief overview of some of ways symbols are used:
Semicolons
In our Hello example several lines end with;
a semicolon symbol. Semicolons are used throughout Free Pascal to terminate statements. A statement is either a block of code or an expression. Later in this guide we'll delve into the difference between blocks and expressions and where to place semicolons. For now it can be explained that all statements must be terminated with a semicolon other than the required end.
with a period that must be placed at the end of every Free Pascal program, unit, or package file.Strings
TheWriteLn
procedure in our Hello example contains a sequence of characters delimited by '
a single quote symbol. This sequence is what is called a string. Strings are ubiquitous throughout Free Pascal and are so important that the identifier string
is a keyword which is reserved and cannot be redefined. Strings are typically used to hold text and can be concatenated together using the +
symbol.Comments
Comments are blocks of characters which the compiler discards treating them as whitespace. They can be used to leave descriptive notes in source code reminding (in DOS batch files comments begin with REM) yourself or other programmers of information which may be of use later. Comments can also be used to remove pieces of code, usually on a temporary basis while testing is being done. When using a syntax highlighting editor, such as Lazarus, comments typically appear as a sequence of green italic though they can usually be configured to appear in whatever style you prefer.Three styles of comments can be used in Free Pascal. A single line comment appears a pair of
//
forward slash symbols and is closed by a newline. Block comment can span across multiple lines of code and are delimited by these { }
, (* *)
symbols. Comments may be nested, but when they are the rules for the first comment style to appear are used when closing the comment.(* Hello.pas Hello is our first program Last modified in January 2016 *) program Hello; begin { Write the string 'Hello World!' to the terminal } WriteLn('Hello World!'); // ReadLn; // uncomment this line to wait for the enter key end.
Digits
Digits are the numeric characters
0 1 2 3 4 5 6 7 8 9
which can be be placed in source code. They can be placed alone or in sequence other to form literal number or text character values. The characters A B C D E F
can also be considered as digits when a digit sequence is prefixes with the $
symbol. Number sequences can include a .
symbol which converts a whole number sequence into a what is known as floating point sequence. When a digit sequence is prefixed with a `#` hash symbol it is interpreted as a text characters or a string literal.Here are some examples of different digit sequences as they can appear in source code:
(* Digits.pas Digits prints out digit sequences Last modified in January 2016 *) program Digits; begin { Write out some strings and digit sequences } WriteLn('A whole number: ', 42); WriteLn('A hexidecimal whole number: ', $1EE7); WriteLn('A floating point number: ', 3.1415); WriteLn('A floating point number with a mantissa: ', 6.0221e+23); WriteLn('A string literal: ', #72#69#76#76#79); end.
Variables and Types
Functions and Procedures
Program Organization
Free Pascal programs are usually divided into source code modules called units. After the program declaration developers have the option to specify a uses clause, which consists of list of one or more unit names separated by commas. The unit names listed are linked into the program and can often be shared with different programs. The uses clause provides the compiler with information about the dependencies between unit modules, which allows developers to build applications without the need for makefiles, header files, or preprocessor include directives.
Here is a program containing a uses clause and several statements:
program Birthday; uses SysUtils; var S: string; D: TDateTime; begin WriteLn('Enter your date of birth:'); ReadLn(S); D := StrToDateDef(S, 0); if D = 0 then S := 'You did not type a valid date' else S := 'You were born on a ' + LongDayNames[DayOfWeek(D)]; WriteLn(S); end.
Having saved the above program to our examples folder in a filed named Birthday.pas, again we can compile our program with a command line using:
pascaluser:~/examples$ fpc Birthday.pas
This time a program named Birthday (Birthday.exe on Windows) is produced. The output of this program when run is:
pascaluser:~/examples$ ./Birthday Enter your date of birth: 31-10-1992 You were born on a Saturday