articles/pascal.md: update
This commit is contained in:
parent
a73200e699
commit
677467e04c
1 changed files with 9 additions and 18 deletions
|
@ -1,17 +1,17 @@
|
||||||
# Pascal
|
# Pascal
|
||||||
Pascal is a procedural programming language created by [Niklaus Wirth](niklaus_wirth.md) in 1970.
|
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)
|
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.
|
due to its simple design, which allowed fast simple single-pass compilers.
|
||||||
|
|
||||||
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
|
In its "vanilla" form, Pascal is not any more complex than C, aside from having features that C doesn't have,
|
||||||
and nested procedures.
|
such as range types, nested procedures, sets, etc.
|
||||||
|
|
||||||
## Compilers
|
## Compilers
|
||||||
- [FPC](https://www.freepascal.org/) (Free Pascal Compiler): currently the most popular Pascal compiler with wide support
|
- [FPC](https://www.freepascal.org/) (Free Pascal Compiler): currently the most popular Pascal compiler with wide support
|
||||||
for different OSes and architectures, libre software.
|
for different OSes and architectures, libre software.
|
||||||
- Turbo Pascal: old compiler for DOS systems, proprietary.
|
- Turbo Pascal: old compiler for [DOS](dos.md) systems, proprietary.
|
||||||
- Delphi: proprietary, adds [OOP](oop.md) bloat and exception handling.
|
- Delphi: proprietary, adds [OOP](oop.md) [bloat](bloat.md) and [exception](exception.md) handling.
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
### Returning values from functions
|
### Returning values from functions
|
||||||
|
@ -54,21 +54,13 @@ fpc hellopascal.pas
|
||||||
### Factorial
|
### Factorial
|
||||||
```pascal
|
```pascal
|
||||||
{
|
{
|
||||||
Calculate a factorial on multiple languages: Pascal version
|
Calculate a factorial on multiple languages: Pascal version
|
||||||
Compile with: fpc -O3 -S2 fact.pas
|
Compile with: fpc -O3 -S2 fact.pas
|
||||||
}
|
}
|
||||||
program Factorials;
|
program Factorials;
|
||||||
|
|
||||||
{ qword = 64 bit unsigned type }
|
{ qword = 64 bit unsigned type }
|
||||||
function FactorialRec(n, acc: qword): qword;
|
function Factorial(n: qword): qword;
|
||||||
begin
|
|
||||||
if n <= 0 then
|
|
||||||
Result := acc
|
|
||||||
else
|
|
||||||
Result := FactorialRec(n - 1, acc * n);
|
|
||||||
end;
|
|
||||||
|
|
||||||
function FactorialIter(n: qword): qword;
|
|
||||||
begin
|
begin
|
||||||
Result := 1;
|
Result := 1;
|
||||||
while n > 0 do
|
while n > 0 do
|
||||||
|
@ -84,10 +76,9 @@ var
|
||||||
n: qword;
|
n: qword;
|
||||||
values: array of qword = (14, 5, 2, 10, 1, 18, 4);
|
values: array of qword = (14, 5, 2, 10, 1, 18, 4);
|
||||||
begin
|
begin
|
||||||
Assert((FactorialIter(0) = 1) and (FactorialRec(0, 1) = 1));
|
|
||||||
for n in values do
|
for n in values do
|
||||||
begin
|
begin
|
||||||
WriteLn('Factorial of ', n, ' = ', FactorialRec(n, 1));
|
WriteLn('Factorial of ', n, ' = ', Factorial(n));
|
||||||
end;
|
end;
|
||||||
end.
|
end.
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue