106 lines
3.4 KiB
Markdown
106 lines
3.4 KiB
Markdown
# Pascal
|
|
Pascal is a procedural programming language created by [Niklaus Wirth](niklaus_wirth.md) in 1970.
|
|
|
|
Pascal was a popular choice for microcomputers in the 70's and 80's, even more popular than [C](c.md) initially (C was still quite an UNIX-only language)
|
|
due to its simple design, which allowed fast simple single-pass compilers. Later on in the mid-80s and early-90s Pascal began
|
|
losing its dominance to C, mainly because of the influence of [OSes](os.md) of that time, which were all written in C.
|
|
|
|
In its "vanilla" form, Pascal is not any more complex than C, aside from having features that C doesn't have,
|
|
such as range types, nested procedures, sets, builtin tagged unions, etc.
|
|
|
|
Pascal has two ISO standards: ISO 7185, informally named "Standard Pascal" an ISO 10206, named "Extended Pascal".
|
|
Because the Pascal described in the ISO 7185 standard is quite useless for practical programming,
|
|
most compilers are for an extended superset of the Pascal described in that standard. A more useful Pascal is
|
|
described on the "Extended Pascal" standard, but by the time it came out it was quite late.
|
|
|
|
## Compilers
|
|
- [FPC](https://www.freepascal.org/) (Free Pascal Compiler): currently the most popular Pascal compiler with wide support
|
|
for different OSes and architectures, libre software.
|
|
- [GNU Pascal](https://www.gnu-pascal.de/gpc/h-index.html): frontend for [GCC](gcc.md), abandoned.
|
|
- Turbo Pascal: old compiler for [DOS](dos.md) systems, proprietary.
|
|
- Delphi: proprietary, adds [OOP](oop.md) [bloat](bloat.md) and [exception](exception.md) handling.
|
|
|
|
## Notes
|
|
### Returning values from functions
|
|
```pascal
|
|
{ The "classical" way, assigning the return value to the function name }
|
|
function Square(n: integer): integer;
|
|
begin
|
|
Square := n * n;
|
|
end;
|
|
|
|
{ Object Pascal and Delphi, assigning to special `Result` variable }
|
|
{ Enable with `-S2` in FPC }
|
|
function Square(n: integer): integer;
|
|
begin
|
|
Result := n * n;
|
|
end;
|
|
|
|
{ Return statement like }
|
|
function Square(n: integer): integer;
|
|
begin
|
|
Exit(n * n);
|
|
end;
|
|
```
|
|
### Identifiers
|
|
Pascal is case-insensitive, so `WriteLn`, `writeLn`, `writeln` and `wRiTeLn` are the same
|
|
identifiers.
|
|
|
|
## Examples
|
|
### Hello world
|
|
```pascal
|
|
program HelloWorld;
|
|
|
|
begin
|
|
writeLn('Hello, World!');
|
|
end.
|
|
```
|
|
|
|
And then compile:
|
|
```
|
|
fpc hellopascal.pas
|
|
```
|
|
|
|
### Factorial
|
|
```pascal
|
|
program Factorial;
|
|
|
|
{ qword = 64 bit unsigned type }
|
|
function factorial(n: qword): qword;
|
|
begin
|
|
Result := 1;
|
|
while n > 0 do
|
|
begin
|
|
{ The usage of C-like arithmetic assignment operators is possible with -Sc: }
|
|
{ Result *= n; }
|
|
Result := Result * n;
|
|
dec(n); { equivalent to C's `--` operator }
|
|
end;
|
|
end;
|
|
|
|
var
|
|
n: qword;
|
|
values: array of qword = (14, 5, 2, 10, 1, 18, 4);
|
|
begin
|
|
for n in values do
|
|
begin
|
|
writeLn('Factorial of ', n, ' = ', factorial(n));
|
|
end;
|
|
end.
|
|
```
|
|
|
|
Compiling:
|
|
```
|
|
fpc -O3 -S2 fact.pas
|
|
```
|
|
|
|
## Resources
|
|
- [Free Pascal Wiki](https://wiki.freepascal.org/Main_Page)
|
|
- [Basic Pascal Tutorial](https://wiki.freepascal.org/Basic_Pascal_Tutorial)
|
|
- [Extended Pascal](https://wiki.freepascal.org/Extended_Pascal) article on the Free Pascal Wiki.
|
|
- *Why Pascal is Not My Favorite Programming Language* by Brian Kernighan, mostly of historic interest as almost all of the
|
|
flaws described in the article has been fixed by modern Pascal compilers. <https://www.lysator.liu.se/c/bwk-on-pascal.html>
|
|
|
|
## See also
|
|
- [Oberon](oberon.md)
|
|
- [PL/0](pl0.md)
|