Pascal is a high-level, procedural programming language that was developed in 1968–1969 and named after the French mathematician Blaise Pascal. It was designed to help teach structured programming to students – encouraging solid programming practices, code clarity, and organizing programs into well-defined blocks. Implementations of Pascal are available on most computer systems, all the way from early mainframes and 8-bit microcomputers to modern 64-bit platforms, and the language is still used to write software.
This is a short introduction to the Pascal language, not a complete tutorial. Udemy.com offer a more practical and comprehensive tutorial that teaches Pascal programming from its fundamentals to object-oriented extensions.
Integrated Development Environments
There is a large choice of integrated development environments (IDEs) and Pascal compilers available on today’s operating systems. Some of the most common are:
Released in 1983 by Borland, Turbo Pascal was available on several DOS-based operating systems. You can still run it on machines using DOSbox or other IBM PC emulators, but its use for Windows-programming was largely superseded by the release of Delphi.
Originally developed by Borland, Delphi is now owned and developed by Embarcadero Technologies Inc. You can use it to write software for desktop systems, such as Microsoft Windows and Apple Mac OS X, and mobile devices running Android or iOS. Delphi supports Pascal with its object-oriented programming extensions and provides full support for graphical user interfaces.
Free Pascal is a compiler for Pascal that is available for Windows, Mac OS X, various distributions of GNU/Linux, and many other systems. It has good compatibility with Turbo Pascal and Delphi, and is open-source under a modified GNU Public License.
Oxygene, from RemObjects Software LLC, is a commercial Pascal compiler that uses the IDE from Microsoft Visual Studio. It runs on Windows, but also supports building projects for GNU/Linux, Mac OS X, iOS, Android, and other platforms capable of running Java.
The screenshot below is of a complete Pascal program created in Free Pascal on Mac OS X. It does very little – only outputting a few strings to the console – but can be compiled into an executable and run. The key sections of the program are numbered and explained below.
With the exception of keywords that start a code block, lines of Pascal code end with a semicolon. The key parts of the program above are:
Comments can be added to Pascal source files by surrounding them by braces or a combination of parenthesis and asterisks (see below).
- Program Name
Pascal programs begin with the keyword program and then a short name.
Code libraries can be included into a project by first using the keyword uses, and then separating the names of libraries with a comma. In the example above, the sockets library is included but is not actually used by the program.
- Global Variables
Global variables, which are accessible from any part of the program, are declared here. Variables are declared using the keyword var, have unique identifiers, and declare the data type that is used to represent the value. Local variables can be defined inside functions and procedures in the same way. For more information about data types in Pascal, see the next section.
An alternative way of marking comments uses left parenthesis and an asterisk to mark the start of a comment, and an asterisk followed by right parenthesis to mark the end of the comment.
Functions are blocks of code that are separate from the main flow of the program. These are typically called to calculate values and return them. Declaring a function consists of using the keyword function followed by a unique name, any parameters it accepts (in the examples above, none), and the type of value it returns. The actual code for the function is written between the keywords begin and end. Returning values is done by assigning a value to the name of the function.
Procedures are similar to functions, except that they do not return a value.
- Main Code
The main code block begins with the keyword begin and ends with the keyword end. Note that a period is used after end, not a semicolon as used when marking the end of functions and procedures. As you can see, this block is not defined in a function or procedure, and so it is the first piece of code that can be executed when the program is run. In the example above, the main code block writes a string to the console and then calls the procedure displayTitle to continue the program.
As it was designed to encourage structured programming, the ability to plan a project – before writing any code – is especially useful for Pascal programmers. Using flowcharts and writing pseudocode are two of the most important skills you can learn.
Data Types & Structures
Pascal is a strongly-typed language. This means that the type of information stored in a variable (for example, whether it is a number or a string of characters) must be declared before the program is compiled. Among other benefits, this allows the compiler to warn you when you attempt to mix data types, such as if you try to add a number to a Boolean value.
The standard data types in Pascal are:
|Integer||Represents a whole number in the range -32,768 through 32,767. Modern Pascal compilers support additional, non-standard, data types such as longint to represent numbers across a larger range.|
|Real||Represents a number (with decimal places) in the range 3.4x10E-38 to 3.4x10E38.|
|Char||A single character from the 8-bit ASCII set, or a byte value.|
|Boolean||Represents true or false.|
Most compilers support many other data types, pointers, and enumerations. Notably, standard Pascal did not include support for string values (a sequence of characters) but these are generally included in modern implementations of the language.
In addition to simple data types, Pascal supports arrays, sets, and records – a structure that groups a collection of variables together to represent a single item with multiple properties. Records will be instantly recognizable to C programmers who are familiar with structs.
Object Pascal is an extension to the Pascal language specification, and was developed by Apple Computer Inc. It was quickly adopted by Borland for the Delphi programming environment. Object Pascal expands Pascal’s abilities to deal with structures and objects, and has helped to ensure the language’s suitability for use in programming projects today. Most modern compilers currently support Object Pascal.
Learn to Program with Pascal is suitable for beginners and builds on the basic information presented here. In particular, it expands on the brief introduction to Object Pascal with principles and practice.