Modern Pascal Language Syntax
Modern Pascal follows the common structure of other Pascal dialects. Pascal is a procedural programming language, designed in 1968 and published in 1970 by Niklaus Wirth and named in honor of the French mathematician and philosopher Blaise Pascal. Pascal runs on a variety of platforms, such as Windows, Mac OS, and various versions of UNIX/Linux.
This tutorial will give you great understanding of Pascal to proceed with Modern Pascal and other related frameworks etc.
Audience
This tutorial is designed for Software Professionals who are willing to learn Pascal Programming Language in simple and easy steps. This tutorial will give you the understanding of Modern Pascal Programming concepts, and after completing this tutorial, you will be at an intermediate level of expertise from where you can take yourself to higher level of expertise.
Prerequisites
Before proceeding with this tutorial you should have an understanding of software basic concepts like: what is source code, a compiler, text editor and execution of programs, etc. If you already have understanding on any other computer programming language, then it will be an added advantage to proceed.
Overview
Pascal is a general-purpose, high-level language that was developed for teaching programming as a systematic discipline and to develop reliable and efficient programs. Pascal offers several data types and programming structures. It is easy to understand and maintain Pascal programs.
Pascal has grown in popularity in the teaching and academia arena for various reasons:
- Easy to learn.
- Structure language.
- It produces transparent, efficient and reliable programs.
- It can be compiled on a variety of computer platforms.
Features of the Pascal Language
Pascal has the following features:
- Pascal is a strongly typed language.
- It offers extensive error checking.
- It offers several data types like arrays, records, files and sets.
- It offers a variety of programming structures.
- It supports structured programming through functions and procedures.
- It supports object oriented programming.
How Pascal is different from other languages:
- It is not case-sensitive, so foo, Foo and FOO are the same.
- It uses the keyword procedure or function for methods instead of just function.
- It uses the semicolon to separate individual statements within a compound statement.
- It requires variables to be defined outside of the compound statement.
- It does not use a separate file for your header or object definition.
- It does not have a problem tracking variable usage (no need for #ifndef variable slop).
- It has always support a true Boolean type.
- It has a more abstract high level method for dealing with bitwise data and sets.
- It is more strict with floating types.
Before we study the language, let us look at the bare minimum Pascal structure so we can take it as a reference in the upcoming language tutorials.
Pascal Program Structure
A Pascal program basically consists of the following parts (many are optional):
- Comments
- Program or Unit name
- Comments
- Header or Unit public declarations
- Comments throughout the declarations
- Public Uses command
- Constant declarations
- Variable declarations
- Type declarations
- Class declarations
- Constant declarations (again)
- Variable declarations (again)
- Function declarations
- Procedure declarations
- Main Program or Unit private implementations
- Comments throughout declarations and implementations
- Private Uses command
- Private Constant declarations
- Private Variable declarations
- Private Type declarations
- Private Class declarations
- Private Constant declarations (again)
- Private Variable declarations (again)
- Class implementations
- Statements and Expressions within each
- Function implementations
- Statements and Expressions within each
- Procedure implementations
- Statements and Expressions within each
- Program Main Routine
- Unit Initialization
- Unit Finalization
Every Pascal program generally has a header section, a declaration and an execution part structure in that order. Following format shows the basic syntax for a Modern Pascal program:
program HelloWorld;
uses display;
(* Here the main program block starts *)
begin
WriteLn('Hello, World!'); { comment about this command }
Readkey;
end.
Source Code Parts...
Looking at the above program source code:
- The first line of the source code states this is a Program called HelloWorld which should correspond to the filename.
- The next line states a preprocessor command, telling the compiler to include the unit display's commands before going to actual compilation.
- Next is one style of comment, which will be ignored by the compiler and is there to help explain the functionality to another programmer - or remind yourself what a routine is doing. Comments are totally optional, some businesses require comments to help others read your code.
- The next section, is the Main Program implementation, enclosed within the begin and end statements. Every block of code in Pascal is enclosed within a begin statement and an end statement. However, the end statement indicating the end of the Main Program source is followed by a full stop (.) period instead of an end of block semicolon (;).
- The begin statement of the Main Program block is where the modern pascal execution begins.
- The statement WriteLn('Hello, World!'); uses the WriteLn function available in the display Unit which causes the message "Hello, World!" to be displayed on the screen with a Carriage Return and Linefeed.
- The WriteLn function is followed by another style of optional comment using braces { ... } usually used to denote something about the command to the left, or sometimes used to disable a block or piece of code during debugging.
- The statement Readkey; allows the display to pause until the user presses a key on the keyboard. It is also part of the display unit. Again, a unit is a library or collection of reusable Types, Objects, Classes, Constants, Variables, Functions and Procedures.
- The last statement end. terminates the execution of your program.
To get a better understanding we will detail each of the basic building blocks of the pascal programming language.
Type Declarations
Type declarations provide you with the ability to extend or expand the variable types known by Modern Pascal. For example, a collection of characters is called a string, however, if you wanted to limit it to 20 characters maximum you could introduce a new string type of String[20]. Type declarations are normally defined at the beginning of your code with a type keyword, followed by definitions of the variable types as follows:
type type_name1=existing_definition; // allows you to "rename" a type for your better understanding
type_name2=new_definition; // allows you to add a new type definition to Modern Pascal
type_name3=array_structure; // allows you to simplify interaction with an array type
type_name4=record_structure; // allows you to introduce a type of multiple variables
type_name6=class_structure; // allows you to introduce a type with associated methods
Variables
Variables hold data which can be changed during the operation of your program. Variable definitions are normally defined at the beginning of your code with a var keyword, followed by the type of the variable as follows:
var variable1:variable_type;
variable2:variable_type;
variable3, variable4:variable_type;
Functions and Procedures
In Pascal, a Procedure is a set of instructions to be executed that do not return a value, and a Function is a procedure which returns a value. The definition of each follows:
Procedure Proc_Name(optional_parameters...);
Function Func_Name(optional_parameters...):Return_Value_Type;
Comments
Pascal is the only language which supports 4 different styles of comments. As we have discussed and shown on the Program Structure page, you can use (* ... *) for single or multiple lines of comment, you can use { ... } for single and multiple lines of comment too and // the double slash for comment the rest of the current line.
(* This is an example of a single line comment in this style *)
(* This is an example of using this style for multi-line
comments as it will span multiple lines. *)
{This is an example of a single line comment in this style}
{This is an example of this style for multi-line
comments as it will span multiple lines.}
// Lastly, this is a single line comment - or
var a:Integer; // comment the rest of this line, or about this line.
/* Modern Pascal also supports C/Java/JavaScript style of comments
which use the slash asterisk combination instead of parenthesis
asterisk combination (like formal Pascal) */
Case Sensitivity
Pascal is not a case-sensitive language, which means you can write your variables, function and procedure in either case. Like A_Variable, a_variable and A_VARIABLE all have the same meaning in Pascal. It is a good writing habit though to be consistent as if the language was case-sensitive for working with certain tools and learning/using other programming languages which usually are case-sensitive.
Statements
Pascal programs and units are made of statements. Each statement specifies a task or job of the program. These jobs could be declarations, assignment, calculations, reading or writing data, comparisons, making logical decisions, transferring program flow control, etc. All statements end with a semicolon:
WriteLn('Hello, World!');
Ch:=Readkey;
if (Ch=#27) then WriteLn('Goodbye.');
Naming Convention
In Pascal, types, class names, constants, variables, function names and procedure names following a strict naming convention:
First Character must be A to Z, a to z or _.
Second Character onward can include 0..9.
The following are reserved characters and cannot be used except for their explicate functionality(s):
~, `, !, %, &, |, \, ? (question mark) are not used in Pascal
' (apostrophe) is used for a string 'start and end'.
" (quotation) is also supported for a string "start and end". (deviation to standards!)
$ is used for hexadecimal example $0A.
# is used for embedding an ASCII character, like #39 for apostrophe.
@ is used for the address of a variable.
^ is used to de-reference a pointer or pointer structure.
* is used for multiplication.
- is used for subtraction.
+ is used for addition.
= and == are used for equality comparisons. (deviation of standards!)
/ is used for division which could result in fractional where div is used where result is a whole number.
( ) are used for grouping logic, passing parameters are part of the (* ... *) comment.
{ } are used for comments.
[ ] are used for array elements.
: used for variable name and type separator and part of := used for assignment.
; used to denote end of statements or blocks of code.
< is used for less than comparison and <= is used for less than or equal to comparison.
> is used for greater than comparison and >= is used for greater than or equal comparison.
<> and != are used to denote not equal. (deviation of standards!)
, comma is used in certain cases for multiple element separation.
. is used in an object to access its properties, methods, events, etc.