Ada Programming/Basic - eMyTextBooks

Search:  

Free online books - just read it!

See live article   •   Ada Programming/Basic
 

Ada Programming/Basic


Table of contents


Things to look out for

It will help to be prepared to spot a number of significant features of Ada that are important for learning its syntax and semantics.

comb format

There is a comb format in all the control structures and module structures. See the following examples for the comb format. You don't have to understand what the examples do yet - just look for the similarities in layout.

if statements

if Boolean expression then
   statements
elsif Boolean expression then
   statements
else
   statements
end if;

procedures

procedure P (parameters : in out type) is
   declarations
begin
   statements
exception
   handlers
end P;

functions

function F (parameters : in type) return type is
   declarations
begin
   statements
exception
   handlers
end F;

records

type R (discriminant : type) is
   record
      declarations
   end record;

class records

type C (discriminant : type) is new parent class declaration
                               and interface declarations
with
   record
      declarations
   end record;

package declarations

package P is
   declarations
private
   declarations
end P;

generic declarations

generic
   declarations
package P is
   declarations
private
   declarations
end P;

Note that semicolons consistently terminate statements and declarations; and that the empty string is not a valid statement: the null statement is null;.

Type and subtype

There is an important distinction between type and subtype: a type is given by a set of values and their operations. A subtype is given by a type, and a constraint that limits the set of values. Values are always of a type. Objects (constants and variables) are of a subtype. This generalizes, clarifies and systematizes a relationship, e.g. between Integer and 1..100, that is handled ad hoc in the semantics of Pascal.

Constrained types and unconstrained types

There is an important distinction between constrained types and unconstrained types. An unconstrained type has one or more free parameters that affect its size or shape. A constrained type fixes the values of these parameters and so determines its size and shape. Loosely speaking, objects must be of a constrained type, but formal parameters may be of an unconstrained type (they adopt the constraint of any corresponding actual parameter). This solves the problem of array parameters in Pascal (among other things).

Dynamic types

Where values in Pascal or C must be static (e.g. the subscript bounds of an array) they may be dynamic in Ada. However, static expressions are required in certain cases where dynamic evaluation would not permit a reasonable implementation (e.g. in setting the number of digits of precision of a floating point type).

Separation of interface

Ada consistently supports a separation of interface and mechanism. You can see this in the format of a package, which separates its declaration from its body; and in the concept of a private type, whose representation in terms of Ada data structures is inaccessible outside the scope containing its definition.

"Hello, world!" programs

"Hello, world!"

A common example of a language's syntax is the Hello world program. Here a straight-forward Ada Implementation:

