Title here
Summary here
In Ada, variables are explicitly declared and used by the compiler to ensure type-correctness of operations and function calls.
with Ada.Text_IO; use Ada.Text_IO;
procedure Variables is
-- Declare a variable
A : String := "initial";
begin
-- Print the value of A
Put_Line(A);
-- Declare multiple variables at once
B, C : Integer := 1, 2;
Put_Line(Integer'Image(B) & " " & Integer'Image(C));
-- Ada will infer the type of initialized variables
D : constant Boolean := True;
Put_Line(Boolean'Image(D));
-- Variables declared without initialization are default-initialized
-- For integers, the default value is typically 0
E : Integer;
Put_Line(Integer'Image(E));
-- Ada doesn't have a shorthand declaration and initialization syntax like Go's :=
-- Instead, we use the full declaration with initialization
F : constant String := "apple";
Put_Line(F);
end Variables;
To run this Ada program:
$ gnatmake variables.adb
$ ./variables
initial
1 2
TRUE
0
apple
In Ada:
name : type := value;
syntax.constant
keyword is used for variables that won’t change after initialization.:=
.Type'Image(variable)
.&
operator.Ada’s strong typing system and explicit declarations help catch errors at compile-time and make the code’s intent clearer.