Skip to main content

Lesson 1: Hello, World!

Welcome to Ada!

Traditionally, the first program written in a new language is "Hello, World." This is a simple program just to print a greeting, and help ensure your environment is set up correctly.

Requirements

  1. Alire - This program provide simple commands to build and run your code, and will install the programs behind this process. The detailed controls are still in the background if you need them, but it helps streamline the process for developers. You will often find just the main Alire interface will work well enough for many projects.

    If you haven't set it up, you can follow the instructions on the Alire site.

  2. A text editor such as Visual Studio Code or Notepad++.

Objectives

  1. Make your first running Ada program!
  2. Learn how to use standard Ada libraries.

Starting a new project

Alire runs from a command-line terminal. This keeps it slim and allows it to be easily usable, such as when making automation to build and test large projects.

note

Commands to type in the terminal will follow a $, don't type the $, that's just there to indicate this is a command. Commands in tutorials might also have another block that will show you their output.

Alire will generate you a new project. You want to start, or initialize (init), a new project which runs an executable, which is also called a binary (bin).

$ alr init --bin my_hello_world

You should respond with something like this:

Success: my_hello_world initialized successfully.

This made a new directory for your project, my_hello_world/ with some contents:

my_hello_world/
├── alire.toml
├── my_hello_world.gpr
├── share
│ └── my_hello_world
└── src
└── my_hello_world.adb

There's a few files and a few directories that it made.

alire.tomlDescribes what is in your project.
my_hello_world.gpr

A GNAT project file. This is a more detailed file describing how to build your code into a running program or a library of code for others to use.

my_hello_world.adb

The file with the code executed when your program starts. "adb" is the suffix for the "bodies" of Ada files. "adb" files tell the details of what to do, the other type of file, an "ads" file by convention, describes how to use what is in an "adb" file.

src/A directory to place additional code your program needs.
share/Used for additional things by Alire.

Building a project

Let's try to build the project:

$ cd my_hello_world
$ alr build

You should see something like:

Note: Synchronizing workspace...
Nothing to update.

Note: Building my_hello_world/my_hello_world.gpr...
Setup
[mkdir] object directory for project My_Hello_World
[mkdir] exec directory for project My_Hello_World
Compile
[Ada] my_hello_world.adb
Bind
[gprbind] my_hello_world.bexch
[Ada] my_hello_world.ali
Link
[link] my_hello_world.adb
Build finished successfully in 1.51 seconds.

Let's try to run the program to see what happens:

$ alr run

It prints some things, but it's not very interesting. It doesn't look like it did anything.

Note: Building my_hello_world/my_hello_world.gpr...
gprbuild: "my_hello_world.exe" up to date
Build finished successfully in 0.70 seconds.

It's just an empty program...

Open up my_hello_world.adb and have a look.

procedure My_Hello_World is
begin
null;
end My_Hello_World;

In most programming languages, the point where the program begins (the entry point) is called "main" but in Ada it can have other names. This is something configured in the my_hello_world.gpr file made when Alire created the project.

This is just an empty program. Broken down, it here's what the various words mean:

procedure - What follows is a block of code that can be run by giving its name.

My_Hello_World - This "identifies" the procedure uniquely in this part of the program. Since it provides identification, it's called an "identifier."

is ... begin - Between these two words is the section of code is where additional variables and constants would go. There' no variables or constants used here, so it's just empty.

begin ... end - Instructions called "statements" go between these words to tell the program to do things. They are executed one at a time, and each one is separated by semicolon ;.

null; - The section between begin and end cannot be empty, so it MUST have a statement. The "null statement" does nothing.

end My_Hello_World; - Indicates the end of the program. In Ada, ends can be annotated the name of the thing which started the block being terminated. In this case, it's the procedure My_Hello_World, so that's the name used here.

Making it do something

There's an empty program, with absolutely nothing in it. How do you make it do something?

The start of every Ada file is what is called the "context clause." This is where dependencies on other pieces of code get put. In Ada, there are three packages provided as part of the language, though with Alire you can easily bring in additional code from other libraries, but these are the big three:

AdaData structures, basic facilities to read/write files, etc.
Standard

The built-in library of extremely basic things to build upon. These are often things the language must handle specially that can't be built out of other things.

InterfacesComponents to talk to other languages, like C.

These "packages" of code can be nested. You want to print things, which is in the Text_IO package, which resides within the Ada package. A period (full stop) between names indicates that the thing on the right is contained within the thing on the left.

You want to use procedure to print text to the console, so you bring in Ada.Text_IO as a dependency:

with Ada.Text_IO;

procedure My_Hello_World is
begin
null;
end My_Hello_World;

There's something to do now, so replace that null statement with a greeting to the world. Put_Line is a procedure within the Ada.Text_IO package, so place a period after that name to indicate that's where the procedure is located. Put_Line accepts an input, so use parentheses ( and ) around your greeting of "Hello, World!".

with Ada.Text_IO;

procedure My_Hello_World is
begin
Ada.Text_IO.Put_Line ("Hello, World!");
end My_Hello_World;

Let's run the program again. You don't need to do alr build every time, if you've change your program code, you can use alr run and it will build your code if it changed automatically before running your program.

$ alr run
Note: Building my_hello_world/my_hello_world.gpr...
Compile
[Ada] my_hello_world.adb
Bind
[gprbind] my_hello_world.bexch
[Ada] my_hello_world.ali
Link
[link] my_hello_world.adb
Build finished successfully in 1.03 seconds.
Hello, World!

And there's your greeting to the world!

Need help?

Go to the Ada language gitter if you have questions or need help.