75 lines
2 KiB
Markdown
75 lines
2 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 initially (C was still quite an UNIX-only language)
|
|
due to its simple design, which allowed fast simple single-pass compilers.
|
|
|
|
## 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.
|
|
- Turbo Pascal: old compiler for DOS systems, proprietary
|
|
- Delphi: proprietary, adds [OOP](oop.md) bloat
|
|
|
|
## Examples
|
|
### Hello world
|
|
```pascal
|
|
program HelloWorld;
|
|
|
|
begin
|
|
WriteLn('Hello, World!');
|
|
end.
|
|
```
|
|
|
|
And then compile:
|
|
```
|
|
fpc hellopascal.pas
|
|
```
|
|
|
|
### Factorial
|
|
```pascal
|
|
{
|
|
Calculate a factorial on multiple languages: Pascal version
|
|
Compile with: fpc -O3 -S2 fact.pas
|
|
}
|
|
program Factorials;
|
|
|
|
{ qword = 64 bit unsigned type }
|
|
function FactorialRec(n, acc: qword): qword;
|
|
begin
|
|
if n <= 0 then
|
|
Result := acc
|
|
else
|
|
Result := FactorialRec(n - 1, acc * n);
|
|
end;
|
|
|
|
function FactorialIter(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
|
|
Assert((FactorialIter(0) = 1) and (FactorialRec(0, 1) = 1));
|
|
for n in values do
|
|
begin
|
|
WriteLn('Factorial of ', n, ' = ', FactorialRec(n, 1));
|
|
end;
|
|
end.
|
|
```
|
|
|
|
## Resources
|
|
- [Free Pascal Wiki](https://wiki.freepascal.org/Main_Page)
|
|
- *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)
|