Ada Hello World
Introduction to Ada
Ada is a high-level, statically typed, object-oriented programming language. It was designed by the U.S Department of Defense in the late 1970s as a general-purpose programming language for safety-critical and real-time systems. Ada has a strong focus on reliability, maintainability, and software engineering principles.
History of Ada
The development of Ada started in 1975 when the U.S Department of Defense realized the need for a standard programming language that could be used across their various projects. The language was named after Ada Lovelace, a mathematician who is considered the world's first programmer.
Ada was first standardized in 1983 as Ada 83, followed by Ada 95 in 1995, Ada 2005 in 2005, and the latest version, Ada 2012, in 2012. Each new version introduced new features and improvements to the language.
Features of Ada
Ada offers a wide range of features that make it suitable for developing complex and reliable software systems. Some of its key features include:
Strong Typing: Ada is a statically typed language, which means that type errors are caught at compile-time rather than runtime. This helps in detecting and preventing a wide range of programming errors.
Safety and Reliability: Ada has built-in features for writing safe and reliable code. It supports strong typing, exception handling, and runtime checks to ensure program correctness.
Concurrency: Ada provides powerful concurrency features, allowing developers to write parallel and distributed programs. It supports tasking, protected types, and rendezvous mechanisms for synchronization.
Modularity: Ada promotes modular programming by supporting packages. Packages encapsulate related data and procedures, making code organization easier and promoting code reuse.
Object-Oriented Programming: Ada supports object-oriented programming (OOP) concepts such as encapsulation, inheritance, and polymorphism. It allows developers to define classes, abstract types, and generic units.
Real-Time Systems: Ada was designed to handle real-time systems effectively. It provides features like tasking, priority-based scheduling, and interrupt handling for developing real-time applications.
Portability: Ada programs are highly portable across different platforms and architectures. The language specification defines a standard set of features and libraries, ensuring consistent behavior across implementations.
Hello World in Ada
Let's start with a simple "Hello, World!" example in Ada:
with Ada.Text_IO; -- Import the Text_IO package
procedure Hello is
begin
Ada.Text_IO.Put_Line("Hello, World!"); -- Print the message to the console
end Hello;
In this example, we import the Ada.Text_IO package, which provides input/output operations. We then define a procedure named Hello and use the Put_Line procedure from Ada.Text_IO to print the "Hello, World!" message to the console. Finally, we end the procedure.
To compile and run the Ada program, you can use a compiler such as GNAT. Here's how you can compile and run the program using the GNAT compiler:
$ gnatmake hello.adb
$ ./hello
Hello, World!
Ada Programming Examples
More Ada Examples.
Example 1: Hello, World!
-- Hello, World! program in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello is
begin
Put_Line("Hello, World!");
end Hello;
Explanation:
- In this example, we use the
Ada.Text_IOpackage for input/output operations. - The
withstatement makes theAda.Text_IOpackage available for use. - The
usestatement allows us to directly use the entities from theAda.Text_IOpackage without explicitly qualifying them. procedureis a subprogram that performs a specific task. In this case, it is namedHello.Put_Lineis a procedure from theAda.Text_IOpackage that prints a line of text to the console.- The line
"Hello, World!"is printed to the console using thePut_Lineprocedure.
Expected Output:
Hello, World!
Example 2: Basic Arithmetic
-- Basic arithmetic operations in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure Arithmetic is
A, B, Result : Integer;
begin
A := 10;
B := 5;
Result := A + B;
Put("Addition: ");
Put(Result);
New_Line;
Result := A - B;
Put("Subtraction: ");
Put(Result);
New_Line;
Result := A * B;
Put("Multiplication: ");
Put(Result);
New_Line;
Result := A / B;
Put("Division: ");
Put(Result);
New_Line;
end Arithmetic;
Explanation:
- In this example, we perform basic arithmetic operations using variables in Ada.
- We use the
Ada.Text_IOpackage for input/output operations. A,B, andResultare integer variables that store values.- We assign values to
AandBusing the assignment operator:=. - We perform addition, subtraction, multiplication, and division operations using the variables.
- The
Putprocedure is used to print the result of each operation to the console. - The
New_Lineprocedure is used to move the cursor to the next line.
Expected Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0000000
Example 3: If-Else Statement
-- If-Else statement in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure IfElseExample is
Number : Integer;
begin
Put("Enter a number: ");
Get(Number);
if Number > 0 then
Put("Number is positive.");
else
Put("Number is non-positive.");
end if;
New_Line;
end IfElseExample;
Explanation:
- In this example, we demonstrate the use of the
if-elsestatement in Ada. - We use the
Ada.Text_IOpackage for input/output operations. Numberis an integer variable that stores the user input.- The
Putprocedure is used to prompt the user to enter a number. - The
Getprocedure is used to read the user input and store it in theNumbervariable. - The
if-elsestatement checks if theNumberis greater than 0. - If the condition is true, it prints "Number is positive." Otherwise, it prints "Number is non-positive."
- The
end ifstatement marks the end of the conditional block.
Expected Output:
Enter a number: 7
Number is positive.
Enter a number: -3
Number is non-positive.
Example 4: For Loop
-- For loop in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure ForLoopExample is
begin
for I in 1..5 loop
Put(I);
Put(" ");
end loop;
New_Line;
end ForLoopExample;
Explanation:
- In this example, we demonstrate the use of the
forloop in Ada. - We use the
Ada.Text_IOpackage for input/output operations. - The
forloop iterates over a range of values defined by1..5. - In each iteration, the loop variable
Itakes the value from the range. - The
Putprocedure is used to print the value ofIfollowed by a space. - The loop continues until it reaches the end of the range.
- The
end loopstatement marks the end of the loop.
Expected Output:
1 2 3 4 5
Example 5: While Loop
-- While loop in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure WhileLoopExample is
Count : Integer := 1;
begin
while Count <= 5 loop
Put(Count);
Put(" ");
Count := Count + 1;
end loop;
New_Line;
end WhileLoopExample;
Explanation:
- In this example, we demonstrate the use of the
whileloop in Ada. - We use the
Ada.Text_IOpackage for input/output operations. Countis an integer variable initialized to 1.- The
whileloop continues as long as the conditionCount <= 5is true. - In each iteration, the value of
Countis printed followed by a space. - The
Countvariable is incremented by 1 in each iteration using the assignment operator:=. - The
end loopstatement marks the end of the loop.
Expected Output:
1 2 3 4 5
Example 6: Arrays
-- Arrays in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure ArrayExample is
Numbers : array(1..5) of Integer := (10, 20, 30, 40, 50);
begin
for I in 1..5 loop
Put(Numbers(I));
Put(" ");
end loop;
New_Line;
end ArrayExample;
Explanation:
- In this example, we demonstrate the use of arrays in Ada.
- We use the
Ada.Text_IOpackage for input/output operations. Numbersis an array of integers with a range from 1 to 5.- The array is initialized with values
(10, 20, 30, 40, 50)using the assignment operator:=. - The
forloop iterates over the range of the array. - In each iteration, the value at the current index
Iis printed followed by a space.
Expected Output:
10 20 30 40 50
Example 7: Subprograms
-- Subprograms in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure SubprogramExample is
procedure PrintMessage is
begin
Put_Line("Hello from subprogram!");
end PrintMessage;
begin
PrintMessage;
end SubprogramExample;
Explanation:
- In this example, we demonstrate the use of subprograms (procedures) in Ada.
- We use the
Ada.Text_IOpackage for input/output operations. PrintMessageis a subprogram (procedure) defined within theSubprogramExampleprocedure.- The
PrintMessageprocedure prints the message "Hello from subprogram!" to the console usingPut_Line. - The
PrintMessageprocedure is called within theSubprogramExampleprocedure.
Expected Output:
Hello from subprogram!
Example 8: Records
-- Records in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure RecordExample is
type Person is record
Name : String(1..20);
Age : Integer;
end record;
P : Person;
begin
P.Name := "John Doe";
P.Age := 30;
Put("Name: ");
Put(P.Name);
New_Line;
Put("Age: ");
Put(P.Age);
New_Line;
end RecordExample;
Explanation:
- In this example, we demonstrate the use of records (structures) in Ada.
- We use the
Ada.Text_IOpackage for input/output operations. Personis a record type that consists of two fields:Name(a string) andAge(an integer).Pis a variable of typePerson.- We assign values to the
NameandAgefields of thePvariable using the dot notation (P.NameandP.Age). - The
Putprocedure is used to print the values of the fields to the console. - The
New_Lineprocedure is used to move the cursor to the next line.
Expected Output:
Name: John Doe
Age: 30
Example 9: Packages
-- Packages in Ada
with Ada.Text_IO;
use Ada.Text_IO;
package HelloWorld is
procedure SayHello;
end HelloWorld;
package body HelloWorld is
procedure SayHello is
begin
Put_Line("Hello from package!");
end SayHello;
end HelloWorld;
with HelloWorld;
procedure PackageExample is
begin
HelloWorld.SayHello;
end PackageExample;
Explanation:
- In this example, we demonstrate the use of packages in Ada.
- We use the
Ada.Text_IOpackage for input/output operations. HelloWorldis a package specification that declares a subprogramSayHello.HelloWorldis also a package body that defines the implementation of theSayHellosubprogram.- The
SayHellosubprogram prints the message "Hello from package!" to the console usingPut_Line. - The
SayHellosubprogram is called within thePackageExampleprocedure.
Expected Output:
Hello from package!
Example 10: Exception Handling
-- Exception handling in Ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure ExceptionExample is
Result : Integer;
begin
Put("Enter a number: ");
Get(Result);
begin
if Result = 0 then
raise Constraint_Error;
else
Result := 10 / Result;
end if;
exception
when Constraint_Error =>
Put_Line("Cannot divide by zero!");
end;
Put("Result: ");
Put(Result);
New_Line;
end ExceptionExample;
Explanation:
- In this example, we demonstrate exception handling in Ada.
- We use the
Ada.Text_IOpackage for input/output operations. Resultis an integer variable that stores the user input.- The
Putprocedure is used to prompt the user to enter a number. - The
Getprocedure is used to read the user input and store it in theResultvariable. - We use a nested
begin-endblock to handle exceptions. - If the user enters 0, the
Constraint_Errorexception is raised. - If the user enters a non-zero value, the division operation
10 / Resultis performed. - If no exception is raised, the result is printed to the console.
- The
when Constraint_Errorclause specifies the exception to be handled. - The
Put_Lineprocedure is used to print the exception message "Cannot divide by zero!"
Expected Output:
Enter a number: 0
Cannot divide by zero!
Enter a number: 5
Result: 2
These were 10 simple examples in the Ada programming language. Each example demonstrates a different concept or feature of the language, ranging from basic syntax to more advanced topics like exception handling. Experiment with these examples to get a better understanding of the Ada programming language.
Comparison with Alternatives
Ada offers several advantages over other programming languages, especially for safety-critical and real-time systems. Here are a few points of comparison:
C/C++: Unlike C/C++, Ada provides strong typing, runtime checks, and exception handling by default, reducing the chances of bugs and errors. The language also offers built-in support for concurrency and real-time systems, which can be challenging in C/C++.
Java: While Java is also a statically typed, object-oriented language, Ada has a stronger focus on safety and reliability. Ada's support for concurrency and real-time systems is more extensive than Java, making it a better choice for developing high-integrity systems.
Python: Python is a dynamically typed language, which means that type errors are detected at runtime. In contrast, Ada's static typing helps catch errors early during the compilation process. Additionally, Ada's performance is generally better than Python, making it suitable for resource-constrained environments.
Official Resources
To learn more about Ada, you can visit the official Ada website: https://www.adaic.org/. The website provides comprehensive documentation, tutorials, and resources for getting started with Ada development.