|
|
| Part 2: Hello World |
|
|
| Written by Blueaura | |
Learning OutcomesIt is tradition to begin the learning of any programming language with a ‘Hello World’ program. We will follow this tradition by creating your first program written in Delphi to display the words ‘Hello World’.
Keywords
IntroductionHopefully by now you have installed a recent version of Delphi and you have also had a quick play around with the IDE. If not you need to go back and have a quick read of Part 1. What we are going to do today is create a simple console (DOS window) application which will display a short string, ‘Hello World’. Strings will be discussed later on, all you need to know for know is that ‘Hello World’ is a string. Console ApplicationLet’s start off by opening up Delphi if you haven’t already and create a new console application. You can do this by navigating to File > New > Other > Delphi Projects > Console Application from the main menu. Now what you should have is a page of code in front of you which looks something like this:
The CodeNow, let’s have a look at the code. The first line declares what you are looking at, a program named Project1. The name of the program can be changed to anything you like as long as it does not contain symbols or spaces and also begins with a letter. For this example HelloWorld will do nicely. To change the whole programs name in this example Right-Click on Project1.exe and select Rename in the Project Manager dialog, if you cannot find this View > Project Manager or Ctrl + Alt + F11 will bring the Project Manager dialog on screen. This is just to ensure the executable program we create is also called HelloWorld. Hint: Try to make the names of programs as descriptive as possible but keep it short. The instruction on line three is encapsulated in curly braces. Anything surrounded by these will be omitted from the actual program we create. The dollar sign at the start of the signals what follows is a compiler instruction telling the compiler it needs to create a console application. Line nine is also ignored and omitted from the final program although this is not a compiler instruction, it is a comment. Developers often use comments to state what the following code does, why it does it, or use it to comment out code for debugging. In this instance it is a To-do memo reminding you to write your code in its place, thanks Borland. Line five states uses followed by SysUtils; on line six. Uses is a keyword in Delphi which tells the compiler that anything following it is required by the program. This allows us to use code contained in other files (called units) in lots of other programs saving us writing it all out again. This also means we can use so very useful code others have written later on. The uses keyword can be followed by one or more files in a list e.g. unit1, unit2, unit3; note the semi-colon at the end of the list. Punctuation in Delphi is very important as you will no doubt find out the hard way. The best approach is to put a semi-colon at the end of any statement although this is not always the case. Just as with English it is something you pick up along the way. Line eight states begin this is the beginning of the actual code which is executed at run time, considering there are no statements in it before code states the program should end. on line ten (Note the punctuation signifying there is no code expected after this point), at the moment nothing happens. Let’s sort that out. Hello WorldSo we want to display the words ‘Hello World’ on screen. This isn’t too hard and the first thing we need to do is remove the comment on line nine. To write a string to the screen we can use the procedure WriteLn()which will write a line to the screen and then move down to the next line. Just type WriteLn() on line nine. Within the brackets type in the message ‘Hello World!’ making sure to include the single quotation marks. Now what do we need at the end of this statement? That’s correct, a semi-colon. So what do we have so far?
Further ReadingCalvert, (1999), Object Pascal Style Guide, BDN |
| < Prev | Next > |
|---|