Friday, March 18, 2011

Hello World!


Je n'ai fait celle-ci plus longue que parce que je n'ai pas eu le loisir de la faire plus courte. (I would have written a shorter letter, but I did not have the time.)
Blaise Pascal, Provincial Letters: Letter XVI


It is typical in computer programming courses to begin with the simple example of a program that prints "Hello World!". It is a good enough place to start for our purposes. You can use either the IDE or your favourite text editor for this simple program and save the file as hello.pas. It is recommended that the programs be kept in a sensible place (e.g., ~/programming/pascal) for easy and consistent access.

The following is the content of the program:


program HelloWorld;
begin
writeln('Hello World!');
end.


The first line provides a title for the program. The second is a block for the program's actions, starting with a begin statement and concluding with an end statement. The content of the block consists of a single statement, to write a line that outputs 'Hello World'. Note that the brackets for the writeln statement which enclose the text field and the semi-colon to mark the end of the statement.

At first the program is written in source code, a high-level language comprehensible to humans. This needs to be converted to a machine language which the computer can comprehend. This process of conversion is one of compiling the source code into object code, which then is passed to a linker, which combines one or more object files into an executable binary. The following process is illustrative:


lev@squirrel:~/programming/pascal$ fpc hello.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling hello.pas
Linking hello
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
6 lines compiled, 0.8 sec
lev@squirrel:~/programming/pascal$ ls
hello hello.o hello.pas
lev@squirrel:~/programming/pascal$ ./hello
Hello World


The command fpc is the Free Pascal Compiler. As the output explains the compilation creates an object file and then an executable. The error message is benign, as explained by the Free Pascal knowledge base. A directory listing shows the three files. When executed the hello binary produces the output "Hello World!".

No comments: