Notice: Undefined offset: 3 in /home/acad/acain/www/htdocs/mediawiki/extensions/CodeSyntaxHighlight/geshi.php on line 2049

Notice: Undefined offset: 0 in /home/acad/acain/www/htdocs/mediawiki/extensions/CodeSyntaxHighlight/geshi.php on line 2086

Notice: Undefined offset: 0 in /home/acad/acain/www/htdocs/mediawiki/extensions/CodeSyntaxHighlight/geshi.php on line 2086
Writing a simple pascal program - SwinBrain

Writing a simple pascal program

From SwinBrain

In this article we will look at writing a simple program in Pascal. We will look at some basic tools that you will need to use in order to create these basic programs.

This is a How To article designed to give step-by-step instructions. Search SwinBrain and the external links if you require more detailed information about this topic.

Hello World

The first program that you should write when you are learning a new language or tool has a special name... "Hello World". The idea of this program is to display a simple Hello World message from your program to ensure that you can get everything working together. You will be amazed at how many different technologies you can use to write such a simple program. In this case we will create a small console program using the Pascal programming language.

The first thing that you need to do is to install the Pascal compiler. The Installing Free Pascal article goes through the details of this process. Basically you need to download and install the Free Pascal Compiler for your platform.

Once you have the compiler installed you can start writing programs. Programs are written as Source Code, which is basically a text file. You can use any text editor you want to create these files, but some are better then others. To make your life a little easier you want to get a text editor that has Syntax Highlighting, a syntax highlighting editor understand the programming language that you are using and highlights the code as you type.

  • If you are using Windows you can download and install the Crimson Editor, which is a small syntax aware text editor.
  • If you are using MacOS you can use Smultron or a number of other text editors.

With all the tools in place, lets get started writing our first program.

  1. Open your text editor
  2. Create a new file called HelloWorld.pas
  3. Enter the following text, then save the file.
program HelloWorld;
begin
    WriteLn('Hello World');
end.

At this point we have created the source code for the program. We now need to convert this into an executable file, one the computer can execute. To do this we are going to need to use the command prompt. You can access the command prompt using the following...

  • In Windows you have the following options:
    • From the Start menu select All Programs, Accessories, then Command Prompt
    • From Crimson Editor Tools menu select MS-DOS Shell or press F10
  • In MacOS you can access the Terminal from Applications, Utilities

The following steps outline the process of compiling the Pascal program.

  1. Open a command prompt
  2. Navigate to the location of your file
  3. Execute the free Pascal compiler by calling fpc HelloWorld.pas
  4. Run the program by calling HelloWorld

The following is the output of running this from Windows. The location of the Pascal file in this example was C:\temp.

Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.
 
C:Program FilesCrimson Editortemplate>cd c:temp
 
C:temp>fpc HelloWorld.pas
Free Pascal Compiler version 2.0.4 [2006/08/21] for i386
Copyright (c) 1993-2006 by Florian Klaempfl
Target OS: Win32 for i386
Compiling HelloWorld.pas
Linking HelloWorld.exe
6 Lines compiled, 0.1 sec
 
C:temp>HelloWorld
Hello World
 
C:temp>

The following illustrates this same process on MacOS. In this case the source file is located at ~/temp.

Last login: Thu Feb  1 12:04:37 on ttyp2
Welcome to Darwin!
macpro:~ acain$ cd ~/temp
macpro:~/temp acain$ fpc HelloWorld.pas 
Free Pascal Compiler version 2.0.4 [2006/08/21] for powerpc
Copyright (c) 1993-2006 by Florian Klaempfl
Target OS: Darwin for PowerPC
Compiling HelloWorld.pas
Assembling helloworld
Linking HelloWorld
3 Lines compiled, 0.2 sec
macpro:~/temp acain$ ./HelloWorld 
Hello World
macpro:~/temp acain$

Simple Input

Now lets create another program that takes some user input...

  1. Return to your text editor
  2. Create a new file called SayHello.pas
  3. Enter the program text that follows these instructions
  4. Save the file
  5. Compile at the command line
  6. Execute and enter your name
program SayHello;
var
    name: String;
begin
    Write('Please enter your name: ');
    ReadLn(name);
    WriteLn('Hello ', name);
end.

Conclusion

In this article we have looked at creating simple Pascal programs that are able to read and write from the console. We have not really explored much of the Pascal language, but we have seen the process involved in creating programs.