Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

Hello World and the Structure of an Ada Program

Write your first Ada program and learn its structure: context clauses with and use, the declarative part, begin and end, comments, identifiers and the layout rules.

Every Ada program is built from the same few parts: a context clause that names the packages it needs, a declarative part that introduces names, and a sequence of statements between begin and end. Once you can read this skeleton, you can read any Ada program.

This lesson walks through Hello World line by line, then shows the general shape of a main procedure, how comments and identifiers work, and how to keep a C stand-in beside it so you can run the idea. Remember that Ada listings here are read-only (see What Is Ada?).

Hello World, line by line

ADA
with Ada.Text_IO;

procedure Hello is
begin
   Ada.Text_IO.Put_Line ("Hello, world!");
end Hello;
  • with Ada.Text_IO; makes the package Ada.Text_IO visible to the compiler. Without it, the name is unknown.
  • procedure Hello is starts a subprogram named Hello. A procedure with no parameters can serve as the main program; the build tool decides which one by name.
  • begin ... end Hello; encloses the statements. Repeating the name after end is optional but strongly recommended: the compiler checks it.
  • Ada.Text_IO.Put_Line is a procedure from that package. The string is in double quotes, and statements end with a semicolon.

Case does not matter: Put_Line, put_line and PUT_LINE are the same identifier. The convention is Mixed_Case_With_Underscores.

The use clause

Writing Ada.Text_IO. before every call is exact but noisy. A use clause lets you drop the prefix:

ADA
with Ada.Text_IO; use Ada.Text_IO;

procedure Hello is
begin
   Put_Line ("Hello, world!");
end Hello;

Many teams prefer use type (to make operators visible) or short renames over a blanket use, because a plain use can hide where a name comes from. For small programs, use Ada.Text_IO; is common.

The general shape

A subprogram body has this layout:

ADA
with Ada.Text_IO; use Ada.Text_IO;

procedure Layout is
   --  declarative part: names introduced here exist only in this procedure
   Greeting : constant String := "Welcome";
   Count    : Integer := 3;
begin
   --  statements
   for I in 1 .. Count loop
      Put_Line (Greeting);
   end loop;
end Layout;
flowchart TD
    A["with clauses (imports)"] --> B["procedure Name is"]
    B --> C["declarations<br/>(constants, variables, types, nested subprograms)"]
    C --> D["begin"]
    D --> E["statements"]
    E --> F["exception handlers (optional)"]
    F --> G["end Name;"]

Declarations come before begin; you cannot declare variables in the middle of statements (use a declare block for that).

Notice there is no main function, no return 0, and no header file. The compilation unit is the procedure itself.

Comments, identifiers and literals

Comments begin with two hyphens and run to the end of the line. There are no block comments.

ADA
--  This whole line is a comment
X : Integer := 5;  --  and this is a trailing comment

Identifiers start with a letter, then letters, digits and single underscores. They cannot end with an underscore or contain two in a row. Numbers may use underscores for readability, and bases are written with #:

ADA
Million : constant := 1_000_000;
Mask    : constant := 2#1111_0000#;   --  binary, 240
Hex     : constant := 16#FF#;         --  255

Character literals use single quotes ('A') and strings use double quotes. To put a double quote inside a string you double it: "She said ""hi""".

Reserved words and style

Ada has about seventy reserved words, such as begin, end, is, loop, if, then, else, elsif, case, when, type, record, package, procedure, function, access and others. You cannot use them as names. The community style (the GNAT style) uses three-space indentation, reserved words in lowercase and identifiers in Mixed_Case.

Stand-in: the same program shape in C

C has a different skeleton, but you can see the same pieces: imports, declarations, statements. This labelled C stand-in prints a greeting three times, mirroring the Layout procedure above, and shows underscore-grouped numbers by computing them from their parts.

C Stand-in: the Layout procedure in C

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C has no 2#...# literals (C23 added 0b), but the numbers are the same.

Here is a second stand-in. In Ada, a typo in the closing label is a compile error. This C program simulates a tiny checker that compares the name after procedure with the name after end, which is exactly what the Ada compiler does.

C Stand-in: checking the end label

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Common mistakes

  • Forgetting the semicolon after end Hello (it is required).
  • Declaring a variable after begin instead of before it.
  • Using = for assignment: Ada assigns with := and compares with =.
  • Using Put_Line without with Ada.Text_IO;: the compiler says the name is undefined.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a with clause do?

    with names the library units the current unit depends on; use then optionally removes the need for prefixes.
  2. 2. Where must variables be declared in a procedure?

    Declarations come between is and begin; for a local scope in the middle, use a declare block.
  3. 3. Which of these begins a comment in Ada?

    Ada has only line comments, introduced by two hyphens.
  4. 4. Are Put_Line and PUT_LINE the same identifier?

    Ada identifiers are case-insensitive; the style guide just standardizes how you capitalize them.

Summary

  • An Ada main program is a parameterless procedure with a context clause, declarations, begin, statements and end Name;.
  • with imports a package; use lets you omit the package prefix.
  • Comments start with --; identifiers are case-insensitive; literals can use underscores and base#digits#.
  • The compiler verifies that closing names match opening names.

Next: GNAT, gprbuild and the Ada Toolchain