Skip to content

Delphi Fusion

Narrow screen resolution Wide screen resolution Increase font size Decrease font size Default font size    Default color brown color green color red color blue color
  • Regular Delphi programming articles and tutorials
  • Free members forum supporting everything Delphi
  • Support for all programming abilities
You are here: Home arrow Articles arrow Delphi arrow Part 2: Hello World
Skip to content

User Menu

Login
Register
Logout

Latest Threads

Delphi and C++Builder Roadmap
by: Jon!
@: 15/09/08 01:14 am
Patched KOL for Delphi 2009 (M..
by: azsd
@: 14/09/08 10:26 pm
Delphi 7 Enterprise!
by: uuf6429
@: 17/06/08 12:33 pm

Delphi Fusion

Links
Sponsors
Amazon

Donate

Enter Amount:


Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285

Warning: Division by zero in /usr/local/psa/home/vhosts/delphifusion.com/httpdocs/mambots/content/geshibot/geshi.class.php on line 2285
Part 2: Hello World Print E-mail
Written by Blueaura   

Learning Outcomes

It 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

  • Compiler
    A program that translates a high-level language, such as Delphi, into machine language.

Introduction

Hopefully 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 Application

Let’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:

New Console Application
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. begin
  9. { TODO -oUser -cConsole Main : Insert code here }
  10. end.
 
All of this is Delphi programming code, something you will soon become very familiar with. You can run the program as it is now by hitting F9 or navigating the main menu to Run > Run. Not much will happen but it is always nice to know your program does not contain any errors to stop it from compiling and running at this stage. At any point feel free to attempt to run the program it is an important debugging tool and will get you up to speed on the various errors, how to solve them, and how Delphi helps you debug.

The Code

Now, 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 World

So 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?

Hello World
  1. program HelloWorld;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. begin
  9. WriteLn('Hello World!');
  10. end.
 
Run the program by hitting F9. If your eyesight is good you might be able to make out the message which then disappears instantly. To solve this issue we just need to add a couple more lines of code so move down to a fresh line and type in WriteLn(‘Press return to quit.’); and then down to another new line and type ReadLn;. All this is doing is instructing the user to press the Return key to exit the program which is captured by the ReadLn procedure. Once return is pressed the code continues to be executed sequentially, because there is only there are no more statements the program ends.

Hello World
  1. program HelloWorld;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. begin
  9. WriteLn('Hello World!');
  10. Writeln('Press retun to quit.');
  11. ReadLn;
  12. end.
 
Congratulations you are now a Delphi developer – you have written your first program. It doesn’t do much but believe it or not the Hello World program is where most programmers started.

Further Reading

Calvert, (1999), Object Pascal Style Guide, BDN

There are no comments for this item.
Please keep your comments brief and on topic, and remember that this is not a discussion thread.
Name : E-mail :
Title : Website :
Comment(s) :
J! Reactions Commenting Software
General Site License
Copyright © 2006 S. A. DeCaro
 
< Prev   Next >