Comparison of Pascal and Delphi: Difference between revisions

Content deleted Content added
No edit summary
Removed PROD: Delphi and Pascal are strongly related languages, so it is no WP:INDISCRIMINATE comparison. As for WP:OR issues: WP:TNT is not policy, while WP:ATD is.
 
(28 intermediate revisions by 20 users not shown)
Line 1:
{{Short description|Programming language}}
 
{{ProgLangCompare}}
{{use mdy dates|date=April 2023}}
This article is a '''comparison between Pascal and Delphi''' because Borland, the original manufacturer of [[CodeGear Delphi|Delphi]], once used ''Delphi'' for their version of the [[Pascal programming language|Pascal]]-based [[programming language]] used in the product, which is otherwise known as [[Object Pascal]]. Compatibles still maintain the Object Pascal designation because Delphi is a registered trademark.
Devised by [[Niklaus Wirth]] in the late 1960s and early 1970s, [[Pascal (programming language)|Pascal]] is a [[programming language]]. Originally produced by [[Borland]] Software Corporation, [[Embarcadero Delphi]] is composed of an IDE, set of standard libraries, and a Pascal-based language commonly called either [[Object Pascal]], Delphi Pascal, or simply 'Delphi' (Embarcadero's current documentation refers to it as 'the Delphi language (Object Pascal)'<ref>{{Cite web|url=http://docwiki.embarcadero.com/RADStudio/XE2/en/Delphi_Reference|title = Delphi Reference - RAD Studio XE2}}</ref>). Since first released, it has become the most popular commercial Pascal implementation.
 
[[PascalWhile (programmingdeveloping language)|Pascal]] is a [[programming language]], developed in 1970 by [[Niklaus Wirth]]. The original Pascal employed a [[Bootstrapping_Bootstrapping (compilers)|bootstrapping]] constructprocedure wherein which each successivelynewer moreversion complicatedof the Pascal compiler was written and compiled thewith nextits iterationpredecessor. Thus, the compiler 'P2' compiler was written in the dialect compilable by 'P1', and could'P3' in turn compilewas P3,written in 'P2' and suchso on, all the way till 'P5'. whichThe implemented'P5' "full"compiler implemented Pascal in its final state as defined by Wirth, and wassubsequently morebecame optimizedstandardised as 'ISO 7185 Pascal'.
 
[[The Borland]] Corporationdialect, like the wildly popular [[UCSD Pascal]] before themit, took the 'P4' subsetversion compilerof the language as theirits basis for their Turbo Pascal range, probablyrather becausethan theyWirth's consideredfinal it more efficient on the [[microcomputer|micros]] they were targetingrevision. OverAfter timemuch theirevolution dialectindependent implementedof manyStandard UCSD extensions toPascal, the originalBorland Pascalvariant likebecame the "string"basis type. Their language went through several versions and names, such as [[Turbo Pascal]], Borland Pascal, and finally [[Borlandfor Delphi|Delphi]] Pascal. This page briefly goes over the differences between thoseDelphi dialectsand ofStandard the languagePascal. It does '''not''' go into theDelphi-specific extensions providedto bythe Delphilanguage, thatwhich isare thenumerous subject of a Delphi page. Rather it just covers the differences that exist between theand basestill implementationsincreasing.
 
== Exclusive features ==
==Terminology==
Following features are mutually exclusive.
It is correct to refer to "Pascal" in general as Niklaus Wirth's original language (and derivatives) and Borland's dialect as "Delphi". When referring to Borland's previous dialects, the terms "Turbo Pascal", and "Borland Pascal" apply.
The Standard Pascal implementation is not accepted by Delphi and vice versa, the Delphi code is not acceptable in Standard Pascal.
 
=== Modulo with negative dividend ===
The term "ISO 7185" or "ISO 7185 Pascal" is used here as synonymous with Niklaus Wirth's programming language Pascal. The ISO 7185 standard is the standardized version of [[Niklaus Wirth]]'s language, as he has stated several times.
Standard Pascal has a Euclidean-like definition of the [[Modulo#In programming languages|<syntaxhighlight lang="pascal" inline>mod</syntaxhighlight> operator]] whereas Delphi uses a truncated definition.
 
=== Nested comments ===
== Differences between the languages ==
Standard Pascal requires that the comment delimiters <syntaxhighlight lang="pascal" inline>{</syntaxhighlight> and the bigramm <syntaxhighlight lang="pascal" inline>(*</syntaxhighlight>, as well as <syntaxhighlight lang="pascal" inline>}</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>*)</syntaxhighlight> are synonymous to each other.<ref name="gpc_comments"/>
In Delphi, however, a block comment started by <syntaxhighlight lang="pascal" inline>{</syntaxhighlight> ''must'' be closed with a <syntaxhighlight lang="pascal" inline>}</syntaxhighlight>.<ref name="moore"/>
The bigramm <syntaxhighlight lang="pascal" inline>*)</syntaxhighlight> will only close any comment that started with <syntaxhighlight lang="pascal" inline>(*</syntaxhighlight>.<ref name="moore"/>
This scheme allows for ''nested'' comments at the expense of compiler complexity.
 
=== Procedural data types ===
Because Borland Delphi is a widely used version of Pascal, it is useful to compare the two languages. Note that here are presented only the differences between Borland Delphi and the basic ISO 7185 standard. Undiscussed are any extensions provided by Borland Delphi. In other words, this section answers the question "why doesn't my standard Pascal program run under Borland Delphi?", and perhaps "what can I write in Borland Delphi that will also be compatible with the ISO 7185 standard?".
The way procedures and functions can be passed as parameters differs:
Delphi requires explicit procedural types to be declared where Standard Pascal does not.<ref name="reagan"/>
{| class="wikitable" style="margin: auto;"
|+ comparison procedural data types
|-
! Standard Pascal !! Delphi
|- style="vertical-align:bottom"
| <syntaxhighlight lang="pascal" highlight="3,4" style="font-size:85%">
program proceduralDataType(output);
 
{ `printYIntersect` has one procedural parameter: }
1. Procedures and functions may not appear as parameters (it is true that it can be done, but a non-standard syntax must be used).
procedure printYIntersect(function f(x: real): real);
begin
writeLn(f(0.0));
end;
{ Standard Pascal does not have procedural “pointers.” }
function f(x: real): real;
begin
f := cos(x);
end;
{ ─── MAIN ───────────────────────────────────────────── }
begin
printYIntersect(f);
end.
</syntaxhighlight>
| <syntaxhighlight lang="delphi" highlight="1-4" style="font-size:85%">
type
TFunc = function(x: real): real;
 
procedure printYIntersect(f: TFunc);
2. [[Goto]] statements cannot reference targets outside procedure/function bodies (so called "intraprocedural gotos").
begin
writeLn(f(0.0));
end;
 
function f(x: real): real;
3. No file buffer variable handling. Standard Pascal has file "buffer variables", and "get" and "put" procedures to operate on them. This functionality is not present in Borland Delphi.
begin
f := cos(x);
end;
// ─── MAIN ─────────────────────────────────────────────
begin
printYIntersect(@f);
end.
</syntaxhighlight>
|}
 
=== Conversion of newline characters ===
4. No "sized" dynamic variable allocation. Given a variant record, the size of a particular variant cannot be specified as per the standard. I.e., the following statement is invalid:
Various computer systems show a wide variety how to indicate a [[newline]].
This affects the internal representation of <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> files which are composed of a series of “lines”.
In order to relieve the programmer from any associated headaches, Standard Pascal mandates that ''reading'' an “end-of-line character” returns a single space character.
To distinguish such an “end-of-line” space character from a space character that is actually genuine payload of the line, <syntaxhighlight lang="pascal" inline>EOLn</syntaxhighlight> becomes <syntaxhighlight lang="pascal" inline>true</syntaxhighlight>.
 
Delphi does not show this behavior.
<source lang="pascal">
Reading a newline will return whatever character sequence represents a newline on the current host system, for example two <syntaxhighlight lang="delphi" inline>char</syntaxhighlight> values <syntaxhighlight lang="delphi" inline>chr(13)</syntaxhighlight> (carriage return) plus <syntaxhighlight lang="delphi" inline>chr(10)</syntaxhighlight> (line feed).<ref name="moore"/>
new(p, t)
</source>
 
== Additional or missing features ==
Where t is a variant record tag type.
Following features are present or missing in either language.
 
=== Global <syntaxhighlight lang="pascal" inline>goto</syntaxhighlight> ===
5. The functions "pack" and "unpack" are not implemented.
Standard Pascal permits a <syntaxhighlight lang="pascal" inline>goto</syntaxhighlight> to any <syntaxhighlight lang="pascal" inline>label</syntaxhighlight> defined in scope.
In Delphi a <syntaxhighlight lang="delphi" inline>goto</syntaxhighlight> must be within the current routine, i. e. may not leave the <syntaxhighlight lang="delphi" inline>begin … end</syntaxhighlight>-frame.<ref name="moore"/>
<syntaxhighlight lang="pascal" highlight="6,7">program jumpAround;
label
999;
procedure foo;
begin
{ This is not permitted in Delphi: }
goto 999;
end;
begin
foo;
999: ;
end.</syntaxhighlight>
 
=== Buffer variables ===
6. { and (*, } and *) are not synonyms of each other as required by the standard. I.e.:
Delphi does not support buffer variables and associated standard routines <syntaxhighlight lang="pascal" inline>get</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>put</syntaxhighlight>.<ref name="moore"/>
<syntaxhighlight lang="pascal" highlight="5,6,15,19">program copy(input, output);
begin
while not EOF(input) do
begin
{ Copy file buffers. Not supported by Delphi }
output↑ := input↑;
{ Input↑ contains a space if a new-line occurred. }
if EOLn(input) then
begin
writeLn(output);
end
else
begin
put(output);
end;
{ Advance reading cursor. }
get(input);
end;
end.</syntaxhighlight>
 
=== Discriminated variant record allocation ===
<source lang="pascal">
In Standard Pascal allocating memory for a variant <syntaxhighlight lang="pascal" inline>record</syntaxhighlight> may indicate a specific variant.
{ comment *)
This allows implementations to allocate the least amount of ''really'' necessary memory.
</source>
Delphi does not support this.<ref name="moore"/>
<syntaxhighlight lang="pascal" highlight="13,14">program variantRecord;
type
sex = (female, male);
clothingMeasures = record
girth: real;
case gender: sex of
female: (underbust: real);
male: ( );
end;
var
size: clothingMeasures;
begin
{ NB: No space allocated for `underbust`. }
new(size, male);
end.</syntaxhighlight>
 
=== Temporary files ===
is not valid in Borland Delphi (Delphi uses the scheme of allowing the different comment types to indicate nested comments). It is valid in Turbo Pascal though.
In Delphi any file must be backed by a file in the file system.
That means any <syntaxhighlight lang="delphi" inline>file</syntaxhighlight> needs to be associated with a file name with Delphi's <syntaxhighlight lang="delphi" inline>assign</syntaxhighlight> procedure.
In contrast, Standard Pascal is usable without file names.
The following will produce a run-time error with Delphi.<ref name="moore"/>
<syntaxhighlight lang="pascal">program temporaryFile(output);
var
FD: text;
begin
rewrite(FD);
writeLn(FD, 'Hello world!');
end.</syntaxhighlight>
 
=== Packing ===
7. Does not replace eoln with space as the standard requires. When reading through the end of a line, the eoln character is supposed to be replaced with a space in ISO 7185. Instead, reading through eoln in Borland Delphi gives the character code for carriage return (13), followed by line feed (10).
Delphi does not implement the standard procedures <syntaxhighlight lang="pascal" inline>pack</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>unpack</syntaxhighlight>.<ref name="moore"/>
Regardless, transferring data between packed and unpacked data types is an easy feat, although the implementation might not be as efficient as a compiler vendor supplied implementation would be.
 
=== Missing default <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> width ===
8. Numbers and booleans are not printed out in their "default" field widths, but are printed in the minimum amount of space. For example:
Delphi does not associate the data type <syntaxhighlight lang="pascal" inline>Boolean</syntaxhighlight> with a default width if specified as <syntaxhighlight lang="pascal" inline>write</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> parameters.<ref name="moore"/>
Delphi demonstrates the behavior as usual for character-strings.
 
=== Overloading ===
<source lang="pascal">
Delphi permits overloading routines.
write(5);
In Standard Pascal identifiers must be unique in every block.
write(55);
<syntaxhighlight lang="delphi">function f(x: integer): real;
</source>
begin
result := sin(x);
end;
 
function f(x: real): real;
is equivalent to:
begin
result := cos(x);
end;
// ─── MAIN ─────────────────────────────────────────────
begin
// Note the different data types.
writeLn(f(3));
writeLn(f(3.0));
end.</syntaxhighlight>
 
=== Default parameter values ===
<source lang="pascal">
Delphi permits [[default parameter]]s.
write(5:1);
write(55:2);
</source>
 
== Peculiar implementation characteristics ==
in Delphi, but:
=== Standard <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> width ===
In Pascal, if the destination file is a <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> file, the parameters to <syntaxhighlight lang="pascal" inline>write</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> have an implemention-defined default total width.
In Delphi, for <syntaxhighlight lang="delphi" inline>integer</syntaxhighlight> values this is simply <syntaxhighlight lang="delphi" inline>1</syntaxhighlight>.
That means always the least amount of space is occupied.<ref name="moore"/>
Other compilers have shown default widths of, for example, <syntaxhighlight lang="pascal" inline>20</syntaxhighlight> allowing for a fine tabular look at no cost of extra code.
{| class="wikitable" style="margin: auto;"
|+ comparison default <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> width
|-
! colspan="2" | source code fragment
|-
| colspan="2" | <syntaxhighlight lang="pascal">writeLn(1);
writeLn(23456789);</syntaxhighlight>
|-
! colspan="2" | produces in
|-
! Standard Pascal ([[GNU Pascal|GPC]]) !! Delphi
|-
| <syntaxhighlight lang="text"> 1
23456789</syntaxhighlight>
| <syntaxhighlight lang="text">1
23456789</syntaxhighlight>
|}
 
== References ==
<source lang="pascal">
<references>
write(5:TotalWidth);
<ref name="gpc_comments">{{cite web
write(55:TotalWidth);
| title = The GNU Pascal Manual
</source>
| first1 = Jan-Jaap
 
| last1 = van der Heijden
in ISO 7185, where TotalWidth is implementation-defined.
| first2 = Peter
 
| last2 = Gerwinski
For booleans:
| first3 = Frank
 
| last3 = Heckenbach
<source lang="pascal">
| first4 = Berend
write(false);
| last4 = deBoer
write(true);
| first5 = Dominik
</source>
| last5 = Freche
 
| first6 = Eike
is equivalent to:
| last6 = Lange
 
| first7 = Peter
<source lang="pascal">
| last7 = Lewis
write('false':5);
| display-authors = etal
write('true':4);
| at = A QuickStart Guide from Borland Pascal to GNU Pascal
</source>
| url = https://www.GNU-Pascal.de/gpc/Comments.html#Comments
 
| access-date = 2023-04-24
in Delphi, but:
}}</ref>
 
<ref name="reagan">{{cite web
<source lang="pascal">
| title = Pascal Standards FAQ
write('false':TotalWidth);
| at = Comparison of Borland Pascal to the Pascal standards
write('true':TotalWidth);
| first = John
</source>
| last = Reagan
 
| date = 1995-04-03
in ISO 7185, where TotalWidth is implementation-defined.
| url = http://Pascal-Central.com/extpascal.html#anchor-6
 
| archive-url = https://web.archive.org/web/20210928162340/http://pascal-central.com/extpascal.html#anchor-6
9. Temporary files are not supported. Executing reset() or rewrite() results in an error under Delphi. Under standard Pascal it opens a temporary file that exists only for the run of the program.
| archive-date = 2021-09-28
 
}}</ref>
== See also ==
<ref name="moore">{{cite web
 
| title = Standard Pascal FAQ
* [[Delphi programming language]]
| at = Q. What are the differences between Borland Delphi and the ISO 7185 standard?
* [[Comparison of Pascal and C]]
| first = Scott
* [[Pascal (programming language)]]
| last = Moore
| date = 2022-12-02
| url = https://www.MooreCAD.com/standardpascal/pascalfaq.html#Q.%20What%20are%20the%20differences%20between%20Borland%20Del
| access-date = 2023-04-24
}}</ref>
</references>
 
== Further reading ==
* Kathleen Jansen and [[Niklaus Wirth]]: ''PASCAL - User Manual and Report.'' Springer-Verlag, 1974, 1985, 1991, {{ISBN |0-387-97649-3}}, {{ISBN |0-387-90144-2}}, and {{ISBN |3-540-90144-2}} [https://web.archive.org/web/20050314152247/http://www.cs.inf.ethz.ch/~wirth/books/Pascal/]
* Niklaus Wirth: ''The Programming Language Pascal.'' Acta Informatica, 1, (Jun 1971) 35-6335–63
* ISO/IEC 7185: ''Programming Languages - PASCAL.'' [http://www.moorecad.com/standardpascal/]
* Doug Cooper: ''Standard Pascal: User Reference Manual.'' W. W. Norton & Company, 1983, {{ISBN 0393301214|0-393-30121-4}}, {{ISBN 9780393301212|978-0-393-30121-2}}
* Pascal standards documents [http://www.standardpascal.org/standards.html]
 
== External links ==
* [https://www.moorecad.com/standardpascal/ The ISO 7185 Pascal web site]
* [https://www.StandardPascal.org/standards.html Pascal standards documents]
 
{{Pascal programming language family}}
* The standard, ISO 7185 Pascal web site [http://www.moorecad.com/standardpascal/]
 
{{DEFAULTSORT:Pascal and Delphi comparison}}
[[Category:Pascal programming language family]]
[[Category:Borland]]
[[Category:ProgrammingComparison languageof comparisonsindividual programming languages]]
 
 
{{Pascal programming language family}}