also see: posts
Introduction
Pascal is an imperative and procedural programming language, which Niklaus Wirth designed in 1968–69 and published in 1970, as a small, efficient language intended to encourage good programming practices using structured programming and data structuring. It is named in honor of the French mathematician, philosopher and physicist Blaise Pascal.
Pascal was developed on the pattern of the ALGOL 60 language. Wirth had already developed several improvements to this language as part of the ALGOL X proposals, but these were not accepted and Pascal was developed separately and released in 1970. A derivative known as Object Pascal designed for object-oriented programming was developed in 1985; this was used by Apple Computer and Borland in the late 1980s and later developed into Delphi on the Microsoft Windows platform. Extensions to the Pascal concepts led to the Pascal-like languages Modula-2 and Oberon. (wiki)
PASCAL COMPILERS
- Online compiler at jdoodle.com.
- CDC 6000 Pascal compiler The source code for the first (CDC 6000) Pascal compiler.
- Free Pascal Free 32/64/16-bit multi-platform Pascal and Object Pascal compiler.
- Modern Pascal is a robust Turbo Pascal, Delphi and Free Pascal interpreter and p-code compiler. Available as a command line tool, Apache Module (like PHP), Middleware solution like ColdFusion, or Stand-Alone multi-threaded server like Node.js. Modern Pascal is available for numerous platforms including Linux, FreeBSD, Mac OS X, Microsoft Windows, 32bit and 64bit CPUs, along with ARM for the Raspberry Pi.
- DWScript aka DelphiWebScript, is an interpreter created by Matthias Ackermann and Hannes Hernler in 2000. Current version runs a dialect of Object Pascal largely compatible with Delphi, but also supports language constructs elements introduced in Prism.
- P4 compiler, the basis for many subsequent Pascal-implemented-in-Pascal compilers, including the UCSD p-System. It implements a subset of full Pascal.
- P5 compiler, is an ISO 7185 (full Pascal) adaption of P4.
- Turbo Pascal was the dominant Pascal compiler for PCs during the 80s and early 90s, popular both because of its powerful extensions and extremely short compilation times. Turbo Pascal was compactly written and could compile, run, and debug all from memory without accessing disk. Slow floppy disk drives were common for programmers at the time, further magnifying Turbo Pascal’s speed advantage. Currently, older versions of Turbo Pascal (up to 5.5) are available for free download from Borland’s site.
- IP Pascal Implements the language “Pascaline” (named after Pascal’s calculator), which is a highly extended Pascal compatible with original Pascal according to ISO 7185. It features modules with namespace control, including parallel tasking modules with semaphores, objects, dynamic arrays of any dimensions that are allocated at runtime, overloads, overrides, and many other extensions. IP Pascal has a built-in portability library that is custom tailored to the Pascal language. For example, a standard text output application from 1970’s original Pascal can be recompiled to work in a window and even have graphical constructs added.
- MIDletPascal – A Pascal compiler and IDE that generates small and fast Java bytecode specifically designed to create software for mobiles
- Morfik Pascal allows the development of Web applications entirely written in Object Pascal (both server and browser side).
- WDSibyl – Visual Development Environment and Pascal compiler for Win32 and OS/2
- PP Compiler, a compiler for Palm OS that runs directly on the handheld computer
- Pascal-S – “Pascal-S: A Subset and Its Implementation”, N. Wirth in Pascal – The Language and Its Implementation, by D.W. Barron, Wiley 1979.
- THINK Pascal, an ObjectPascal dialect, initially developed by Symantec for classic Mac OS.
STRUCTURE OF A PASCAL PROGRAM
Every Pascal program must follow a basic structure. Below is the basic structure that every Pascal program must follow.
EXAMPLE
PROGRAM ProgramName; VAR VariableName : VariableType; VariableName : VariableType; ... PROCEDURE ProcedureName; variables here if necessary BEGIN Some Code; END; FUNCTION FunctionName(variableList): VariableType; variables here if necessary BEGIN Some Code if necessary; FunctionName := some expression More Code if necessary; END; ;... more functions and procedures if necessary ... BEGIN the main program block. It should be small and all work should be delegated to the procedures and functions. It often consists of a WHILE loop that calls in turn procedures and functions in the appropriate order. END.
Note: The functions and procedures can appear in any order. The only requirement is that if one procedure or function uses another one, that latter one must have been defined already.
VARIABLE TYPES
There are five basic variable types in Pascal: INTEGER, REAL, CHAR, BOOLEAN, and STRING. They are defined as follows:
INTEGER | A positive or negative integer between a smallest (negative) and a largest number. In general the smallest and largest number possible depends on the machine; for IBM PC and Turbo Pascal they are:
|
||||
REAL | Can contain a real number in scientific or decimal notation. There is a limit on the size and accuracy of the real number that will be covered later. Valid real numbers are, for example:
|
||||
CHAR | Any key on the keyboard is considered a valid character. Characters are usually enclosed in single quotes. For example: '1' is a character, while 1 is an integer. | ||||
BOOLEAN | We will deal with boolean variables later | ||||
STRING | A string is a collection of up to 255 characters enclosed in single quotes. For example: 'Bert' is a string of 4 characters. More details about strings will follow later. |
ASSIGNING VALUES TO VARIABLES
Variables are simply a name for a block of memory cells in main memory. If a value is assigned to a variable, that value must be of the same type as the variable, and will be stored in the memory address designated by the variable name. The assignment statement is the semicolon-equal :=.
- Variables must be declared at the beginning of the program, a procedure, or a function.
- Variables must be initialized before they can be used.
- Variables can be reused as often as necessary. Their old value is simply overwritten by a new assignment.
EXAMPLE
PROGRAM Test; VAR x : REAL; { variable name is x, type is real i : INTEGER: { variable name is i, type is integer c : CHAR; { variable name is c, type is character s : STRING; { variable name is s, type is string BEGIN x := -34.55; { valid real number assigned to variable x } x := -3.9E-3; { valid real number assigned to variable x } WRITELN(x); { x contains the value -3.9E-3 } i := 10; { valid integer number assigned to variable i } i := i * i; { valid (!) - i will be 100 now } i := 9933; { valid integer number assigned to variable i } i := -99999; { invalid integer - too small } i := 999.44; { invalid assignment - types do not match } c := '1'; { valid character assigned to variable c } c := 1; { invalid assignment - types do not match } c := 'Bert'; { invalid assignment - types do not match } c := 'd'; { valid character assigned to variable c } WRITELN(c); { c contains the value 'd' } d := 'c'; { unknown variable - the variable d is not declared } WRITELN(s); { invalid reference - s has undefined value } END.
FORMATTED INPUT AND OUTPUT
Reading Information
- To read information from the keyboard, you can the command READLN, as in the following:
EXAMPLE
PROGRAM Test; VAR x : REAL; { x is declared to be real } i : INTEGER; { i is declared to be an integer } c : CHAR; { c is declared to be a character } BEGIN READLN(x); { user can type a real number, followed by the return key. The value will be stored in the variable x. If the user input is not a real (or integer) number, a runtime error (invalid assignment) will occur. } READLN(i); { user can type a integer, followed by the return key. The value will be stored in the variable i. If the user input is not an integer, a runtime error (invalid assignment) will occur. } READLN(c); { user can type any character, followed by the return key. The value will be stored in the variable c and will be a character. If a user enters 1, c will be the character '1' not the integer 1. } READLN; { user can type a single return. } END.
The exact workings of the READLN command will be discussed later.
Writing Information
- To write information on the screen, you can use the WRITE or WRITELN command. You can write the content of variables or simple text. There are several variations:
Writing Text WRITE('any text'); writes any text enclosed in simple quotes on the screen Writing integers unformatted WRITE(I); I is an integer variable Writing integers formatted WRITE(I:num); I is an integer and num indicates the total positions to be used. If the value contained in the variable I needs more digits, num is ignored. Writing reals unformatted WRITELN(x); x is a real variable. Will always write the real number in scientific notation and is almost never what you want. Writing reals formatted WRITELN(X:num1:num2); X is a real variable, num1 is the total amount of digits to use (including sign and period) and num2 is the number of digits after the period.
Note: The same rules apply for the command WRITELN but this command also positions the cursor to the first position of the next line.
You can combine writing text and more than one variable by separating the individual components by a comma. Here is an example:
PROGRAM Test; VAR x : REAL; i : INTEGER; j : INTEGER; BEGIN x := 12.449; i := 10; j := -300; WRITE('This is some text'); WRITELN('Unformatted integer ',i); WRITELN('Unformatted integer computation ',i*i); WRITELN('formatted integer',i:4); WRITELN('formatted integer',j:4); WRITELN('Unformatted real ',x); WRITE('Formatted real'); WRITE(x:8:2); WRITELN('all in one line'); END.
which will produce the following output:
This is some text Unformatted integer 10 Unformatted integer computation 100 formatted integer 10 formatted integer-300 Unformatted real 1.24490000000E+01 Formatted real 12.45all in one line
FUNCTIONS
Functions provide a flexible method to apply one formula many times to possibly different values. They are comparable to procedures but
functions are of always of a certain type | |
functions usually have one or more input variable(s) | |
the function name must appear at least once inside the definition |
The general form of the function statement looks like this:
FUNCTION FunctionName(VariableName: VariableType): VariableType; BEGIN some code, if necessary; FunctionName := some computation; more code if necessary; END;
Note that every function must contain the function name at least twice: once in the definition of the function, and once to assign the result of a computation to the function. Functions can be used similar to variables. You can assign the result of a function to a new variable, you can print the result of a function using WRITE or WRITELN, or you can use the result of a function in another computation or test.
EXAMPLE
PROGRAM Test; VAR radius: REAL; FUNCTION CircleArea(r : REAL): REAL; BEGIN CircleArea := 3.1415 * r * r; END; BEGIN WRITE('Area of circle with radius 2.0: '); WRITELN(CircleArea(2.0):6:1); WRITE('Area of circle with radius 5.0: '); WRITELN(CircleArea(5.0):6:1); WRITE('Enter your own radius: '); READLN(radius); WRITE('Area of circle with radius ', radius:3:1,': '); WRITELN(CircleArea(radius)); radius := 5.0; radius := CircleArea(radius); WRITELN(radius); END.
Notes