Pascal (language)

Pascal is a programming language imperative that, designed for teaching, is characterized by a clear syntax, rigorous and facilitating the structuring of programs 1 .

Apart from the syntax and its rigor, the Pascal language has points in common with the C (see the pointers ). The basic Pascal language was designed for purely educational use and was rather limited. For example, strings , missing the original language, were quickly integrated 2 , 3 .

Its power has been established, and its diffusion quickly facilitated by the writing of Pascal compilers Pascal written.

The developments which he subsequently experienced made of it a complete and effective language. More recently, genericity has been added in Delphi 2009 4 and in Free Pascal since version 2.2 5 , 6 .

Pascal’s current implementations, used outside the educational world, are extensions such as Turbo Pascal (text mode), Pascal Objet (object programming), and Delphi (windowed). There are free versions like Free Pascal and Lazarus (windowed). You can program in Pascal under DOS , Windows , Mac OS or under Linux / Unix or Palm OS .

The operating system of the Apollo 7 computers , as well as part of the system of the Macintosh were written in Pascal. The first version of Adobe Photoshop also 8 . The GCC compiler was developed by Richard Stallman from an LLNL compiler , which was written in Pastel language, an extension of the Pascal 9 language .

The language syntax has been adapted to other languages ​​such as Ada , Modula-2 (then Modula-3 ) or Oberon .

Presentation and history

The programming language Pascal (whose name comes from the French mathematician Blaise Pascal ) was invented by Niklaus Wirth in the 1970s with the help of one of his students, Urs Amman. It was designed to be used in the teaching of programming in a rigorous but simple way, in reaction to the complexity of Algol 68 . The first compiler was designed on a CDC 6400 1 , 10 .

This language is one of those used to teach structured programming . The goto or jump anywhere in the program (the “connection”) is strongly discouraged in this language, the program is a collection of procedures and functions, in which we can use conditional blocks ( yew , box ) and repetitive ( While , for , repeat ) each having an input and an output to facilitate controls, resulting in quick and safe tuning.

The language is moreover strongly and statically typed , that is to say that all the variables must have a type defined at the time of the compilation. On the other hand, its lack of flexibility in managing character-to-character passages is one of its weak points.

He has widely penetrated the world of education and research (universities) and then to a lesser extent that of industry and software publishing.

The P4 compiler has been distributed in source at universities, at a very reduced price. It generated P-Code , a code for a virtual machine. Pascal programs were therefore easily portable on a machine. It was enough to write for her a P-Code interpreter. So there were rapid portages on DEC PDP-11 , first support of C, and on 6502 , 8080 and Z80 , the main microprocessors of the time.

The UCSD Pascal compiler, of the University of California at San Diego , was very successful, especially on machines such as the Apple II that were very diffused 11 .

But the blow of “turbo” will be given by the company Borland , created in 1983 , which marketed the compiler Turbo Pascal for a very small fee ( $ 49 at the time when the Pascal compiler Microsoft was more than $ 500 and did not have No built-in editor or in core compilation ). Microsoft’s Quick Pascal will be marketed far too late to reverse the trend.

Compilers have been produced for various computers including Sun 12 , HP 13 , SGI 14 , CDC 15 , IBM 16 , Unisys 17 , Texas Instruments 18 .

Pascal has been covered by ISO 7185 (1983) 19 and ISO 10206 (1990) 20 standards .

The source files

In general, a program in Pascal is recognized by the extension of the file containing it: .pas , .p or .pp , the latter two being more frequent on UNIX systems . The use of windowed environments (Delphi and Lazarus) led to the appearance of new extensions.

The source code is organized according to different possibilities:

  • A main program begins with the program keyword , optional in some implementations. It is compiled and linked with the units it uses as an executable file . This compilation phase is the most common case in recent compilers. On the contrary, Pascal UCSD, for example, produced bytecode .
  • A dynamic library starts with the library keyword . It is compiled into a file with the extension. Dll on Windows ,. So on UNIX. The dynamic libraries appeared in the Pascal language at the same time as Delphi.
  • A unit file begins with the unit keyword . It is included in programs or libraries, and can be compiled into a standard object code (.o or .obj extension) or in a particular format, such as .dcu for Delphi and .ppu for Lazarus.

Before the appearance of libraries, Turbo Pascal permitted to use Overlay (in) , standard technique under DOS for large programs 21 . These files were separate from the main executable file and could be loaded from time to time.

It is possible to include code in a program other than by writing a unit and simply by doing an include , that is, by instructing the compiler to include the text of a file in a program with The $ I :

Program test ;
Uses Crt , Dos ; {Usual use of units}
{$ I} {direct use of external code}
 {use the features of file}
end .

However, this method is not recommended in view of the disadvantages it presents – especially if an identifier is declared several times in different files – and the lack of control of the compiler on this type of file.

The Turbo Pascal

The Turbo Pascal software was written by Anders Hejlsberg 22 , 23 : it was previously called Compass Pascal, then Poly Pascal. Very compact (12 kilobytes) and very fast because working mainly in RAM , it compiled in one pass and produced x86 machine code under DOS and not bytecode . It was delivered with a complete environment (a text editor and online help, innovation at the time, particularly compact thanks to a substitution system ).

Each new version of Turbo Pascal has brought its share of innovations, partially filling gaps in the original language. Thus, in 1987 version 4 appeared with a true integrated development environment , in 1989 version 5.5 introduced objects 24 , in 1990 version 6 allows the programming of windows in the DOS console, through the Turbo Vision, bringing the premises of event planning . Finally, in 1992 release version 7 for DOS, which will be the last, as well as a version for Windows , quickly supplanted by Delphi .

A sample code: Hello World

Program HelloWorld ( output ) ;
writeln ( 'Hello World' ) ;
Readln ;
End .

The Output parameter that follows the name of the program is now optional (it was only required with the first versions of the Pascal implementations). Nowadays, he is absent most of the time.

Unlike C , Pascal is not case – sensitive , ie reserved words (such as begin ) or identifiers (such as write or variable a ) can be written in either uppercase or lowercase .

Still contrary to the C, statements are made in a clearly separated part of the code. Local declarations are made at the beginning of the procedure or function, the global declarations being made anywhere before the main program. This adds clarity to the language at the cost of a certain lack of flexibility. You can not declare a variable in the middle of a function. Note that in Pascal, declarations must precede any use; In particular, it is prohibited to use a procedure or function that has not yet been declared. You can work around this problem by using the forward keyword .

Finally, the distinction between procedures and functions makes it possible to avoid certain programming errors – by default, because a compilation directive of Delphi allows to activate an “extended” syntax that offers the same laxity as C.

Delphi and windowed programming

In 1995 , to thwart Microsoft and the visual programming of Visual Basic , Borland released Delphi . Unlike VB which produces p-code, Delphi produces faster machine code. Are also emerging the library VCL for interfacing to system libraries (in) for Windows , greatly facilitating the development.

In the early 2000s, Borland produced Kylix , the equivalent of Delphi for the Linux world , which will not be very successful . In the early years of 2010, Embarcadero, which took over Borland’s development tools business, produced Delphi XE, whose recent versions are compatible with Windows , OS X and iOS .

Lazarus , is a slightly different RAD integrated development software allowing to compile on different operating systems such as Windows , GNU / Linux , OS X , Unix , OS / 2 , ReactOS , Haiku and platforms such as x86 , x86- 64 , ARM , SPARC , PowerPC , IA-64 .

Source Sample Lazarus

Here is an example of a Pascal file associated with a window ( Form1: TForm1 ) containing a label (a text). This file is generated automatically and serves as a basic structure for programming. That is, it can be modified, completed, and so on. Small dots are landmarks when editing. They are invisible to execution. The Delphi source and windows are very similar.
Unit Unit1 ;
 {$ Mode objfpc} {$ H +}
 Classes , SysUtils , LResources , Forms , Controls , Graphics , Dialogs , StdCtrls ;
 TForm1 = class ( TForm )
 Label1 : TLabel ; {The label "Hello world!"
 Posed on the window} private
 {private declarations}
 {public declarations}
 end ;
 Form1 : TForm1 ;
 {$ I unit1.lrs}
End .

A small explanation: the {$ I unit1.lrs} directive lets you bind the class TForm1, describing a window, to the resource file unit1.lrs that contains the window design. With Lazarus, the lrs file is an intermediate file created automatically by the compiler from information in the lfm file and compilation directives (including the possibility to choose the widget library ). With Delphi, the equivalent directive would have been {$ R unit1.dfm} and there is no intermediate file. Moreover, it would have been placed in the interface part.


Among the compilers still in use today (2013), we can cite:

  • Free Pascal in particular accompanied by the compiler and the free development environment RAD Lazarus , aims at the best possible compatibility with Delphi. It exists on several platforms, making it easier to carry a program from one environment to another.
  • Delphi , compiler and development environment ” RAD ” commercial. Recent versions of Delphi run on Windows and MacOS X . In the early 2000s, Borland tried a version for Linux , Kylix , which did not have the expected success. Kylix used the CLX , a VCL-like framework, but usable on both operating systems. In Delphi XE2, Embarcadero introduced the same principle of compatibility between systems, with FireMonkey (en)
  • GNU Pascal , whose goal is compatibility with the ISO standard: it completely implements ISO 7185, and in large part ISO 10206. Its development is no longer active since 2006.

Technical aspects


Recursive functions can be written in Pascal. The following function gives an example:

Function factor ( n : integer ) : integer ;
 if n <= 1 Then
 factor : = 1
 factor : = n * factorial ( n - 1 ) ;
End ;

The number of recursive calls is limited by the stack . Some implementations are, however, able to optimize terminal recursion , for example Free Pascal with the -O2 option .


It is possible to define generic types, that is to say they can be used with different possible types without having to rewrite the code. Here is an example in Free Pascal:

Program Pj_gen ;
Uses SysUtils ;
 {An object is defined that adds two elements and multiplies an element by a strictly positive integer}
 generic TAnneauGenerique < T > = class
 function Sum ( a , b : T ) : T ;
 Function Multiple ( n : integer ; a : T ) : T ;
 End ;
 {We create two specialized versions, applying to the real numbers and to the strings}
 TAnneauReel = specialize TAnneauGenerique < Real >;
 TAnneauChaine = specialize TAnneauGenerique < String >;
function TAnneauGenerique . Sum ( a , b : T ) : T ;
 result : = a + b ;
End ;
function TAnneauGenerique . Multiple ( n : integer ; a : T ) : T ;
Var i : integer ;
 {We only handle the case where n> = 1}
 result : = a ;
 For i : = 2 to n do result : = result + a ;
End ;
 r : TAnneauReel ;
 C : TChannel ;
 r : = TAnneauReel . Create ;
 Writeln ( 'With reels:' ) ;
 Writeln ( '4.1 + 5.2 =' , FloatToStr ( r . Sum ( 4.1 , 5.2 ) )) ;
 writeln ( '3 x 5.1 =' , FloatToStr ( r . Multiple ( 3 , 5.1 ) )) ;
 r . Free ;
 Writeln ;
 C : = TChannel . Create ;
 Writeln ( 'With strings:' ) ;
 Writeln ( 'good + day =' , c . Sum ( 'good' , 'day' )) ;
 Writeln ( '10 xa = ' , c . Multiple ( 10 , ' a ' )) ;
 c . Free ;
 Writeln ;
 Writeln ( 'Press <Enter> to quit' ) ;
 Readln ;
End .

Result under FPC2.6.0:

With the reels :
4.1 + 5.2 = 9 , 3
3 x 5.1 = 15 , 3
With the chain :
good + day = hello
10 x a = aaaaaaaaaa
Press < Enter > to quit

Note: in Delphi, do not add the keywords generic and specialize .

Mutual Dependencies of Units

It is possible that the different units of the program can be used mutually, but there is a limitation. Indeed, in Pascal, one can refer to another unit in two possible places, namely in the interface and the implementation. If two units refer to each other in their interface, a circular dependency error is obtained. This problem can be avoided by using a third unit that will serve as the basis for the other two, and / or by using more abstract types at the interfaces. This limitation does not exist in Visual Basic .

Critique of language

In 1981, Brian Kernighan and Phillip J. Plauger (in) publish the book Software Tools in Pascal , reissue of their previous book, Software Tools , published in 1976 and which employed the language Rational Fortran (en) . The aim was to provide, in Pascal language, comprehensive and useful 25 programs , well documented, and showing how to write “good” programs. Note that Niklaus Wirth had published in 1979 a collection of programs to a similar target 26 . The Software Tools were written in the language defined by Kathleen Jensen and Niklaus Wirth, Pascal User Manual and Report of 1978, And the ISO standard proposal. In the same year, Brian Kernighan published the article Why Pascal is not my Favorite Language 27 , in which he denounced the flaws he saw in the language, which he said would prevent him from using it for “serious programming.” The article was based on his experience with the previous book and the comparison he had made with the C , which he also promoted – the book The C Programming Language , co-written with Dennis Ritchie , was released in 1978 28 . In which he denounced the defects which he saw in language, and which according to him prevented him from using it for “serious programming”. The article was based on his experience with the previous book and the comparison he had made with the C , which he also promoted – the book The C Programming Language , co-written with Dennis Ritchie , was released in 1978 28 . In which he denounced the defects which he saw in language, and which according to him prevented him from using it for “serious programming”. The article was based on his experience with the previous book and his comparison with the C , which he also promoted – the book The C Programming Language , co-written with Dennis Ritchie , was released in 1978 28 .

