The Const keyword | parameters | record | expressions | Block
|
Smart pascal source code
// ######## CASE I ########################
const ONE = 1;
const TWO = ONE + ONE;
const MINUS_ONE = -ONE;
procedure WriteLnOut(Value: Integer = MINUS_ONE);
begin
Writeln(IntToStr(Value));
end;
WriteLnOut();
WriteLnOut(TWO + MINUS_ONE - ONE);
WriteLnOut(ONE);
WriteLnOut(TWO);
WriteLnOut($100);
{ <<<<<<<<<<<< CONSOLE OUTPUTS >>>>>>>>>>>>>>>>>>>
-1
0
1
2
256
}
// ######## CASE II ########################
const X = 1024;
const Y = Round(X / 5);
const Z = Trunc(X / 5);
Writeln(IntToStr(Y));
Writeln(IntToStr(Z));
{ <<<<<<<<<<<< CONSOLE OUTPUTS >>>>>>>>>>>>>>>>>>>
205
204
}
// ######## CASE III ########################
// EMPTY ARRAY ----> VAR X CONST <-----
//var ARR := [];
Const ARR = [];
var i := 0;
try
WriteLn(ARR[i]);
except
on E: Exception do
WriteLn(E.Message);
end;
// ######## CASE IV ########################
// -----> CONST BLOCK <-------
procedure Stuff1;
const
a = 1;
b = 2;
var
c : Integer;
d : Integer;
begin
c:=d;
end;
procedure Stuff2;
var
c : Integer;
d : Integer;
const
a = 1;
b = 2;
begin
c:=d;
end;
procedure Stuff3;
var
c : Integer;
d : Integer;
begin
c:=d;
end;
procedure Stuff4;
const
a = 1;
b = 2;
begin
end;
// ######## CASE V ########################
// -----> CONST IN PARAMETERS <-------
procedure Test(const i : Integer);
begin
WriteLn(i);
end;
Test(1);
Test(1+2);
var j = 10;
Test(j+1);
Test(2*j);
Test(Round(j*PI));
{ <<<<<<<<<<<< CONSOLE OUTPUTS >>>>>>>>>>>>>>>>>>>
1
3
11
20
31
}
// ######## CASE VI ########################
// -----> CONST IN PARAMETERS II <-------
type
TRec = record
Field: Integer;
end;
function Func1(const r: TRec): Integer;
begin
Result := r.Field;
end;
function Func2(const r: TRec): TRec;
begin
Result.Field := r.Field + 1;
end;
var
r: TRec;
Writeln(Func1(r));
Writeln(Func1(Func2(r)));
r := Func2(r);
Writeln(Func1(r));
Writeln(Func1(Func2(r)));
{ <<<<<<<<<<<< CONSOLE OUTPUTS >>>>>>>>>>>>>>>>>>>
0
1
1
2
}
// ######## CASE VII ########################
// --- CONST with RECORD TYPE --
type
TRec1 = record
w, x, y: Integer;
z: Float;
Name: string;
end;
const
c: TRec1 = (x: 1; y: 2; z: 3.1; Name: 'hello');
Writeln(c.w);
Writeln(c.x);
Writeln(c.y);
Writeln(c.z);
Writeln(c.Name);
{ <<<<<<<<<<<< CONSOLE OUTPUTS >>>>>>>>>>>>>>>>>>>
0
1
2
3.1
hello
}