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

 6.1 Subprogram Declarations

1
[A subprogram_declaration declares a procedure or function.] 

Syntax

2/2
{AI95-00218-03} subprogram_declaration ::= 
    [overriding_indicator]
    
subprogram_specification;
3/2
This paragraph was deleted.{AI95-00348-01} abstract_subprogram_declaration ::= subprogram_specification is abstract;
4/2
{AI95-00348-01} subprogram_specification ::= 
    procedure_specification
  | function_specification

    procedure defining_program_unit_name parameter_profile
  | function defining_designator parameter_and_result_profile
4.1/2
{AI95-00348-01} procedure_specification ::= procedure defining_program_unit_name parameter_profile
4.2/2
{AI95-00348-01} function_specification ::= function defining_designator parameter_and_result_profile
5
designator ::= [parent_unit_name . ]identifier | operator_symbol
6
defining_designator ::= defining_program_unit_name | defining_operator_symbol
7
defining_program_unit_name ::= [parent_unit_name . ]defining_identifier
8
[The optional parent_unit_name is only allowed for library units (see 10.1.1).]
9
operator_symbol ::= string_literal
10/2
{AI95-00395-01} The sequence of characters in an operator_symbol shall form a reserved word, a delimiter, or compound delimiter that corresponds correspond to an operator belonging to one of the six categories classes of operators defined in clause 4.5(spaces are not allowed and the case of letters is not significant).
10.a/2
Reason: {AI95-00395-01} The ‘sequence of characters” of the string literal of the operator is a technical term (see 2.6), and does not include the surrounding quote characters. As defined in 2.2, lexical elements are “formed” from a sequence of characters. Spaces are not allowed, and upper and lower case is not significant. See 2.2 and 2.9 for rules related to the use of other_format characters in delimiters and reserved words. 
11
defining_operator_symbol ::= operator_symbol
12
parameter_profile ::= [formal_part]
13/2
{AI95-00231-01} {AI95-00318-02} parameter_and_result_profile ::= 
    
