-- Inputs 2 numbers in range 0 .. 999 and prints whether they are reverse of each other
-- Demonstrates declaring and calling a procedure
-- A procedure is equivalent to a method with a void return type (eg void println() )
-- Procedures are used as statements!
pragma Ada_2012; -- Make sure 2012 compiler is used
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure proc1 is
subtype Digit is Natural range 0 .. 9;
subtype Small_Nat is Natural range 0 .. 999;
function ones_dig(n: Small_Nat) return Digit is (n mod 10);
function tens_dig(n: Small_Nat) return Digit is ((n / 10) mod 10);
function hundreds_dig(n: Small_Nat) return Digit is (n / 100);
function eq_rev(n1, n2: Small_Nat) return Boolean is
eq1, eq10, eq100: Boolean;
begin
eq1 := ones_dig(n1) = hundreds_dig(n2);
eq10 := tens_dig(n1) = tens_dig(n2);
eq100 := hundreds_dig(n1) = ones_dig(n2);
return eq1 and eq10 and eq100;
end eq_rev;
procedure check_nums(sm1, sm2: Small_Nat) is
begin
put(sm1'img & " and " & sm2'img & " are");
if eq_rev(sm1, sm2) then -- Function returns a value
put(" THE SAME");
else
put(" are DIFFERENT");
end if;
put_line(" in reverse");
end check_nums;
s1, s2: Small_Nat;
begin
get(s1);
get(s2);
check_nums(s1, s2); -- Call procedure as a statement
exception
when constraint_error => put_line("Value out of range");
when others => put_line("Error occurred");
end proc1;
-- Input: 123 321
-- Output: 123 and 321 are THE SAME in reverse
-- Input: 123 123
-- Output: 123 and 123 are DIFFERENT in reverse