# Pascal

- Appeared in:
- 1970
- Influenced by:
- Influenced:
- Paradigm:
- Typing discipline:
- File extensions:
- .pas
- Dialects:
- Versions and implementations (Collapse all | Expand all):

Pascal (named after Blaise Pascal) is a purely procedural programming language, often used for teaching structured programming.

Pascal was created by Niklaus Wirth in 1970 as an educational language. It was based on ALGOL. Originally programs in Pascal were compiled to bytecode.

In 1983 the first standard of the language was accepted as ISO 7185:1983 (often referred to as Standard Pascal). It aimed not to add new features to the language but rather to formalize the existing ones. In 1990 the second standard ISO/IEC 10206 (Extended Pascal) followed, adding the concept of modules.

Pascal was one of the first languages featuring strong typing and means of structured programming, as well as intuitive syntax with as few ambiguous elements as possible. However, originally it had plenty of faults: lack of variable-length arrays, no means for dynamic memory allocation, limited I/O capabilities, no means for using functions written in other languages etc.

A dialect Object Pascal was developed in 1985 to added object-oriented programming capabilities to the language. At the present moment there are several implementations of Pascal, and the language is suite popular.

## Elements of syntax:

Inline comments | none |
---|---|

Nestable comments | (*...*) or {...} or (*...} or {...*) |

Case-sensitivity | no |

Variable identifier regexp | [A-Za-z_][A-Za-z0-9_]* |

Function identifier regexp | [A-Za-z_][A-Za-z0-9_]* |

Variable assignment | varname := value |

Variable declaration | varname: type |

Variable declaration with assignment | varname: type = value |

Grouping expressions | ( ... ) |

Block | begin ... end |

Deep equality | = |

Deep inequality | <> |

Comparison | < > <= >= |

Function definition | function functionName(argname1: argtype1; ...; argnameN: argtypeN): returntype; |

Function call | functionName(arg1, ..., argN) |

Function call with no parameters | functionName |

Sequence | ; |

If - then | if condition then trueBlock |

If - then - else | if condition then trueBlock else falseBlock |

Loop forever | while true do loopBody |

While condition do | while condition do loopBody |

Do until condition | repeat loopBody until condition |

For each value in a numeric range, 1 increment | for i := first to last do loopBody |

For each value in a numeric range, 1 decrement | for i := last downto first do loopBody |

## Examples:

### Factorial:

Example for versions Free Pascal 2.0.4, Free Pascal 2.2.0, Turbo Pascal 4.0, Turbo Pascal 5.0, Turbo Pascal 5.5, Turbo Pascal 6.0, Turbo Pascal 7.0, gpc 20070904This example uses recursive factorial definition.

Note that this example works in all given implementations of Pascal, but it produces slightly different results. In gpc everything works perfectly. Turbo Pascal and Free Pascal have arithmetic overflow for factorial of numbers greater than 12, but Free Pascal reports an error:

13! = Runtime error 215 at $004013C7

$004013C7

$00401449

$004063E0

while Turbo Pascal doesn’t detect the error and simply prints wrong values:

13! = 1932053504

14! = 1278945280

15! = 2004310016

16! = 2004189184

This example doesn’t work in Turbo Pascal 3.0 and earlier due to absence of `longint`

data type in these versions.

In GNU Pascal this program works without any problems.

```
program factorial;
function fact(n: integer): longint;
begin
if (n = 0) then
fact := 1
else
fact := n * fact(n - 1);
end;
var
n: integer;
begin
for n := 0 to 16 do
writeln(n, '! = ', fact(n));
end.
```

### Hello, World!:

Example for versions Free Pascal 2.0.4, Free Pascal 2.2.0, Turbo Pascal 1.0, Turbo Pascal 2.0, Turbo Pascal 3.0, Turbo Pascal 4.0, Turbo Pascal 5.0, Turbo Pascal 5.5, Turbo Pascal 6.0, Turbo Pascal 7.0, gpc 20070904```
program helloworld;
begin
writeln('Hello, World!');
end.
```

### Fibonacci numbers:

Example for versions Free Pascal 2.0.4, Free Pascal 2.2.0, Turbo Pascal 1.0, Turbo Pascal 2.0, Turbo Pascal 3.0, Turbo Pascal 4.0, Turbo Pascal 5.0, Turbo Pascal 5.5, Turbo Pascal 6.0, Turbo Pascal 7.0, gpc 20070904This example uses recursive definition of Fibonacci numbers.

```
program fibonacci;
function fib(n:integer): integer;
begin
if (n <= 2) then
fib := 1
else
fib := fib(n-1) + fib(n-2);
end;
var
i:integer;
begin
for i := 1 to 16 do
write(fib(i), ', ');
writeln('...');
end.
```

### Factorial:

