Lady Ada

Ada '83 Language Reference Manual

Copyright 1980, 1982, 1983 owned by the United States Government. Direct reproduction and usage requests to the Ada Information Clearinghouse.


6.1. Subprogram Declarations

[UP][NEXT]

A subprogram declaration declares a procedure or a function, as indicated by the initial reserved word.

    subprogram_declaration ::= subprogram_specification; 

    subprogram_specification ::=
         procedure identifier [formal_part]
       | function designator  [formal_part] return type_mark 

    designator ::= identifier | operator_symbol 

    operator_symbol ::= string_literal 

    formal_part ::=
       (parameter_specification {; parameter_specification}) 

    parameter_specification ::=
       identifier_list : mode type_mark [:= expression] 

    mode ::= [in] | in out | out  

The specification of a procedure specifies its identifier and its formal parameters (if any). The specification of a function specifies its designator, its formal parameters (if any) and the subtype of the returned value (the result subtype). A designator that is an operator symbol is used for the overloading of an operator. The sequence of characters represented by an operator symbol must be an operator belonging to one of the six classes of overloadable operators defined in section 4.5 (extra spaces are not allowed and the case of letters is not significant).

A parameter specification with several identifiers is equivalent to a sequence of single parameter specifications, as explained in section 3.2. Each single parameter specification declares a formal parameter. If no mode is explicitly given, the mode in is assumed. If a parameter specification ends with an expression, the expression is the default expression of the formal parameter. A default expression is only allowed in a parameter specification if the mode is in (whether this mode is indicated explicitly or implicitly). The type of a default expression must be that of the corresponding formal parameter.

The use of a name that denotes a formal parameter is not allowed in default expressions of a formal part if the specification of the parameter is itself given in this formal part.

The elaboration of a subprogram declaration elaborates the corresponding formal part. The elaboration of a formal part has no other effect.

Examples of subprogram declarations:

    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.8.1 

    function RANDOM return PROBABILITY;                      --  see 3.5.7    

    function MIN_CELL(X : LINK) return CELL;                 --  see 3.8.1
    function NEXT_FRAME(K : POSITIVE) return FRAME;          --  see 3.8
    function DOT_PRODUCT(LEFT,RIGHT: VECTOR) return REAL;    --  see 3.6 

    function "*"(LEFT,RIGHT : MATRIX) return MATRIX;         --  see 3.6  

Examples of in parameters with default expressions:

 procedure PRINT_HEADER(PAGES  : in NATURAL;
                HEADER : in LINE    :=  (1 .. LINE'LAST => ' ');  --  see 3.6
                CENTER : in BOOLEAN := TRUE);

Notes:

The evaluation of default expressions is caused by certain subprogram calls, as described in section 6.4.2 (default expressions are not evaluated during the elaboration of the subprogram declaration).

All subprograms can be called recursively and are reentrant.

References: declaration, elaboration, evaluation, expression, formal parameter, function, identifier, identifier list, mode, name, elaboration has no other effect, operator, overloading, and 8.7, procedure, string literal, subprogram call, type mark.

Rationale references: 8.1 Subprogram Declarations and Subprogram Bodies

Style Guide references: 2.1.2 Indentation, 3.2.4 Program Unit Names, 3.3.3 Program Unit Specification Header, 4.1.1 Separate Compilation Capabilities, 4.1.4 Packages, 4.1.5 Cohesion, 4.2.1 Minimization of Interfaces, 4.3.1 Using Exceptions to Help Define an Abstraction, 5.2.1 Formal Parameters, 5.6.6 Recursion and Iteration Bounds, 7.1.3 Comments, 7.1.4 Main Subprogram

[INDEX][CONTENTS]


[Ada Information Clearinghouse]

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.