Pascal from the ground up

Your first Pascal program

What Pascal is for, the three parts every program is built from, and your first run: three lines printed with writeln.

Part 1 of 12

Beginner12 min readBack to Courses

A language built for reading

Pascal was designed in 1970 by Niklaus Wirth as a language for teaching programming. That origin still shows: the syntax is regular, the compiler is strict, and a program reads from the top down in the order the machine runs it.

It was never only a classroom language. Delphi and Free Pascal compile it today for real desktop software, and its ideas — typed variables, records, routines declared before they are used — shaped the languages that came after. The compiler behind this page is Free Pascal 3.0.4.

The source you type is plain text, and nothing happens until a compiler turns it into instructions the machine can run. That extra step is where Pascal earns its strictness: a name used before it is declared, a real assigned to an integer, a missing semicolon — each is caught while the program is being built, not while a user waits for an answer.

The three parts of a program

Every program opens with a name, written as program Main;. Then come the declarations: variables, constants, types and routines. After those, a single begin … end block holds the statements that actually run.

The final end. is the line beginners get wrong. It ends with a period, not a semicolon, because the period ends the whole program. An end; with a semicolon only closes a block inside it, and the compiler keeps waiting for more.

Declarations describe; the block between begin and end. does the work. Nothing above that block runs, which is why a program with no statements in its block prints nothing at all.

Printing with writeln

writeln prints the items you give it, in order, and then moves to a new line. write prints them and stays on the same line, so the next output continues where this one stopped.

Pascal inserts nothing between items. There is no separator and no space, so the spacing you want has to live inside the quoted text: writeln('Total ', 42) prints Total 42 because the space is the last character of the text, not something the language adds.

Text sits between single quotes, and one call can mix text and numbers freely. The number is turned into text for you, so there is no format string to learn.

Each writeln is a statement, and statements are separated by semicolons. The semicolon goes between two of them rather than after the last one before end, though Pascal tolerates that final one. When you add a line, copy the punctuation of the line above rather than inventing a new pattern; most first-program errors are punctuation borrowed from a language that does not need it.

A whole program

program Main;
begin
  writeln('Pascal says hello.');
  writeln('Two plus two is ', 2 + 2);
end.

How the compiler reads your file

Free Pascal reads the file in stages. The scanner turns characters into names, numbers and symbols; the parser checks that those pieces form statements in the grammar; then the compiler emits code. A missing semicolon is caught by the parser, which is why the message points at the place where the sentence stopped making sense rather than at the line you forgot.

Case does not matter to the compiler: PROGRAM, Program and program are the same keyword, and Total and total are the same name. Whitespace and comments never reach the parser. Braces { ... }, the pair (* ... *) and a line starting with // all comment out text, which is how you leave a note for the next reader without changing what runs.

Reading the first error

Free Pascal reports the position where parsing stopped, which is often one token after the real mistake. A missing semicolon at the end of a line is noticed when the next line begins with something that cannot follow the previous statement, so the caret points at the start of the next line. Read the message, then look at the line above it. Fix the first error and compile again: the later messages often describe damage the first one caused rather than separate faults.

Three slips account for most first programs: a last line of end; instead of end., no semicolon between two statements on separate lines, and an apostrophe inside a quoted string. The apostrophe needs doubling — writeln('It''s here') — because a single quote would end the text early.

Try these next

  • Change the greeting to a name of your own and add a comment above it.
  • Print a line with write, then finish it with a bare writeln;.
  • Break the last line into end; and read the compiler error before putting the period back.

Print three lines

Run it, then change the greeting and the number. Add a third writeln line of your own and run the program again.

Output
Click "Run" to execute your code

Run the example, then change the greeting and the number and run it again. The output pane shows exactly what the program printed, so a prediction is checked a second later.

Comments and an apostrophe

Run it, then add a comment of your own and a line that prints an apostrophe. Predict where the two halves of each line meet before you run it again.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

Which line ends a whole Pascal program?