Variables in Pascal

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:

  1. Variables are declared using the var keyword, followed by the variable name and its type.
  2. Multiple variables of the same type can be declared on a single line.
  3. Pascal uses := for assignment, not for declaration and initialization in one step.
  4. The WriteLn procedure is used for output, similar to fmt.Println in the original example.
  5. Pascal is not case-sensitive for keywords, but it’s a common convention to use lowercase for keywords and CamelCase for identifiers.
  6. Pascal uses begin and end to denote blocks of code, instead of curly braces.
  7. Comments in Pascal are enclosed in 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.