In this section, we will write our first Ada program: the classic "Hello, World!" This simple program will help you understand the basic structure of an Ada program and how to compile and run it.

Key Concepts

  1. Program Structure: Understanding the basic structure of an Ada program.
  2. Compilation: How to compile an Ada program.
  3. Execution: How to run the compiled program.

Basic Structure of an Ada Program

An Ada program consists of one or more compilation units. The simplest form of an Ada program is a single procedure. Here is the basic structure:

with Ada.Text_IO; use Ada.Text_IO;

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

Explanation

  • with Ada.Text_IO; use Ada.Text_IO;: This line tells the compiler that we are using the Ada.Text_IO package, which provides facilities for input and output. The use clause allows us to use the entities of Ada.Text_IO without prefixing them with the package name.
  • procedure Hello is: This line declares a procedure named Hello. In Ada, a procedure is a subprogram that performs a specific task.
  • begin: This marks the beginning of the executable part of the procedure.
  • Put_Line("Hello, World!");: This line calls the Put_Line procedure from the Ada.Text_IO package to print the string "Hello, World!" to the console.
  • end Hello;: This marks the end of the procedure Hello.

Compiling the Program

To compile the Ada program, follow these steps:

  1. Save the program in a file named hello.adb.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where you saved hello.adb.
  4. Run the following command to compile the program:
gnatmake hello.adb

This command will generate an executable file named hello.

Running the Program

To run the compiled program, use the following command:

./hello

You should see the output:

Hello, World!

Practical Exercise

Exercise 1: Modify the Hello World Program

  1. Modify the program to print "Hello, Ada!" instead of "Hello, World!".
  2. Save the modified program in a file named hello_ada.adb.
  3. Compile and run the modified program.

Solution

Here is the modified program:

with Ada.Text_IO; use Ada.Text_IO;

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

To compile and run the modified program:

gnatmake hello_ada.adb
./hello_ada

You should see the output:

Hello, Ada!

Common Mistakes and Tips

  • Missing with or use Clauses: Ensure you include the with and use clauses for the Ada.Text_IO package.
  • File Naming: The file name should match the procedure name (e.g., hello.adb for procedure Hello).
  • Compilation Errors: If you encounter compilation errors, check for syntax errors such as missing semicolons or incorrect procedure names.

Conclusion

In this section, you learned how to write, compile, and run a simple Ada program. This foundational knowledge will help you as you progress through more complex topics in Ada programming. Next, we will delve into the basic syntax and structure of Ada programs in more detail.

© Copyright 2024. All rights reserved