Hello World in Pascal

Hello World in Pascal Featured Image

Welcome back to another installment of Hello World in Every Language. Today, we’re going to look at Hello World in Pascal, a language recommended to me by Gayane Muradyan.

Table of Contents

Pascal Background

According to Wikipedia, Pascal is an imperative and procedural language which first appeared in 1970. Pascal’s creator, Niklaus Wirth, designed the language with compiler and runtime efficiency in mind. In addition, Wirth drew much of the inspiration for Pascal from the ALGOL family of languages.

That said, Pascal isn’t simply an ALGOL clone. In fact, Pascal includes many additions to ALGOL such as mechanisms for defining custom datatypes. Likewise, the language includes several additional features like enumerations, subranges, and records.

As an added bonus, Pascal is a strongly typed language. This forces the user to explicitly write conversions between types, so errors can be caught at compile time. Unfortunately, I’ve read that Pascal has a loophole in the type system. I just haven’t found any articles describing it. If you know, let me know in the comments.

Before we get into Hello World, I want to look a bit deeper at sets because I find them interesting. In Pascal, we can define a set as follows:

var
  Set1 : set of 5..20;
  Set2 : set of 'f'..'m';

With Pascal being such an old language, I find it interesting how intuitive the set syntax is. In fact, I can’t think of many industrial languages that have such a nice syntax for setting up lists or sets. In fact, here’s how you would generate a list of numbers in a few languages:

// Java 8+
int[] range = IntStream.rangeClosed(5, 20).toArray();
# Python 3
list(range(5, 20))
// JavaScript
var list = [];
for (var i = 5; i <= 20; i++) {
  list.push(i);
}
// C#
int[] values = Enumerable.Range(5, 15).ToArray();

That Pascal set syntax is great. In fact, it even allows us to do fun things like check values in some range:

if i in [5..20] then

At any rate, I think we’ve played around enough. We have to get to our Hello World implementation.

Hello World in Pascal

There’s no time to waste! Let’s dig right into Hello World in Pascal:

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

As usual, we’ll tackle this implementation line by line. Up first, we have the program line which declares the name of the program and a list of file descriptors.

Up next, we have our main block which is denoted by the begin and end keywords. Within this block, we have the print statement. Of course, the function we use in Pascal is called writeln.

Finally, we have a period, also known as a full stop. This indicates the end of the program.

As an added tidbit, semicolons mark the ends of statements, but they are optional on the last statement in a block. In other words, we could have removed the semicolon after the writeln command.

How to Run the Solution

If we want to try any of the Pascal code snippets from this article, we can use an online Pascal compiler. We just need to drop the snippets into the online editor and hit run.

Of course, we can always download and install Pascal locally. While we’re downloading stuff, we should probably get a copy of the solution.

Assuming Pascal is now in our path, we can navigate to our folder containing the solution and run the following commands from the terminal:

fpc hello-world.p
hello-world.exe  # Windows
./hello-world  # Unix/Linux/Mac

Since Pascal is a compiled language, we first need to compile our script. After compilation, we’ll have an executable we can run. When we run it, the “Hello, World!” string should print to the console.

Sample Programs in Every Language

And, that’s it! We’re all finished with Hello World in Pascal. If you liked this article, make sure to give it a share. And of course, feel free to recommend the next language in the comments. Thanks for stopping by!

Series Navigation← Hello World in ElixirHello World in Hack →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.