Annotated Ada Reference ManualLegal Information
Table of Contents   Index   References   Search   Previous   Next 

 6.3 Subprogram Bodies

1
[A subprogram_body specifies the execution of a subprogram.] 

Syntax

2/2
{AI95-00218-03} subprogram_body ::= 
    [overriding_indicator]

    subprogram_specification is
       declarative_part
    begin
        handled_sequence_of_statements
    end [designator];
3
If a designator appears at the end of a subprogram_body, it shall repeat the defining_designator of the subprogram_specification

Legality Rules

4
[In contrast to other bodies,] a subprogram_body need not be the completion of a previous declaration[, in which case the body declares the subprogram]. If the body is a completion, it shall be the completion of a subprogram_declaration or generic_subprogram_declaration. The profile of a subprogram_body that completes a declaration shall conform fully to that of the declaration. {full conformance (required)}

Static Semantics

5
A subprogram_body is considered a declaration. It can either complete a previous declaration, or itself be the initial declaration of the subprogram. 

Dynamic Semantics

6
{elaboration (non-generic subprogram_body) [partial]} The elaboration of a non-generic subprogram_body has no other effect than to establish that the subprogram can from then on be called without failing the Elaboration_Check. 
6.a
Ramification: See 12.2 for elaboration of a generic body. Note that protected subprogram_bodies never get elaborated; the elaboration of the containing protected_body allows them to be called without failing the Elaboration_Check. 
7
{execution (subprogram_body) [partial]} [The execution of a subprogram_body is invoked by a subprogram call.] For this execution the declarative_part is elaborated, and the handled_sequence_of_statements is then executed. 

Examples

8
Example of procedure body: 
9
procedure Push(E : in Element_Type; S : in out Stack) is
begin
   if S.Index = S.Size then
      raise Stack_Overflow;
   else
      S.Index := S.Index + 1;
      S.Space(S.Index) := E;
   end if;
end Push;
10
Example of a function body: 
11
function Dot_Product(Left, Right : Vector) return Real is
   Sum : Real := 0.0;
begin
   Check(Left'First = Right'First and Left'Last = Right'Last);
   for J in Left'Range loop
      Sum := Sum + Left(J)*Right(J);
   end loop;
   return Sum;
end Dot_Product;

Extensions to Ada 83

11.a
{extensions to Ada 83} A renaming_declaration may be used instead of a subprogram_body

Wording Changes from Ada 83

11.b
The syntax rule for subprogram_body now uses the syntactic category handled_sequence_of_statements.
11.c
The declarative_part of a subprogram_body is now required; that doesn't make any real difference, because a declarative_part can be empty.
11.d
We have incorporated some rules from RM83-6.5 here.
11.e
RM83 forgot to restrict the definition of elaboration of a subprogram_body to non-generics. 

Wording Changes from Ada 95

11.f/2
{AI95-00218-03} Overriding_indicator is added to subprogram_body. 

Table of Contents   Index   References   Search   Previous   Next 
Ada-Europe Sponsored by Ada-Europe