[formal_partreturn [null_exclusion] subtype_mark
  | [formal_partreturn access_definition
14
formal_part ::= 
   (parameter_specification {; parameter_specification})
15/2
{AI95-00231-01} parameter_specification ::= 
    defining_identifier_list : mode [null_exclusion] subtype_mark [:= default_expression]
  | defining_identifier_list : access_definition [:= default_expression]
16
mode ::= [in] | in out | out

Name Resolution Rules

17
{formal parameter (of a subprogram)} A formal parameter is an object [directly visible within a subprogram_body] that represents the actual parameter passed to the subprogram in a call; it is declared by a parameter_specification. {expected type (parameter default_expression) [partial]} For a formal parameter, the expected type for its default_expression, if any, is that of the formal parameter. {parameter: See formal parameter}

Legality Rules

18
{parameter mode} The parameter mode of a formal parameter conveys the direction of information transfer with the actual parameter: in, in out, or out. Mode in is the default, and is the mode of a parameter defined by an access_definition. The formal parameters of a function, if any, shall have the mode in.
18.a
Ramification: Access parameters are permitted. This restriction to in parameters is primarily a methodological restriction, though it also simplifies implementation for some compiler technologies. 
19
A default_expression is only allowed in a parameter_specification for a formal parameter of mode in.
20/2
 {AI95-00348-01} {requires a completion (subprogram_declaration) [partial]} {requires a completion (generic_subprogram_declaration) [partial]} A subprogram_declaration or a generic_subprogram_declaration requires a completion: [a body, a renaming_declaration (see 8.5), or a pragma pragma Import (see B.1)]. [A completion is not allowed for an abstract_subprogram_declaration (see 3.9.3) or a null_procedure_declaration (see 6.7).] 
20.a/2
Ramification: {AI95-00348-01} Abstract subprograms and null procedures are not declared by subprogram_declarations, and so do not require completion. Protected subprograms are declared by subprogram_declarations, and so require completion. Note that an abstract subprogram is a subprogram, and a protected subprogram is a subprogram, but a generic subprogram is not a subprogram. 
21
A name that denotes a formal parameter is not allowed within the formal_part in which it is declared, nor within the formal_part of a corresponding body or accept_statement.
21.a
Ramification: By contrast, generic_formal_parameter_declarations are visible to subsequent declarations in the same generic_formal_part.

Static Semantics

22
{profile} The profile of (a view of) a callable entity is either a parameter_profile or parameter_and_result_profile[; it embodies information about the interface to that entity — for example, the profile includes information about parameters passed to the callable entity. All callable entities have a profile — enumeration literals, other subprograms, and entries. An access-to-subprogram type has a designated profile.] Associated with a profile is a calling convention. A subprogram_declaration declares a procedure or a function, as indicated by the initial reserved word, with name and profile as given by its specification.
23/2
 {AI95-00231-01} {AI95-00318-02} {nominal subtype (of a formal parameter) [partial]} The nominal subtype of a formal parameter is the subtype determined denoted by the optional null_exclusion and the subtype_mark, or defined by the access_definition, in the parameter_specification. The nominal subtype of a function result is the subtype determined by the optional null_exclusion and the subtype_mark, or defined by the access_definition, in the parameter_and_result_profile. {nominal subtype (of a function result) [partial]}
24/2
 {AI95-00231-01} {AI95-00254-01} {AI95-00318-02} {access parameter} An access parameter is a formal in parameter specified by an access_definition. {access result type} An access result type is a function result type specified by an access_definition. An access parameter or result type is of an anonymous access general access-to-variable type (see 3.10). [Access parameters of an access-to-object type allow dispatching calls to be controlled by access values. Access parameters of an access-to-subprogram type permit calls to subprograms passed as parameters irrespective of their accessibility level. ]
24.a/2
Discussion: {AI95-00318-02} Access result types have normal accessibility and thus don't have any special properties worth noting here. 
25
{subtypes (of a profile)} The subtypes of a profile are: 
26
27/2
27.1/2
28/2
28.1/2
28.2/2
29
[{types (of a profile)} The types of a profile are the types of those subtypes.]
30/2
 {AI95-00348-01} [A subprogram declared by an abstract_subprogram_declaration is abstract; a subprogram declared by a subprogram_declaration is not. See 3.9.3, “Abstract Types and Subprograms”. Similarly, a procedure defined by a null_procedure_declaration is a null procedure; a procedure declared by a subprogram_declaration is not. See 6.7, “Null Procedures”. ]
30.1/2
   {AI95-00218-03} [An overriding_indicator is used to indicate whether overriding is intended. See 8.3.1, “Overriding Indicators”.]

Dynamic Semantics

31/2
 {AI95-00348-01} {elaboration (subprogram_declaration) [partial]} {elaboration (abstract_subprogram_declaration) [partial]} The elaboration of a subprogram_declaration or an abstract_subprogram_declaration has no effect. 
NOTES
32
1  A parameter_specification with several identifiers is equivalent to a sequence of single parameter_specifications, as explained in 3.3.
33
2  Abstract subprograms do not have bodies, and cannot be used in a nondispatching call (see 3.9.3, “Abstract Types and Subprograms”).
34
3  The evaluation of default_expressions is caused by certain calls, as described in 6.4.1. They are not evaluated during the elaboration of the subprogram declaration.
35
4  Subprograms can be called recursively and can be called concurrently from multiple tasks. 

Examples

36
Examples of subprogram declarations: 
37
procedure Traverse_Tree;
procedure Increment(X : in out Integer);
procedure Right_Indent(Margin : out Line_Size);          --  see 3.5.4
procedure Switch(From, To : in out Link);                --  see 3.10.1
38
function Random return Probability;                      --  see 3.5.7
39
function Min_Cell(X : Link) return Cell;                 --  see 3.10.1
function Next_Frame(K : Positive) return Frame;          --  see 3.10
function Dot_Product(Left, Right : Vector) return Real;  --  see 3.6
40
function "*"(Left, Right : Matrix) return Matrix;        --  see 3.6
41
Examples of in parameters with default expressions: 
42
procedure Print_Header(Pages  : in Natural;
            Header : in Line    :=  (1 .. Line'Last => ' ');  --  see 3.6
            Center : in Boolean := True);

Extensions to Ada 83

42.a
{extensions to Ada 83} The syntax for abstract_subprogram_declaration is added. The syntax for parameter_specification is revised to allow for access parameters (see 3.10)
42.b
Program units that are library units may have a parent_unit_name to indicate the parent of a child (see Section 10). 

Wording Changes from Ada 83

42.c
We have incorporated the rules from RM83-6.5, “Function Subprograms” here and in 6.3, “Subprogram Bodies
42.d
We have incorporated the definitions of RM83-6.6, “Parameter and Result Type Profile - Overloading of Subprograms” here.
42.e
The syntax rule for defining_operator_symbol is new. It is used for the defining occurrence of an operator_symbol, analogously to defining_identifier. Usage occurrences use the direct_name or selector_name syntactic categories. The syntax rules for defining_designator and defining_program_unit_name are new. 

Extensions to Ada 95

42.f/2
{AI95-00218-03} {extensions to Ada 95} Subprograms now allow overriding_indicators for better error checking of overriding.
42.g/2
{AI95-00231-01} An optional null_exclusion can be used in a formal parameter declaration. Similarly, an optional null_exclusion can be used in a function result.
42.h/2
{AI95-00318-02} The return type of a function can be an anonymous access type. 

Wording Changes from Ada 95

42.i/2
{AI95-00254-01} A description of the purpose of anonymous access-to-subprogram parameters and the definition of the profile of subprograms containing them was added.
42.j/2
{AI95-00348-01} Split the production for subprogram_specification in order to make the declaration of null procedures (see 6.7) easier.
42.k/2
{AI95-00348-01} Moved the Syntax and Dynamic Semantics for abstract_subprogram_declaration to 3.9.3, so that the syntax and semantics are together. This also keeps abstract and null subprograms similar.
42.l/2
{AI95-00395-01} Revised to allow other_format characters in operator_symbols in the same way as the underlying constructs. 

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