The first thing you should know how to do is to compile a program. Assuming you are using the default key shortcut mappings, all you have to do is press Control+F9. If you press F9 only, the program will both compile and run. Press Control+F9 now. You can use this keyboard shortcut to check the syntax of code you enter without running the program.
Line 1 is a
compiler directive. Compiler directives allow us to change the behavior of the compiler while it is parsing the code. In
Unit I, we will not deal with compiler directives in depth. We use it here only because we must tell the compiler that we are creating a
console application. A
console application is a program that has only a text-based interface.
Line 2 defines the program name. It should always match the file name of the source code. You will notice the use of the
reserved word program and the
identifier HelloWorldConsole followed by the semicolon symbol. The individual symbols such as keywords, identifiers, and semicolons in any programming language are known as
tokens. The compiler reads tokens and assigns predefined meanings to them. Although it may be tempting to think that "program" and "HelloWorldConsole" are both identifiers, the compiler knows a set of "
reserved words" (also known as
keywords) which help define the valid syntax of a language. Object Pascal is case-insensitive which means that "program" and "Program" are both the same reserved word. Reserved words cannot be used as identifiers. You can try to change "program HelloWorldConsole" to "program Program" and press Ctrl+F9. Note also that the syntax higlighter automatically changes the color to the reserved word color instead of the identifier color. The compiler returns an error message that an identifier was expected. Now change this line back to "program HelloWorldConsole".
Line 3 is witespace. Whitespace has no defined meaning in Object Pascal which means that the compiler just ignores it as if the uses clause and the begin line were adjacent. While it may seem trivial to discuss whitespace, some languages (such as Matlab and Python) use parts of whitespace (line breaks and tab characters respectively) to actually mean something in the source code.
Line 4 contains the "begin" reserved word. In Object Pascal, "begin" and "end" define a
compound statement. Compound statements are used to group simple statements. Although compound statements are universal to programming languages, different languages use different symbols (e.g., C uses curly braces
{ } ).
Line 5 is a comment. Comments are used to explain code in plain English as well as to inactivate portions of code since the compiler treats comments as whitespace. Explaining code in comments is known as
documentation and is extremely important for two reasons. First, your code should be readable by others. Second, your code should be readable by you because after a month you will have forgotten everything about it and will not understand your own code without good comments. In Object Pascal, comments can be multi-line or single line. Multi-line comments are defined by curly braces
{ } or parenthesis-asterisks
(* *) . Single line comments are defined by double front-slashes
// and terminate automatically at the end of a line. You should play around with these symbols in the editor to see what happens. By convention, curly brace
{ } and double slash
// comments are generally used for documentation and parenthesis-asterisk
(* *) comments are used to inactivate blocks of code. You may also notice a similarity between curly brace
{ } comments and compiler directives. Compiler directives always have a left curly brace followed by a dollar sign
{$ } which allows the compiler to distinguish them from comments. Like comments, compiler directives are not considered a part of the actual code. The difference is that compiler directives can change the compiler's behavior while comments do nothing.
Line 6 is the Writeln command. In Object Pascal, commands are
calls to either
procedures or
functions. We will discuss the difference between procedures and functions in
Chapter 2. Together, procedures and functions are known as
subroutines.
Subroutines are like mini-programs inside of a larger program which are called to perform some specified action. When we
call a subroutine, the processor's execution shifts to a different location in the code and executes the code at that new location. When the subroutine is done, the processor
returns to the original location in the code and continues executing where it left off.
The Writeln subroutine writes some specified text to the screen. In programming terms, text is known as a
literal string, or a sequence of numbers, letters, or symbols that are meant to be interpreted literally as is instead of being treated as code. You will notice also that
strings in Object Pascal start and stop with a single quote (') and are given a color different than the rest of the code.
As we mentioned above, console apps interact with a user through only a text-based interface. The creation of the console window is left entirely to the operating system (which is why we use {$APPTYPE CONSOLE} to tell the compiler a window will need to be created. In terms of a user interface, console applications are one of the simplest types of applications that can be created. Keep in mind we said "in terms of a user interface" when refering to the simplicity of console applications. Some of the most sophisticated programs used by scientists and engineers have only console interfaces. Programs can also not have an interface at all. These types of programs are called services or daemons (the latter word daemon refers to the invisibility of the program and does not imply malicious intent). Had we wanted to create a program that runs in the background with no interface, we could have simply left out the {$APPTYPE CONSOLE} directive. We of course would not have been able to use Writeln or Readln though. More commonly, programs have what is known as a Graphical User Interface (GUI, pronounced gooey). In the next section, we will create a simple Hello World graphical application.