Your First Pascal Program
The traditional first program in any language simply prints a greeting to the screen, and in Pascal that means using the WriteLn procedure inside a minimal program skeleton: a heading, an empty declaration section, and a main begin...end block. WriteLn appends a newline after its output automatically, while its sibling Write does not, and both accept a comma-separated list of values, strings, numbers, or expressions, that get concatenated in the printed output without any manual string formatting required.
Cricket analogy: WriteLn automatically starting a new line after each call is like a scorer automatically starting a fresh line in the scorebook after each over, you don't have to manually insert the line break yourself.
Compiling and Running the Program
Once the source file is saved with a .pas extension, for example 'greet.pas', you compile it from the terminal with 'fpc greet.pas', which produces an executable (greet on Linux/macOS, greet.exe on Windows) alongside object files used internally by the compiler. Running that executable directly, './greet' or 'greet.exe', executes your compiled machine code immediately, no separate interpreter step is involved the way there is with Python, because Pascal compiles all the way down to native code ahead of time.
Cricket analogy: Compiling greet.pas into a standalone executable is like a player completing all their training drills and finally being match-ready, once compiled, the program runs directly without needing the 'coach' (compiler) present anymore.
Reading User Input
To make a program interactive, Pascal provides ReadLn, which pauses execution, waits for the user to type a line of text and press Enter, and stores that value into the variable you pass it, automatically converting the typed text into the variable's declared type. If you call ReadLn(age) where age is an Integer, and the user types something that isn't a valid whole number, the program will raise a runtime error, which is why well-written Pascal programs often validate input or wrap risky reads in error handling before trusting the value.
Cricket analogy: ReadLn pausing execution until the user responds is like a bowler waiting at the top of their mark for the umpire's signal before starting their run-up, nothing proceeds until that input arrives.
program Greet;
var
userName: String;
age: Integer;
begin
Write('What is your name? ');
ReadLn(userName);
Write('How old are you? ');
ReadLn(age);
WriteLn('Hello, ', userName, '!');
WriteLn('In 10 years you will be ', age + 10, ' years old.');
end.Write and WriteLn accept optional field-width and decimal-place formatting, for example WriteLn(price:8:2) prints 'price' right-aligned in an 8-character field with exactly 2 decimal places, which is the standard way to format currency-like output in Pascal without a separate formatting library.
Calling ReadLn(age) when age is declared as Integer and the user types non-numeric text (like 'twenty') will cause a runtime error and abruptly terminate the program unless you've wrapped the read in exception handling. Beginners often assume Pascal 'gracefully' rejects bad input the way some form-validation libraries do; it does not, by default.
- A minimal Pascal program is a heading followed by an empty declaration section and a begin...end block.
- WriteLn prints values and appends a newline; Write prints without one.
- Source files are compiled with 'fpc filename.pas', producing a native executable ahead of time.
- The compiled executable runs directly with no separate interpreter step needed at runtime.
- ReadLn pauses execution and converts typed input into the target variable's declared type.
- Passing bad input to ReadLn for a numeric variable causes a runtime error, not a graceful rejection.
- Field-width and decimal formatting, like WriteLn(x:8:2), control output alignment without extra libraries.
Practice what you learned
1. What is the key difference between WriteLn and Write in Pascal?
2. What command compiles a Pascal source file named greet.pas using Free Pascal?
3. What happens if ReadLn(age) is called with age declared as Integer, and the user types 'twenty'?
4. What does WriteLn(price:8:2) control?
5. Why doesn't a compiled Pascal executable need a separate interpreter at runtime?
Was this page helpful?
You May Also Like
Pascal Program Structure
How every Pascal program is organized into a program heading, declaration sections, and a main statement block.
Pascal Data Types and Variables
An overview of Pascal's built-in data types, variable declarations, and how the language's strict typing shapes everyday code.
Installing Free Pascal
How to install the Free Pascal compiler and the Lazarus IDE on your system and verify your setup with a test compile.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics