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:

Request for Pascal in Hello World Collection

Table of Contents

Pascal Background

According to WikipediaOpens in a new tab., 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 compilerOpens in a new tab.. We just need to drop the snippets into the online editor and hit run.

Of course, we can always download and install PascalOpens in a new tab. locally. While we’re downloading stuff, we should probably get a copy of the solutionOpens in a new tab..

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!

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts