The original Pascal language was built on the basis of some simple concepts, which are now commonly found in programming languages. The most important concept is the data type. The data type determines the values that a variable can take and the operations that can be performed on these values . The concept of Pascal data type is stronger than that of C language and the early BASIC language. In C language, arithmetic data types are interchangeable, but there is no concept similar to data types in the early BASIC language.
Keyword var
Pascal variables must be declared before they are used, and a data type must be specified when declaring variables . The following is an example of variable declaration:
var
Value: Integer;
IsCorrect: Boolean;
A, B: Char;
(1) The keyword var can be used in many places and placed at the beginning of a function or procedure to declare local variables of the function or procedure. It can also be placed in the unit to declare global variables.
(2) After the var keyword is a list of variable names, each variable name is followed by a colon and data type name . Multiple variables can be declared in one line, as in the last sentence of the above example.
(3) Once the variable type is specified, you can only perform operations supported by the variable type on the variable. For example, if you use Boolean values in judgment operations and integer values in numeric expressions, you cannot mix Boolean values and integer values (this is possible in C language).
Variable assignment
Using a simple assignment statement, you can write the following code:
Value := 10;
IsCorrect := True;
But the following statement is incorrect because the two variables have different data types:
Value := IsCorrect; // error
Compiling this code in Delphi, an error message will appear: Incompatible types:'Integer' and'Boolean'. (Type incompatible:'Integer' and'Boolean'). There is no point in assigning a True or False value to an integer variable.
Initial value
The so-called initial value is the value assigned to a variable when it is first declared. It is important to note that global variables in Delphi have default initial values, and their initial values are generally related to types, for example
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var
Num: Integer;
begin
Writeln(Num);
Readln;
end.
In the above case, the Num variable is a global variable and the type is Integer, so its initial value is 0. Of course, global variables can also be directly assigned initial values when they are declared , for example, the following code
uses
System.SysUtils;
var
Num: Integer=10;
begin
Writeln(Num);
Readln;
end.
Local variables cannot be assigned at the time of declaration. At the same time, local variables have no default initial value. If the local variable is not initialized and assigned, the system will give a random value that does not have any meaning . Therefore, when using local variables, the first reference recommendation is assignment operation
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
procedure Show ();
var
Num: Integer;
begin
//此处打印输出Num的值为一个随机的整数
Writeln(Num);
end;
begin
Show();
Readln;
end.
This is why we always need to assign a value of 0 or nil when we define local variables.
Keyword const
For values that remain unchanged during the running of the program, Pascal allows constant declarations. Declaring a constant does not need to have a specific data type, but it needs to be assigned an initial value. The compiler will automatically select the appropriate data type according to the initial value assigned. E.g:
const
Thousand = 1000;
Pi = 3.14;
AuthorName = 'Marco Cantù';
Delphi determines its data type based on the value of the constant. For the Thousand variable in the above example, Delphi will use the SmallInt data type (short integer-the smallest integer type that can accommodate the Thousand variable). If you want to tell Delphi to use a specific type, you can add the type name in the declaration, as follows:
const
Thousand: Integer = 1000;
For famous constants, the compiler has two compilation options:
(1) Allocate memory for the constant, and put the value of the constant into the memory
(2) Copy the constant value each time the constant is used. The second method is more suitable for simple constants.
Constants are also called read-only variables, meaning that they can only be read but not written, which means that their value cannot be changed
Resource string constant
My understanding is that this type of constant is treated as the resource data of the program. For example, when defining a string constant, you can write:
const
AuthorName = 'Marco Cantù';
Starting from Delphi 3, you can write in another way:
resourcestring
AuthorName = 'Marco Cantù';
The above two statements both define a constant, that is, define a value that remains unchanged during the running of the program, but the implementation process of the two is different. The string variable defined by the **resourcestring** instruction will be saved to In the string table of the program resource.
Example: A button is set in, the corresponding code is as follows:
resourcestring
AuthorName = 'Marco Cantù';
BookName = 'Essential Pascal';
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage (BookName + #13 + AuthorName);
end;
The two strings in the above code will be output and displayed in two lines, because the strings are separated by a line break #13.
When you open the executable file with the resource editor, you will see the string you defined in the program resource. This means that the string does not enter the compiled code, but is stored in a separate area of the executable file (EXE file).
In short, the advantages of using resources on the one hand allow Windows to complete effective memory processing, on the other hand, the program can be localized (translating strings into different languages) without changing the source code .