Pascal basic syntax (I)
preface
Sometimes I'm bored. I look at pascal, but I forget it after reading it So record it
pascal heard that it would be disabled by NOIP, but I didn't verify it Whatever, he was just curious
environment
Using the free compiler fpc[ https://www.freepascal.org/]
The editor can just find one you like, and the IDE can choose lazarus[ https://www.lazarus-ide.org/ ], cross platform
pascal version has been changed and supports many dialects. Based on the current best practice, ${mode objfpc} {$H +} {$J -} {$I +} {$R +} is used in all codes
{$H +} enable AnsiString
{$J-} const constant cannot be modified
{$I+} i/o error detection
{$R +} open out of bounds check
Note: {$mode objfpc} can also be changed to {$mode delphi}
This compilation instruction needs to be written before uses
pascal is not case sensitive. As a result of custom, it adopts a coding specification similar to C#
notes
There are many options for pascal annotations. The first two support multiple lines, and the last line is a single line annotation. pascal annotations allow nesting, but tp and delphi do not support it. In order to be portable, it is recommended not to nest annotations
(* some comments *) { some comments } // some comments
Keywords (reserved words)
Language built-in identifier with special meaning. The keyword list is as follows
absolute and array asm begin case const constructor destructor div do downto else end file for function goto if implementation in inherited inline interface label mod nil not object of operator or packed procedure program record reintroduce repeat self set shl shr string then to type unit until uses var while with xor as class dispinterface except exports finalization finally initialization inline is library on out packed property raise resourcestring threadvar try
Under normal circumstances, write cannot define the same identifier as the keyword Sometimes it must be (for ex amp le, calling the DLL developed by c language) can be preceded by the & symbol
var &var : integer; begin &var:=1; Writeln(&var); end.
identifier
Identifiers can use letters, numbers, underscores (), Cannot start with a number, length 1-127
Overview of data types
The following categories refer to freepascal's documentation
Basic type
type | sizeof | Sign bit |
---|---|---|
byte | 1 | nothing |
word | 2 | nothing |
longword | 4 | nothing |
Cardinal | 4 | nothing |
qword | 8 | nothing |
shortint | 1 | have |
smallint | 2 | have |
longint | 4 | have |
int64 | 8 | have |
integer | 2 or 4 | have |
Integer: byte,shortint,smallint,word,integer,longint,int64
The default value is decimal. If hexadecimal uses $prefix, octal uses & prefix and binary uses% prefix TP(Turbo Pascal) and delphi do not support octal and binary prefix forms
Floating point type: real,single,double,extended,comp,currency
boolean: boolean
String type
String: shortstring, AnsiString, widering, Unicode string, PChar
The string uses single quotation marks and does not support escape in the form of \ r\n\t similar to C language Instead, use a method similar to the following
The LINEENDING constant can be used to break lines across platforms
var a:string; begin a := 'contain tab '#9' example '; a:='contain new line '#13#10' example'; a:='contain new line '+LINEENDING+' example'; end.
structure type
Subbound, enumeration, array, collection, class, file
See the following for specific use
Pointer
var i:integer;p: ^integer; // definition
p:=@i;i=p^; // Take address
It seems that the pointer operation of c language is not as smooth as that of c language. The use of pointers should be discouraged
Basic structure
program {Program name} uses {Comma separated Unit} const {Constant definition} var {Global variable definition} //Function definition function foo();boolean; { Local variable definition } begin ... end; //process definition procedure bar(); { Local variable definition } begin ... end; begin { Main program start} ... end. { End of main program }
See the following for the structure of this executable program and the creation of dll
The following is the structure of Unit, which is similar
unit {Unit name}; interface //Function declaration function foo();boolean; //Process declaration procedure bar(); implementation //Function implementation function foo();boolean { Local variable definition } begin end; //Process implementation procedure bar(); { Local variable definition } begin end; end.
var a:integer; // Variable definition method
a:=10;// Assignment method
Functions and procedures
Function has a return value, procedure does not Everything else is the same
function Function name(Variable name 1:Variable type;Variable name 2:Variable type...):Return type; var Variable name 3:Variable type; Variable name 4:Variable type; begin Function definition result := Return value end;
The return value can also use the function name: = return value. I like to use result: = return value. In addition, result can be used for recursive calls
function Process name(Variable name 1:Variable type;Variable name 2:Variable type...); var Variable name 3:Variable type; Variable name 4:Variable type; begin process definition end;
Cyclic structure
While do loop
while (condition) do S; //for example while i<10 do begin i := i-1; writeln(i); end;
for loop
for < variable-name > := < initial_value > to [down to] < final_value > do S; //for example for i:=1 to 10 do writeln(i); for i:=10 downto 1 do writeln(i);
until loop
repeat S until condition; // for example repeat sum := sum+ i; i := i-1 until i=0;
Break and continue are used to jump out of the loop, which is the same as C language
Of course, there are goto statements, which are hardly used by individuals
Branching structure
if condition then S; if condition then S1 else S2;
Note that there is no else if
case (expression) of L1 : S1; L2: S2; ... ... Ln: Sn; end; case (expression) of L1 : S1; L2,L3 : S2; ... ... Ln: Sn; else Sm; end;
Code example
//Only for grammar demonstration, no specific requirements
demo01.pas
Program demo01; ${mode objfpc } {$H+}{$J-}{$I+}{$R+} Uses SysUtils,Common; Const PI = 3.14; Function FactTail(n,a:integer): longint; Begin If n < 0 Then result := 0 Else Begin If n<=2 Then result := a Else result := FactTail(n-1,n*a); End End; Function Sum(n:integer): integer; Var s: integer; i: integer; Begin s := 0; For i:=1 To n Do s := s+i; result := s; End; Function Mean(n:integer): real; Var s: integer; i:integer; Begin s := 0; i := n; Repeat s := s + n; i := i-1; Until i=0; result := s*1.0/n; End; Var a,b,c: integer; Begin a := 10; b := 20; c := 10; Swap(a,b); writeln(FactTail(5,1), ', ',Sum(c),', ',Mean(c)); End.
common.pas
Unit common; ${mode objfpc } {$H+}{$J-}{$I+}{$R+} Interface Function RectangleArea(l,w :real ): real; Function CircleArea(r :real ): real; Function TriangleArea(a,b,c :real ): real; Procedure Swap(Var a:integer;Var b:integer); Implementation Procedure Swap(Var a:integer;Var b:integer); Var temp: integer; Begin temp := a; a := b; b := temp; End; Function RectangleArea(l,w :real ): real; Begin result := l*w; End; Function CircleArea(r :real ): real; Const PI = 3.14; Begin result := PI*r*r; End; Function TriangleArea(a,b,c :real ): real; Var s : real; Begin s := (a+b+c)/2.0; result := sqrt(s*(s-a)*(s-b)*(s-c)); End; End.