Subsections
Free Pascal was designed to resemble Turbo Pascal as closely as possible. There
are, of course, restrictions. Some of these are due to the fact that Free Pascal is
a 32-bit compiler. Other restrictions result from the fact that Free Pascal works
on more than one operating system.
In general we can say that if you keep your program code close to ANSI
Pascal, you will have no problems porting from Turbo Pascal, or even Delphi, to
Free Pascal. To a large extent, the constructs defined by Turbo Pascal are
supported. This is even more so if you use the -So or -S2
switches.
In the following sections we will list the Turbo Pascal constructs which are
not supported in Free Pascal, and we will list in what ways Free Pascal extends the Turbo
Pascal language.
Here we give a list of things which are defined/allowed in Turbo Pascal, but
which are not supported by Free Pascal. Where possible, we indicate the reason.
- Duplicate case labels are not allowed. This is a bug in Turbo Pascal
and will not be changed.
- Parameter lists of previously defined functions and procedures must
match exactly. The reason for this is the function overloading mechanism of
Free Pascal. (however, the See So option solves this.)
- (* ... *) as comment delimiters are not allowed in versions
older than 0.9.1. This can easily be remedied with a grown-up editor.
- The MEM, MEMW, MEML and PORT variables for memory and port
access are not available in the system unit. This is due to the operating system. Under
DOS, the extender unit (GO32.PPU) implements the mem constuct.
under LINUX, the ports unit implements such a construct.
- PROTECTED, PUBLIC, PUBLISHED, TRY, FINALLY, EXCEPT, RAISE
are reserved words. This means you cannot create procedures or variables
with the same name. While they are not reserved words in Turbo Pascal,
they are in Delphi. Using the -So switch will solve this problem if
you want to compile Turbo Pascal code that uses these words.
- The reserved words FAR, NEAR are ignored. This is
because Free Pascal is a 32 bit compiler, so they're obsolete.
- INTERRUPT will work only on the DOS target.
- Boolean expressions are only evaluated until their result is completely
determined. The rest of the expression will be ignored.
- By default the compiler uses AT&T assembler syntax.
This is mainly because Free Pascal uses GNU as. However, other assembler
forms are available. For more information, see Programmers' guide.
- Turbo Vision is not completely available. There is FreeVision, but the
degree of compatibility with Turbo Vision is unclear at this
time7.1.
- The 'overlay' unit is not available. It also isn't necessary, since
Free Pascal is a 32 bit compiler, so program size shouldn't be a point.
- There are more reserved words. (see appendix for a
list of all reserved words.)
- The command-line parameters of the compiler are different.
- Compiler switches and directives are mostly the same, but some extra
exist.
- Units are not binary compatible.
- Sets are always 4 bytes in Free Pascal; this means that some typecasts
which were possible in Turbo Pascal are no longer possible in Free Pascal.
- A file is opened for output only (using fmOutput) when it is
opened with Rewrite. In order to be able to read from it, it should
be reset with Reset.
- Destructors cannot have parameters. This restriction can be solved by
using the -So switch.
- There can be only one destructor. This restriction can also be
solved by using the -So switch.
- The order in which expressions are evaluated is not necessarily the
same. In the following expression:
a := g(2) + f(3);
it is not guaranteed that g(2) will be evaluated before f(3).
Here we give a list of things which are possible in Free Pascal, but which
didn't exist in Turbo Pascal or Delphi.
- There are more reserved words. (see appendix for a
list of all reserved words.)
- Functions can also return complex types, such as records and arrays.
- You can handle function results in the function itself, as a variable.
Example
function a : longint;
begin
a:=12;
while a>4 do
begin
{...}
end;
end;
The example above would work with TP, but the compiler would assume
that the a>4 is a recursive call. To do a recursive call in
this you must append () behind the function name:
function a : longint;
begin
a:=12;
{ this is the recursive call }
if a()>4 then
begin
{...}
end;
end;
- There is partial support of Delphi constructs. (see the Programmers' guide for
more information on this).
- The exit call accepts a return value for functions.
function a : longint;
begin
a:=12;
if a>4 then
begin
exit(a*67); {function result upon exit is a*67 }
end;
end;
- Free Pascal supports function overloading. That is, you can define many
functions with the same name, but with different arguments. For example:
procedure DoSomething (a : longint);
begin
{...}
end;
procedure DoSomething (a : real);
begin
{...}
end;
You can then call procedure DoSomething with an argument of type
Longint or Real.
This feature has the consequence that a previously declared function must
always be defined with the header completely the same:
procedure x (v : longint); forward;
{...}
procedure x;{ This will overload the previously declared x}
begin
{...}
end;
This construction will generate a compiler error, because the compiler
didn't find a definition of procedure x (v : longint);. Instead you
should define your procedure x as:
procedure x (v : longint);
{ This correctly defines the previously declared x}
begin
{...}
end;
(The See So switch disables overloading. When you use it, the above will
compile, as in Turbo Pascal.
- Operator overloading. Free Pascal allows to overload operators, i.e. you can
define e.g. the '+' operator for matrices.
- On FAT16 and FAT32 systems, long file names are supported.
When you compile a program with the -So switch, the compiler will
attempt to mimic the Turbo Pascal compiler in the following ways:
- Assigning a procedural variable doesn't require a @ operator. One of
the differences between Turbo Pascal and Free Pascal is that the latter requires
you to specify an address operator when assigning a value to a procedural
variable. In Turbo Pascal compatibility mode, this is not required.
- Procedure overloading is disabled. If procedure overloading is
disabled, the function header doesn't need to repeat the function header.
- Forward defined procedures don't need the full parameter list when
they are defined. Due to the procedure overloading feature of Free Pascal, you must
always specify the parameter list of a function when you define it, even
when it was declared earlier with Forward. In Turbo Pascal
compatibility mode, there is no function overloading, hence you can omit the
parameter list:
Procedure a (L : Longint); Forward;
...
Procedure a ; { No need to repeat the (L : Longint) }
begin
...
end;
- recursive function calls are handled differently. Consider the
following example :
Function expr : Longint;
begin
...
Expr:=L:
Writeln (Expr);
...
end;
In Turbo Pascal compatibility mode, the function will be called recursively
when the writeln statement is processed. In Free Pascal, the function result
will be printed. In order to call the function recusively under Free Pascal, you
need to implement it as follows :
Function expr : Longint;
begin
...
Expr:=L:
Writeln (Expr());
...
end;
- Any text after the final End. statement is ignored. Normally,
this text is processed too.
- You cannot assign procedural variables to untyped pointers; so the
following is invalid:
a: Procedure;
b: Pointer;
begin
b := a; // Error will be generated.
- The @ operator is typed when applied on procedures.
- You cannot nest comments.
Under WINDOWS 95 and higher, long filenames are supported. Compiling
for the win32 target ensures that long filenames are supported in all
functions that do fie or disk access in any way.
Moreover, Free Pascal supports the use of long filenames in the system unit and
the dos unit also for go32v2 executables. The system unit contains the
boolean variable LFNsupport. If it is set to True then all
system unit functions and DOS unit functions will use long file names
if they are available. This should be so on all versions of Windows,
with the possible exception of WINDOWS 2000. The system unit will check
this by calling DOS function 71A0h and checking whether long
filenames are supported on the C: drive.
It is possible to disable the long filename support by setting the
LFNSupport variable to False.
root
2000-12-20