File: hello_world_1.adb (view as markup (http://cvs.sourceforge.net/viewcvs.py/wikibook-ada/demos/Source/hello_world_1.adb?only_with_tag=HEAD&view=markup), view as plain text (http://cvs.sourceforge.net/viewcvs.py/*checkout*/wikibook-ada/demos/Source/hello_world_1.adb), download page (https://sourceforge.net/project/showfiles.php?group_id=124904))
with Ada.Text_IO;

procedure Hello is
begin
   Ada.Text_IO.Put_Line("Hello, world!");
end Hello;

The with statement adds the package Ada.Text_IO to the program. It contains all functionality needed for textual Input/Output. It makes the declarations (i.e. all types and the operations on them) within Ada.Text_IO visible by selection. In Ada, packages are used as toolboxes, providing a collection of tools and types in one easy-to-access module.

Next we declare a main procedure. In Ada the main procedure does not need to be called "main". It is implementation defined which subprograms can be used as main subprograms. However, an implementation is required to support all main subprograms that are public parameterless library procedures. (The last sentence is a quote from the reference manual, RM 10.2(29).)

The call on Ada.Text_IO.Put_Line actually writes the "Hello World".

Since with only makes the content of a package visible by selection, we need to prefix the procedure name Put_Line with the package name Ada.Text_IO. If you need procedures from a package more often some form of shortcut is needed. There are two options open:

"Hello, world!" with renames

By renaming a package it is possible to give a shorter alias to any package name. This reduces the typing involved while still keeping some of the readability.

File: hello_world_2.adb (view as markup (http://cvs.sourceforge.net/viewcvs.py/wikibook-ada/demos/Source/hello_world_2.adb?only_with_tag=HEAD&view=markup), view as plain text (http://cvs.sourceforge.net/viewcvs.py/*checkout*/wikibook-ada/demos/Source/hello_world_2.adb), download page (https://sourceforge.net/project/showfiles.php?group_id=124904))
with Ada.Text_IO;

procedure Hello is
   package IO renames Ada.Text_IO;
begin
   IO.Put_Line("Hello, world!");
   IO.New_Line;
   IO.Put_Line("I am an Ada program with package rename.");
end Hello;

renames can also be used for procedures, functions, variables, array elements. It can not be used for types - a type rename can be accomplished with subtype.

"Hello, world!" with use

The use clause makes all the content of a package directly visible. It allows even less typing but removes some of the readability. My suggested "rule of thumb": use for the most used package and renames for all other packages. You might have another rule (for example, always use Ada.Text_IO, never use anything else).

File: hello_world_3.adb (view as markup (http://cvs.sourceforge.net/viewcvs.py/wikibook-ada/demos/Source/hello_world_3.adb?only_with_tag=HEAD&view=markup), view as plain text (http://cvs.sourceforge.net/viewcvs.py/*checkout*/wikibook-ada/demos/Source/hello_world_3.adb), download page (https://sourceforge.net/project/showfiles.php?group_id=124904))
with Ada.Text_IO;

procedure Hello is
   use Ada.Text_IO;
begin
   Put_Line("Hello, world!");
   New_Line;
   Put_Line("I am an Ada program with package use.");
end Hello;

use can be used for packages and in the form of use type for types. use type makes only the operators of the given type directly visible but not any other operations on the type.

FAQ: Why is "Hello, world!" sooo big?

Ada beginners frequently ask how it can be that such a simple program as "Hello, world!" results in such a large executable. The reason has nothing to do with Ada but can usually be found in the compiler and linker options used - or better not used.

Standard behavior for Ada compiler — or better compilers in general — is not to create the best code possible but to be optimized for ease of use. This is done not to frighten away potential new users by providing a system which does not work "out of the box".

The GNAT project files which you can download alongside the example programs use better tuned compiler, binder and linker options. If you use those your "Hello, world!" will be a lot smaller:

 32K ./Linux-i686-Debug/hello_world_1
8,0K ./Linux-i686-Release/hello_world_1
 36K ./Linux-x86_64-Debug/hello_world_1
 12K ./Linux-x86_64-Release/hello_world_1
1,1M ./Windows_NT-i686-Debug/hello_world_1.exe
 16K ./Windows_NT-i686-Release/hello_world_1.exe
 32K ./VMS-AXP-Debug/hello_world_1.exe
 12K ./VMS-AXP-Release/hello_world_1.exe

For comparison the sizes for a plain gnat make compile:

497K hello_world_1 (Linux i686)
500K hello_world_1 (Linux x86_64)
1,5M hello_world_1.exe (Windows_NT i686)
589K hello_world_1.exe (VMS AXP)


Also helps finding: AdaProgrammingBasic, AdaProgramming, ProgrammingBasic, sda, programing, basi, adu, programmin, bacic, adas, progamming, asic, adz, prgramming, basix

   
 
  
Add to bookmarks
Related Articles
 
Ada Programming
Computer programming
Top Articles
 
Ada Programming/Keywords/begin
Ada Programming/Keywords/pragma
Ada Programming/Keywords/private
Ada Programming/Keywords/procedure
Ada Programming/Keywords/range
Ada Programming/Keywords/record
Ada Programming/Keywords/return
Ada Programming/Keywords/use
Cookbook:Basic foodstuffs
Cookbook:Egg
Cookbook:Flour
Cookbook:Fruit
Cookbook:Pepper
Electronics
GAT: A Glossary of Astronomical Terms
Japanese
Programming:C
SA NCS:Mathematical Literacy
SA NCS:Qualification Assesment
SA NCS:Tourism
South African Curriculum
Search LiveJournal blogs for Ada Programming/Basic
 

Insurance Quotes  •  Computer Jobs  •  Web Hosting  •  Buy WoW Gold •  Kamala

Copyright @ 2005 eMyTextBooks.com
This article is from Wikibooks. All text is available under the terms of the GNU Free Documentation License.