# ADA Program Examples

A small collection of other simple ADA programs written to learn portions of the language.

## Hello World

`greet.adb`

``````with Ada.Text_IO; use Ada.Text_IO;

procedure Greet is
begin
--  Print "Hello, World!" to the screen
Put_Line ("Hello, World!");
end Greet;``````

Output

``Hello, World!``

## Hello World w/ String and Integer Concatenation

`greet_5a.adb`

``````with Ada.Text_IO; use Ada.Text_IO;

procedure Greet_5a is
begin
for I in 1 .. 5 loop
-- & is string concatenation
-- Integer'Image() is a "attribute"
Put_Line ("Hi" & Integer'Image(I));
end loop;
Put_Line ("...");
for I in reverse 1 .. 5 loop -- see reverse keyword
Put_Line ("Hi" & Integer'Image(I));
end loop;
end Greet_5a;``````

Output

``````Hi 1
Hi 2
Hi 3
Hi 4
Hi 5
...
Hi 5
Hi 4
Hi 3
Hi 2
Hi 1``````

## Get CLI Input, Check if Positive Number

`check_positive.adb`

``````with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Check_Positive is
N: Integer;
begin
Put ("Enter an integer value: ");
Get (N);
if N > 0 then
Put (N);
Put_Line (" is a positive number");
else
Put (N);
Put_Line (" is not positive");
end if;
end Check_Positive;``````

Output

``````Enter an integer value: -2
-2 is not positive``````
``````Enter an integer value: 12
12 is a positive number``````

## Nested Function Definition

`nested_ex.adb`

``````with Ada.Text_IO; use Ada.Text_IO;

procedure Nested_Ex is
procedure Nested is
begin
Put_Line ("I am nested");
end Nested;
begin
Nested; -- call to nested
end Nested_Ex;``````

Output

``I am nested``

## Packages Basics

`show_increment.adb`

``````with Ada.Text_IO; use Ada.Text_IO;
with Increment;

procedure Show_Increment is
A, B, C : Integer;
begin
A := 10;
B := 2;
C := Increment (A, B);
Put_Line ("Value" & Integer'Image (C));
end Show_Increment;``````

`increment.ads`

``function Increment (I : Integer; Incr : Integer) return Integer;``

`increment.adb`

``````function Increment ( I : Integer; Incr : Integer ) return Integer is
begin
return I + Incr;
end Increment;``````

Output

``Value 12``