Table of contents
3333Ada is a very verbose language, meaning it uses a lot of explicit, declarative syntax. This includes:
Long identifiers: variable and function names can be very long and descriptive. This improves readability but makes the code wordy.
Explicit typing: Ada requires you to specify the type of all variables, parameters, functions, etc. There are no type inferences.
Abundant keywords: Ada has many keywords for control structures, packages, exceptions, etc. This makes the syntax explicit and self-documenting.
Ada is an original language created in the early 1980s, quite different from languages like Fortran, C, Java or Python.
Ada does not belong to any particular language family but it has been influenced by languages like Pascal and ALGOL.
Ada is a free-form language, meaning code is not restricted to fixed column positions and indentation is not syntactically significant.
Ada is a case-insensitive language, so variable names like Count and COUNT refer to the same variable.
Some other language conventions:
Use camelCase for variable names, UPPER_CASE for constants.
Begin blocks with the keywords begin and end.
Use double quotes for string literals.
Use pragma to specify compiler options.
Compile Ada programs using GNAT or other Ada compilers.
So in summary, Ada's syntax is very explicit and wordy, making Ada code self-documenting but somewhat verbose. The language has its own unique conventions that differ from more modern languages.
Project Files
An Ada program consists of one or more compilation units called packages. The main components of an Ada program are:
Specification: Contains the interface of the package - definitions of types, constants, subprograms etc. The specification has a .ads extension.
Body: Contains the implementation of the subprograms declared in the specification. The body has a .adb extension.
Project File: Contains information to compile all the packages that make up the program. It has a .gpr extension.
Main Program: Contains the main subprogram that gets executed first. It has a .adb extension.
So a typical Ada project would have:
Package1.adb
Package2.adb
main.adb
project.gpr
The .ads and .adb files contain the specification and body of the packages. The main.adb contains the main subprogram that acts as the entry point. And the project.gpr file ties everything together for compilation.
The main components of an Ada program are packages - which contain specification and body. Packages allow modularization and reusability. The main subprogram acts as the entry point for execution.
The .ads, .adb and .gpr extensions are specific to Ada to identify the compilation units and project file.
So in summary, packages (.ads and .adb files) are the main building blocks of an Ada program. The project file ties everything together and the main subprogram acts as the entry point for execution.
Main File
A main file in Ada contains the main subprogram, which acts as the entry point for the execution of the program. A simple "Hello World" program in Ada would be:
main.adb:
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
begin
Put_Line("Hello World!");
end Main;
This program does the following:
We import the Ada.Text_IO package using
with
and use it usinguse
. This provides access to text I/O functions likePut_Line
.We declare a procedure named
Main
. All Ada programs have a main procedure as the entry point.Within the
Main
procedure, we call thePut_Line
function to print "Hello World!" to the standard output.The
end Main;
ends theMain
procedure.
To compile and run this program, you can use:
gnatmake main
./main
This will compile the program using the GNAT compiler and run the executable named main
which prints "Hello World!".
So in summary, the main file in an Ada program:
Contains the
main
subprogram, which acts as the entry point.Imports any necessary packages using
with
and uses them usinguse
.Contains the executable code of the program.
Is compiled separately and linked to generate the final executable.
The main subprogram is where the program execution starts, so it is an essential part of any Ada program. Unfortunately the color syntax do not work on HashNode. So you do not have higlighted code examples. For larger code examples I will try to find a better solution to share code.
Comments
Comments in Ada code start with -- and extend to the end of the line. For example:
-- This is a single-line comment
x := 10; -- Assigning 10 to x
y := 20; -- Assigning 20 to y
Ada also supports multiline comments using the syntax:
--{
This is
a multiline
comment
--}
For example:
--{
This is a
multiline
comment
--}
z := x + y; -- Adding x and y
Comments in Ada serve the following purposes:
Document the code to make it self-explanatory
Explain the purpose of variables, functions, etc.
Disable chunks of code temporarily during debugging
Leave notes for future maintenance
Ada encourages the use of descriptive identifiers and syntax, so comments are used more to complement that rather than to make the code self-explanatory.
Some guidelines for writing comments in Ada:
Use -- for single-line comments and --{ and --} for multiline comments
Start each comment with a space after --
Explain what the code does, not how it works
Avoid redundant comments
Update comments whenever the code changes
Note: Comments in Ada use "--" therefore "--" is not an operator like in C++, Java or other languages. Also Ada is not a curly brakets language. The block of code is indented but indentation is optional. THe blocks of code are ending with keyword: "end".
Statement
A statement in Ada is a line of code that performs an action. Some examples of statements in Ada are:
- Assignment statements:
x := 5;
y := x + 1;
- Procedure call statements:
Put(Item => x);
Put_Line(Item => y);
- If statements:
if x > y then
z := x;
else
z := y;
end if;
- Case statements:
case Size is
when 1 => Put_Line("Small");
when 2 => Put_Line("Medium");
when 3 => Put_Line("Large");
end case;
- Loop statements:
for I in 1..10 loop
Put(I);
end loop;
- Return statements:
return Result;
- Exception handling statements:
raise Constraint_Error;
So in summary, a statement in Ada performs some action like:
Assigning a value
Calling a procedure or function
Controlling the flow of execution (if/case/loop/return/exception handling)
Statements are the basic building blocks of Ada programs. They are separated by semicolons or end of block statement "end" <something>. This hint that semicolons are optional for blocks of code.
Block of Code
The basic syntax for a block of code in Ada is:
begin -- Declarations and statements exception -- Exception handlers
end [block_name];
Where:
begin and end delimit the block.
block_name is an optional name that can be given to the block. This is useful when nesting blocks.
exception is optional and is used to specify exception handlers for the block.
For example:
begin
declare
x : Integer := 0;
begin
x := x + 1;
exception
when others =>
-- Handle exception
end block1;
end block2;
Here we have:
An outer block named block2
An inner block named block1
A declaration of x within block1
An exception handler for the inner block1
So in summary, the basic syntax elements of a block in Ada are:
begin and end keywords
Optional block name
Declarations
Statements
Optional exception handlers
Blocks in Ada can be nested to any level, with each inner block having its own scope. Hope this explanation of the syntax for blocks in Ada was helpful! Let me know if you have any other questions.
Disclaim: This content is generated using Rix. Learn and prosper. ๐