Example for versions Free Pascal 2.0.4, Free Pascal 2.2.0, Turbo Pascal 1.0, Turbo Pascal 2.0, Turbo Pascal 3.0, Turbo Pascal 4.0, Turbo Pascal 5.0, Turbo Pascal 5.5, Turbo Pascal 6.0, Turbo Pascal 7.0, gpc 20070904This example is exactly the same as main recursive example for Pascal implementations, except for that it uses `real`

data type to store factorial values. Command `writeln(f:-1:0)`

outputs the floating point number `f`

with 0 digits after decimal point and left-justifies it.

```
program factorial;
function fact(n: integer): real;
begin
if (n = 0) then
fact := 1
else
fact := n * fact(n - 1);
end;
var
n: integer;
begin
for n := 0 to 16 do
writeln(n, '! = ', fact(n):-1:0);
end.
```

### Quadratic equation:

Example for versions Free Pascal 2.0.4, Free Pascal 2.2.0, Turbo Pascal 1.0, Turbo Pascal 2.0, Turbo Pascal 3.0, Turbo Pascal 4.0, Turbo Pascal 5.0, Turbo Pascal 6.0, gpc 20070904Pascal has built-in complex data type `complex`

, but using it is inconvenient in this case, because `writeln`

can’t output complex numbers directly, and functions `Re`

and `Im`

would have to be used. In this example calculations are done in real numbers. Library function `halt`

(added in Extended Pascal) exits current block (in later versions it is replaced with `exit`

).

```
program Quadratic;
var
A,B,C,D: integer;
begin
write('A = ');
readln(A);
if (A=0) then
begin
writeln('Not a quadratic equation.');
halt;
end;
write('B = ');
readln(B);
write('C = ');
readln(C);
D := B*B-4*A*C;
if (D=0) then
begin
writeln('x = ',-B/2.0/A);
halt;
end;
if (D>0) then
begin
writeln('x1 = ',(-B+Sqrt(D))/2.0/A);
writeln('x2 = ',(-B-Sqrt(D))/2.0/A);
end
else
begin
writeln('x1 = (',-B/2.0/A,',',Sqrt(-D)/2.0/A,')');
writeln('x2 = (',-B/2.0/A,',',-Sqrt(-D)/2.0/A,')');
end;
end.
```

### CamelCase:

Example for versions Free Pascal 2.2.0, Turbo Pascal 4.0, Turbo Pascal 5.0, Turbo Pascal 5.5, Turbo Pascal 6.0, gpc 20070904This example is similar to previous one, but uses sets of characters for letter check. This makes the code more readable.

Note that in Turbo Pascal series this program works only with Turbo Pascal 4.0 and higher due to the fact that earlier versions didn’t have `char`

datatype.

```
program Camelcase;
var
text, cc: string[100];
c: char;
i: integer;
lastSpace: boolean;
upper, lower: set of char;
begin
upper := ['A'..'Z'];
lower := ['a'..'z'];
readln(text);
lastSpace := true;
cc := '';
for i := 1 to Length(text) do
begin
c := text[i];
if (c in lower) or (c in upper) then
begin
if (lastSpace) then { convert to uppercase }
begin
if (c in lower) then
c := chr(ord(c) - 32);
end
else { convert to lowercase }
if (c in upper) then
c := chr(ord(c) + 32);
cc := cc + c;
lastSpace := false;
end
else
lastSpace := true;
end;
writeln(cc);
end.
```

### CamelCase:

Example for versions Free Pascal 2.2.0, Turbo Pascal 4.0, Turbo Pascal 5.0, Turbo Pascal 5.5, Turbo Pascal 6.0, gpc 20070904This example processes the string char by char, and works with ASCII-codes to figure out whether they are lower- or uppercase letters. `ord`

returns ASCII-code of a character, while `chr`

converts given ASCII-code into a character. String capacity is omitted and thus set to 255 by default.

Note that in Turbo Pascal series this program works only with Turbo Pascal 4.0 and higher due to the fact that earlier versions didn’t have `char`

datatype.

```
program Camelcase;
var
text, cc: string;
c: char;
i: integer;
lastSpace: boolean;
begin
readln(text);
lastSpace := true;
cc := '';
for i := 1 to Length(text) do
begin
c := text[i];
if ((c >= #65) and (c <= #90)) or ((c >= #97) and (c <= #122)) then
begin
if (lastSpace) then
begin
if ((c >= #97) and (c <= #122)) then
c := chr(ord(c) - 32);
end
else
if ((c >= #65) and (c <= #90)) then
c := chr(ord(c) + 32);
cc := cc + c;
lastSpace := false;
end
else
lastSpace := true;
end;
writeln(cc);
end.
```

## Comments

]]>blog comments powered by Disqus

]]>