Title here
Summary here
In Pascal, variables are explicitly declared and used by the compiler to check type-correctness of function calls.
program Variables;
uses
SysUtils;
var
a: string;
b, c: Integer;
d: Boolean;
e: Integer;
f: string;
begin
{ Declare and initialize a variable }
a := 'initial';
WriteLn(a);
{ You can declare multiple variables at once }
b := 1;
c := 2;
WriteLn(b, ' ', c);
{ Pascal will infer the type of initialized variables }
d := True;
WriteLn(d);
{ Variables declared without a corresponding
initialization are zero-valued. For example, the
zero value for an Integer is 0 }
WriteLn(e);
{ In Pascal, we don't have a shorthand syntax like ':='
for both declaring and initializing. We need to declare
the variable first and then assign a value }
f := 'apple';
WriteLn(f);
end.
To run this program, save it as variables.pas
and use a Pascal compiler like Free Pascal:
$ fpc variables.pas
$ ./variables
initial
1 2
TRUE
0
apple
In Pascal:
var
keyword, followed by the variable name and its type.:=
for assignment, not for declaration and initialization in one step.WriteLn
procedure is used for output, similar to fmt.Println
in the original example.begin
and end
to denote blocks of code, instead of curly braces.{ }
or parentheses and asterisks (* *)
.Pascal doesn’t have a direct equivalent to Go’s :=
syntax for declaring and initializing variables in one step. Variables must be declared before they can be used, typically in the var
section at the beginning of the program or procedure.