Among the aspects contested in this article, one of them made programming in Pascal particularly complicated: the typing of tables, and consequently the typing of strings also. Indeed, the dimensions of the arrays are part of the type, in Pascal, which prevents to switch to a function of arrays of variable size (or chains of variable size). Bypassing the problem requires either writing multiple versions of functions that take arrays as parameters, or using an array type of the estimated maximum size. This latter “trick” was frequently used in this language; This is how the Numerical Recipes in Pascal programs routinely use it. This disadvantage, Always present in the final version of the ISO 7185 language of 1983, was well known in the field of numerical analysis. For example, at the Conference on the Programming Environment for Development of Numerical Software 29 , organized in 1978 by the Jet Propulsion Laboratory and ACM SIGNUM 30 , one of the presentations 31 showed possible use of Pascal in numerical analysis, Problem of passing an array, proposing an alternative syntax intended to be integrated into a later version of the standard. In fact, it will be added in 1990 to ISO 10206 Extended Pascal . The latter allows, via the diagrams , To create structured types of dynamic size. The same Extended Pascal, all types strings are compatible with each other 32 . But few compilers have followed the ISO 10206 standard to the letter, in particular neither Turbo Pascal nor the compilers derived from it (Delphi and Free Pascal).

Evolution of Pascal

Today, this problem is no longer topical, since the recent versions of Delphi and Free Pascal allow several alternatives:

  • Dynamic allocation with getmem and freemem , and then access to the “array” through a pointer. This is the exact equivalent of what is done in C,
  • Dynamic arrays, with sizing by SetLength 33 ,
  • The “open tables”, the function parameters whose dimension is not specified 34 : this is what comes closest to what the ISO standard allows.

Moreover, another solution, much more relevant in Pascal Objet, is to define a class encapsulating the matrices and their operations. The arguments passed to the functions are then objects.

As for strings, Delphi and Free Pascal provide AnsiString 35 , the size of which is not limited, and more important for what concerns us here, is not part of the type. In reality, these strings are pointers whose management is done transparently for the user.

Another criticism , pointed out by the Numerical Recipes in Pascal , concerns the passage of function as a parameter, very useful in numerical analysis. It is essential to write a calculation library as general as possible: thus, for the integral calculation, one writes a function performing the calculation in the general case, the function to be integrated is unknown, and it is passed to it in parameter in the Particular cases that are needed. This feature is also part of the Extended Pascal, and has not been used in the previously cited work because few compilers implemented it. Again, this is no longer a problem with Delphi and Free Pascal, who accept “procedural types” 36 , which can be passed into parameters.

Descendancy and Relationship

Beyond the commercial variations, a certain number of reliability-oriented Pascal users have switched to either the Portal language , the Modula 2 language and its descendants, or to related languages ​​such as Ada 83 , or to object languages ​​such as Simula , Modula 3, Oberon or Ada 95 .

In particular, Modula as C is enough to write an operating system, but with greater security because of a clear separation between the physical level – the most delicate in carrying operations – and the symbolic level, the easiest To control, and most used. [not clear]

Pascal in teaching

In France

INRIA ‘s portage of the Pascal / CDC on Iris 80 made this language available to university computer centers in the late 1970s. From 1980 onwards, universities and engineering schools Are used as support for algorithms and structured programming . Indeed, it was:

  • More efficient and more open than Algol 60 ,
  • More open, more structured and less permissive than Fortran ,
  • clearer, more structured and less permissive than PL / I .

The arrival of microcomputers introduced a certain competition between Pascal UCSD, Microsoft Pascal, and Turbo Pascal.

The teaching of computer science in preparatory classes was introduced only in 1987, based on Pascal (specifically Turbo Pascal). A library (“MODULOG”), developed by the ALESUP 37 and the IREM of Marseille , was also made available to high schools 38 , 39 , 40 .

With the reform of 1995, the teaching of computing in scientific preparation is separated into a common core, based on a formal computing software, and an optional material in MPSI and MP based, as desired, on Pascal or Caml . The success of the latter is such that at the “Luminy Stage” in 1997, only three participants choose Pascal 41 . Very minority in the MP sector, Pascal is officially abandoned from the 2015 session of the competitions, and since only Caml is authorized to the tests of the computer option.

The prépas BCPST followed a similar path: Pascal entered the program in 1987 and retired in the early 2000s in favor of MATLAB and Scilab . Pascal is no longer available for competitions as of 2004 42 .

In Switzerland

Pascal was first widely adopted. Subsequently, the practical works and developments combining Pascal and assembler led to replace it with the language Portal .


  • Niklaus Wirth , Kathleen Jensen, Pascal User Manual and Report: ISO Standard Pascal , 4 th ed. Springer , 1991 ( ISBN 978-0387976495 )
  • Niklaus Wirth, Introduction to systematic programming ; Trad. By Olivier Lecarme / Masson, Paris, 1977
  • Sanford Leestma, Larry Nyhoff, Pascal Programming and Problem Solving , 4 th ed., Prentice Hall , 1993 ( ISBN 978-0023887314 )
  • Nell Dale, Programming in Pascal , Jones and Bartlett, 1997 ( ISBN 978-0763704841 )