In general, the structure of a Pascal program will include a header, declaration section, and execution section in that order. Alongside learning Pascal functions, it's crucial to grasp the structure of a Pascal program to apply commands and functions logically.
Pascal Program Structure
Basic Structure of a Pascal Program
- Program Name
- Command Usage
- Declaration Type
- Continuous Declaration
- Variable Declaration
- Function Declaration
- Procedure Declaration
- Main Program Block
- Reporting and Expressions within each Block
- Comment
In general, every Pascal program follows a header, declaration, and execution sequence. The format below illustrates the basic syntax of a Pascal program:
program {name of the program}
uses {comma-separated names of libraries}
const {global constant declaration block}
var {global variable declaration block}
function {function declarations, if any}
{ local variables }
begin
...
end;
procedure { procedure declarations, if any}
{ local variables }
begin
...
end;
begin { main program block starts}
...
end. { the end of the main program block }
Pascal Example: Hello World
Below is a simple Pascal code that can print words like Hello, World:
program HelloWorld;
uses crt;
(* Main program block starts here *)
begin
writeln('Hello, World!');
readkey;
end.
The above code produces the output:
Greetings, World!
Below are the various parts of the program:
- The first line of the program program HelloWorld indicates the program's name.
- The second line of the program uses crt, this is a preprocessing command indicating that the compiler includes the crt units before actual compilation.
- The subsequent lines enclosed in parentheses form the main program block. Each block in Pascal is enclosed within a begin (start) statement and an end (end) statement; however, following the end statement of the main Pascal program is a period (.) rather than a semicolon (;).
- The begin statement of the main program block is where the program execution begins.
- Lines within (* ... *) are ignored by the compiler and serve as a comment within the program.
- The command writeln('Hello, World!'); utilizes the writeln function available in Pascal to display the message “Hello, World!” on the screen.
- The command readkey; allows pausing the display of the message until the user presses any key. It's part of the crt unit, a standard unit commonly used in Pascal.
- The end command at the end concludes the program.
Compiling and Executing Pascal Programs
- Open a text editor on your computer, then copy and paste the code above.
- Save the file as hello.pas.
- Open Command Prompt and navigate to the directory where you saved the hello.pas file.
- Type fpc hello.pas there and press Enter to compile your code.
- If there are no errors in your code, Command Prompt will move you to the next line and create the executable file hello and hello.o object file.
- Next, type hello into the Command Prompt window to execute your program.
- Hello World will be displayed on the screen, and the program will wait until you press any key.
$ fpc hello.pas
Free Pascal Compiler version 2.6.0 [2011/12/23] for x86_64
Copyright (c) 1993-2011 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling hello.pas
Linking hello
8 lines compiled, 0.1 sec
$ ./hello
Hello, World!
Make sure that the Free Pascal compiler fpc is in your path and you are running the compiler in the directory containing the source file hello.pas.
Basically, the structure of a Pascal program consists of a header, declaration section, and execution section in that order. To delve deeper into variables and Pascal declaration, readers can look forward to and refer to upcoming articles by Mytour, especially on how to write functions in Pascal. Information about writing functions in Pascal that we have introduced will help readers better understand the structure and commands.