Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

Program Structure, Comments and Style

Learn how a Pascal program is organized: the program header, declaration sections, the main block, comment styles, identifiers, case rules, and clean formatting conventions.

Pascal programs have a strict, predictable layout. At first it may feel like paperwork, but it gives every program the same shape: what the program needs is declared at the top, and what it does is in one block at the bottom. This lesson maps that layout, explains how to write comments, shows the rules for identifiers and statements, and ends with a short style guide.

The listings are read-only Pascal; the runnable demo is a labelled C stand-in.

The shape of a program

flowchart TD
    A["program Name;"] --> B["uses ... ;<br/>(optional units)"]
    B --> C["const / type / var<br/>(declarations)"]
    C --> D["procedures and functions"]
    D --> E["begin<br/>main block<br/>end."]

Figure: sections in the order the compiler expects them.

A fuller example showing every part:

PASCAL
program Layout;

uses
  SysUtils;

const
  Greeting = 'Hello';

type
  TCount = Integer;

var
  total: TCount;

procedure Announce;
begin
  WriteLn(Greeting, ', total is ', total);
end;

begin
  total := 5;
  Announce;
end.

Output:

TEXT
Hello, total is 5

Each part has a job:

  • program header. Optional in Free Pascal, required in strict standard Pascal.
  • uses. Lists units (libraries) to import. SysUtils provides string and conversion helpers.
  • const, type, var. Declare constants, new type names and variables. They may appear in any order and more than once in Free Pascal and Delphi, but the traditional order is shown above.
  • Procedures and functions. Reusable pieces of code, declared before they are called.
  • Main block. The code between the last begin and end. runs first.

The rule behind the layout is declare before use: the compiler reads the file once from top to bottom, so a name must exist before the line that mentions it.

Statements and semicolons

A statement is one instruction, such as an assignment or a procedure call. In Pascal the semicolon separates statements; it is not a terminator. That is why the last statement before end may omit it:

PASCAL
begin
  WriteLn('one');
  WriteLn('two')   { legal: no semicolon needed before end }
end.

Most people still write the semicolon so adding a line later is painless. A begin ... end pair groups several statements into a compound statement, which is how you put more than one statement where only one is allowed (for example in an if).

Two traps for newcomers:

  1. A semicolon directly before else is an error (you will see why in If, Else and Case).
  2. end closing a program has a full stop, but end closing a block inside it has a semicolon.

Comments

Pascal has three comment styles:

PASCAL
{ A brace comment, may span
  several lines. }

(* A parenthesis-star comment.
   Useful when the text contains braces. *)

// A line comment, to the end of the line (Free Pascal and Delphi).

You cannot nest two comments of the same kind. Using a different kind inside works, so you can comment out code that already has comments:

PASCAL
(*
{ old code }
WriteLn('temporary');
*)

A brace followed immediately by a dollar sign, {$...}, is not a comment but a compiler directive, like {$mode objfpc}. Be careful not to start a real comment with {$.

Identifiers and case

An identifier names a program, variable, type or routine. The rules:

  • Start with a letter or underscore; then letters, digits and underscores.
  • No spaces or punctuation (total-score is a subtraction, not a name).
  • Not a reserved word such as begin, end, if, var, type.
  • Length is practically unlimited.

Pascal is not case sensitive: Total, total and TOTAL are the same identifier, and so are WRITELN and WriteLn. This means you cannot have two variables called count and Count in the same scope. By convention programmers capitalize each word (TotalScore) and prefix types with T (TCustomer).

Style conventions

Pascal compilers do not care about layout, but readers do. A common house style:

  • Two-space indentation inside begin ... end.
  • Keywords in lower case; identifiers in PascalCase (TotalScore).
  • Type names begin with T, constants are PascalCase or ALL_CAPS, parameters often begin with A (AValue) in Delphi code.
  • One statement per line.
  • Short comments that say why, not what.

Layout of a typical Delphi project

When you create a Delphi or Lazarus application you get several files:

File Purpose
.dpr / .lpr The project's main program file (the program unit)
.pas Unit source files, one per form or module
.dfm / .lfm Form layout (components and properties)
.dproj / .lpi Project settings

In the console examples of this course, everything lives in one .pas or .dpr file. the Units chapter explains the multi-file layout.

Stand-in: ordering matters

Because the compiler reads top to bottom, a routine must exist before it is called. C has the same rule, so a C stand-in shows the effect: swap the order of the two definitions and the compiler would complain unless a forward declaration exists. The program below is C, structured like the Pascal layout above.

C C stand-in: declare before use

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the usual order of the top-level parts of a Pascal program?

    The compiler reads top to bottom, so names must be declared before use; the main block comes last and ends with end..
  2. 2. Which of these is a valid Pascal comment?

    Braces, parenthesis-star pairs and (in Free Pascal and Delphi) double slashes start comments. C-style block comments are not Pascal.
  3. 3. Are Total and total the same variable?

    Identifiers and keywords are case-insensitive in Pascal. Two names that differ only in case clash.
  4. 4. What is {$mode objfpc}?

    A brace followed by a dollar sign is a directive for the compiler, not a comment.

Summary

  • A program is: header, uses, declarations (const, type, var), routines, then begin ... end..
  • Semicolons separate statements; begin ... end groups them.
  • Comments: { }, (* *), //. {$ ... } is a compiler directive.
  • Identifiers are case-insensitive; follow PascalCase and the T prefix for types.

Next: Variables, Constants and Assignment