home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / lrm / chap06.doc < prev    next >
Encoding:
Text File  |  1988-05-03  |  36.9 KB  |  1,586 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. The following document is a draft  of  the  corresponding  chapter  of  the
  7. version  of  the  Ada  Reference  Manual  produced  in response to the Ansi
  8. Canvass.  It is given a limited circulation  to  Ada  implementers  and  to
  9. other groups contributing comments (according to the conventions defined in
  10. RRM.comments).  This draft should not be referred to in any publication.
  11.  
  12.  
  13.  
  14.                       ANSI-RM-06.v23 - Draft Chapter
  15.  
  16.                               6  Subprograms
  17.                                 version 23
  18.  
  19.                                  83-02-11
  20.  
  21. This revision has addressed comments up to the number #5795
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.                               6. Subprograms
  78.  
  79.  
  80.  
  81.  
  82. Subprograms  are  one  of the four forms of program unit, of which programs
  83. can be composed.  The other forms are packages,  task  units,  and  generic
  84. units.
  85.  
  86. A  subprogram  is a program unit whose execution is invoked by a subprogram
  87. call.  There are two forms of  subprogram:  procedures  and  functions.   A
  88. procedure  call  is  a  statement;   a  function  call is an expression and
  89. returns a value.  The definition of a subprogram can be given in two parts:
  90. a subprogram declaration defining its calling conventions, and a subprogram
  91. body defining its execution.
  92.  
  93. References:  function 6.5, function call 6.4, generic unit 12,  package  7,
  94. procedure  6.1,  procedure  call  6.4, subprogram body 6.3, subprogram call
  95. 6.4, subprogram declaration 6.1, task unit 9
  96.  
  97.  
  98.  
  99. 6.1  Subprogram Declarations
  100.  
  101.  
  102. A subprogram declaration declares a procedure or a function,  as  indicated
  103. by the initial reserved word.
  104.  
  105.  
  106.     subprogram_declaration ::= subprogram_specification;
  107.  
  108.     subprogram_specification ::=
  109.          procedure identifier [formal_part]
  110.        | function designator  [formal_part] return type_mark
  111.  
  112.     designator ::= identifier | operator_symbol
  113.  
  114.     operator_symbol ::= string_literal
  115.  
  116.     formal_part ::=
  117.        (parameter_specification {; parameter_specification})
  118.  
  119.     parameter_specification ::=
  120.        identifier_list : mode type_mark [:= expression]
  121.  
  122.     mode ::= [in] | in out | out
  123.  
  124.  
  125. The  specification  of  a procedure specifies its identifier and its formal
  126. parameters (if  any).   The  specification  of  a  function  specifies  its
  127.  
  128.  
  129.                                    6 - 1
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. designator,  its formal parameters (if any) and the subtype of the returned
  139. value (the result subtype).  A designator that is  an  operator  symbol  is
  140. used  for  the  overloading  of  an  operator.   The sequence of characters
  141. represented by an operator symbol must be an operator belonging to  one  of
  142. the  six  classes  of  overloadable operators defined in section 4.5 (extra
  143. spaces are not allowed and the case of letters is not significant).
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                    6 - 2
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205. A parameter specification with  several  identifiers  is  equivalent  to  a
  206. sequence  of  single parameter specifications, as explained in section 3.2.
  207. Each single parameter specification declares a  formal  parameter.   If  no
  208. mode  is  explicitly  given,  the  mode  in  is  assumed.   If  a parameter
  209. specification ends with  an  expression,  the  expression  is  the  default
  210. expression  of  the formal parameter.  A default expression is only allowed
  211. in a parameter specification if the  mode  is  in  (whether  this  mode  is
  212. indicated explicitly or implicitly).  The type of a default expression must
  213. be that of the corresponding formal parameter.
  214.  
  215. The use of a name that denotes a formal parameter is not allowed in default
  216. expressions  of  a  formal  part  if  the specification of the parameter is
  217. itself given in this formal part.
  218.  
  219. The elaboration of a subprogram declaration  elaborates  the  corresponding
  220. formal part.  The elaboration of a formal part has no other effect.
  221.  
  222. Examples of subprogram declarations:
  223.  
  224.     procedure TRAVERSE_TREE;
  225.     procedure INCREMENT(X : in out INTEGER);
  226.     procedure RIGHT_INDENT(MARGIN : out LINE_SIZE);          --  see 3.5.4
  227.     procedure SWITCH(FROM, TO : in out LINK);                --  see 3.8.1
  228.  
  229.     function RANDOM return PROBABILITY;                      --  see 3.5.7
  230.  
  231.     function MIN_CELL(X : LINK) return CELL;                 --  see 3.8.1
  232.     function NEXT_FRAME(K : POSITIVE) return FRAME;          --  see 3.8
  233.     function DOT_PRODUCT(LEFT,RIGHT: VECTOR) return REAL;    --  see 3.6
  234.  
  235.     function "*"(LEFT,RIGHT : MATRIX) return MATRIX;         --  see 3.6
  236.  
  237.  
  238. Examples of in parameters with default expressions:
  239.  
  240.  
  241.     procedure PRINT_HEADER(PAGES  : in NATURAL;
  242.                            HEADER : in LINE    :=  (1 .. LINE'LAST => ' ');  --  see 3.6
  243.                            CENTER : in BOOLEAN := TRUE);
  244.  
  245.  
  246. Notes:
  247.  
  248. The  evaluation  of  default  expressions  is  caused by certain subprogram
  249. calls, as described in section 6.4.2 (default expressions are not evaluated
  250. during the elaboration of the subprogram declaration).
  251.  
  252. All subprograms can be called recursively and are reentrant.
  253.  
  254.  
  255. References:  declaration 3.1, elaboration 3.9, evaluation  4.5,  expression
  256. 4.4,  formal  parameter  6.2, function 6.5, identifier 2.3, identifier list
  257. 3.2, mode 6.2, name 4.1, elaboration has no other effect 3.9, operator 4.5,
  258. overloading 6.6 8.7, procedure 6, string literal 2.6, subprogram call  6.4,
  259.  
  260.  
  261.                                    6 - 3
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270. type mark 3.3.2
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                    6 - 4
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336. 6.2  Formal Parameter Modes
  337.  
  338.  
  339. The value of an object is said to be read when this value is evaluated;  it
  340. is  also  said to be read when one of its subcomponents is read.  The value
  341. of a variable is said to be updated when an assignment is performed to  the
  342. variable,  and  also  (indirectly)  when  the  variable  is  used as actual
  343. parameter of a subprogram call or entry call  statement  that  updates  its
  344. value;   it  is  also  said  to be updated when one of its subcomponents is
  345. updated.
  346.  
  347. A formal parameter of a subprogram has one of the three following modes:
  348.  
  349.   in      The formal parameter  is a  constant  and  permits   only reading  of the value  of the
  350.           associated actual parameter.
  351.  
  352.   in out  The formal parameter is a variable  and permits both reading and updating of the  value
  353.           of the associated actual parameter.
  354.  
  355.   out     The formal parameter is a variable  and permits updating of the value of the associated
  356.           actual parameter.
  357.  
  358.           The value  of  a scalar parameter that is not updated by the call is undefined  upon return;
  359.           the same  holds for the  value  of a  scalar subcomponent,  other  than a  discriminant.
  360.           Reading the  bounds and discriminants of the  formal parameter  and  of its subcomponents
  361.           is allowed,  but no other reading.
  362.  
  363. For a scalar parameter, the above effects are achieved  by  copy:   at  the
  364. start  of  each  call, if the mode is in or in out, the value of the actual
  365. parameter is copied into  the  associated  formal  parameter;   then  after
  366. normal completion of the subprogram body, if the mode is in out or out, the
  367. value  of  the  formal  parameter is copied back into the associated actual
  368. parameter.  For a parameter whose type is an access type, copy-in  is  used
  369. for all three modes, and copy-back for the modes in out and out.
  370.  
  371. For  a  parameter  whose  type  is  an  array,  record,  or  task  type, an
  372. implementation may likewise achieve the  above  effects  by  copy,  as  for
  373. scalar  types.   In  addition, if copy is used for a parameter of mode out,
  374. then copy-in is required at least for the bounds and discriminants  of  the
  375. actual  parameter  and of its subcomponents, and also for each subcomponent
  376. whose type is an access type.  Alternatively, an implementation may achieve
  377. these effects by reference, that is, by arranging that  every  use  of  the
  378. formal  parameter  (to  read or to update its value) be treated as a use of
  379. the associated actual parameter, throughout the execution of the subprogram
  380. call.  The language does not define which of these two mechanisms is to  be
  381. adopted  for  parameter  passing,  nor  whether different calls to the same
  382. subprogram are to use the same mechanism.  The execution of  a  program  is
  383. erroneous  if  its  effect  depends  on  which mechanism is selected by the
  384. implementation.
  385.  
  386. For a parameter whose type  is  a  private  type,  the  above  effects  are
  387. achieved  according to the rule that applies to the corresponding full type
  388. declaration.
  389.  
  390.  
  391.  
  392.  
  393.                                    6 - 5
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402. Within the body of a subprogram, a  formal  parameter  is  subject  to  any
  403. constraint   resulting   from   the   type  mark  given  in  its  parameter
  404. specification.  For a formal parameter of an unconstrained array type,  the
  405. bounds  are obtained from the actual parameter, and the formal parameter is
  406. constrained by these bounds (see 3.6.1).   For  a  formal  parameter  whose
  407. declaration  specifies  an  unconstrained  (private  or  record)  type with
  408. discriminants, the discriminants of the formal  parameter  are  initialized
  409. with the values of the corresponding discriminants of the actual parameter;
  410. the  formal parameter is unconstrained if and only if the mode is in out or
  411. out and the variable  name  given  for  the  actual  parameter  denotes  an
  412. unconstrained variable (see 3.7.1 and 6.4.1).
  413.  
  414. If the actual parameter of a subprogram call is a subcomponent that depends
  415. on discriminants of an unconstrained record variable, then the execution of
  416. the  call  is  erroneous  if  the  value of any of the discriminants of the
  417. variable is changed by this execution;  this rule does  not  apply  if  the
  418. mode  is  in and the type of the subcomponent is a scalar type or an access
  419. type.
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                    6 - 6
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468. Notes:
  469.  
  470. For parameters of array and record types, the parameter passing rules  have
  471. these consequences:
  472.  
  473.   -  If the execution of a subprogram  is  abandoned  as  a  result  of  an
  474.      exception,  the  final value of an actual parameter of such a type can
  475.      be either its value before the call or a value assigned to the  formal
  476.      parameter during the execution of the subprogram.
  477.  
  478.   -  If no actual parameter of such a type is accessible by more  than  one
  479.      path,  then  the effect of a subprogram call (unless abandoned) is the
  480.      same whether or not the  implementation  uses  copying  for  parameter
  481.      passing.   If,  however,  there  are  multiple  access paths to such a
  482.      parameter (for example,  if  a  global  variable,  or  another  formal
  483.      parameter, refers to the same actual parameter), then the value of the
  484.      formal  is  undefined after updating the actual other than by updating
  485.      the formal.  A program using such an undefined value is erroneous.
  486.  
  487. The same parameter modes are defined for formal parameters of entries  (see
  488. 9.5)  with  the same meaning as for subprograms.  Different parameter modes
  489. are defined for generic formal parameters (see 12.1.1).
  490.  
  491. For all modes, if an actual parameter designates  a  task,  the  associated
  492. formal   parameter  designates  the  same  task;   the  same  holds  for  a
  493. subcomponent of an actual parameter and the corresponding  subcomponent  of
  494. the associated formal parameter.
  495.  
  496. References:   access  type  3.8,  actual  parameter  6.4.1, array type 3.6,
  497. assignment 5.2, bound of an  array  3.6.1,  constraint  3.3,  depend  on  a
  498. discriminant 3.7.1, discriminant 3.7.1, entry call statement 9.5, erroneous
  499. 1.6,  evaluation  4.5,  exception 11, expression 4.4, formal parameter 6.1,
  500. generic formal parameter 12.1, global 8.1, mode 6.1, null access value 3.8,
  501. object 3.2, parameter specification 6.1, private type 7.4, record type 3.7,
  502. scalar type 3.5, subcomponent 3.3, subprogram  body  6.3,  subprogram  call
  503. statement  6.4, task 9, task type 9.2, type mark 3.3.2, unconstrained array
  504. type  3.6,  unconstrained  type  with  discriminants  3.7.1,  unconstrained
  505. variable 3.2.1, variable 3.2.1
  506.  
  507.  
  508.  
  509.  
  510. 6.3  Subprogram Bodies
  511.  
  512.  
  513. A subprogram body specifies the execution of a subprogram.
  514.  
  515.     subprogram_body ::=
  516.         subprogram_specification is
  517.            [declarative_part]
  518.         begin
  519.             sequence_of_statements
  520.        [exception
  521.             exception_handler
  522.            {exception_handler}]
  523.  
  524.  
  525.                                    6 - 7
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.         end [designator];
  535.  
  536. The  declaration  of  a  subprogram  is optional.  In the absence of such a
  537. declaration, the subprogram specification of the subprogram body  (or  body
  538. stub) acts as the declaration.  For each subprogram declaration, there must
  539. be  a  corresponding  body  (except  for  a  subprogram  written in another
  540. language, as explained in section 13.9).  If both a declaration and a  body
  541. are  given,  the  subprogram  specification of the body must conform to the
  542. subprogram  specification  of  the  declaration  (see  section  6.3.1   for
  543. conformance rules).
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                    6 - 8
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600. If a designator appears at the end of a subprogram body, it must repeat the
  601. designator of the subprogram specification.
  602.  
  603. The  elaboration of a subprogram body has no other effect than to establish
  604. that the body can from then on be used for the execution of  calls  of  the
  605. subprogram.
  606.  
  607. The  execution  of  a  subprogram body is invoked by a subprogram call (see
  608. 6.4).  For this  execution,  after  establishing  the  association  between
  609. formal  parameters  and actual parameters, the declarative part of the body
  610. is elaborated, and the sequence of statements of the body is then executed.
  611. Upon completion of the body,   return  is  made  to  the  caller  (and  any
  612. necessary  copying  back  of formal to actual parameters occurs (see 6.2)).
  613. The optional exception handlers at the end  of  a  subprogram  body  handle
  614. exceptions raised during the execution of the sequence of statements of the
  615. subprogram body (see 11.4).
  616.  
  617. Note:
  618.  
  619. It  follows  from  the  visibility rules that if a subprogram declared in a
  620. package is to be visible outside the package,  a  subprogram  specification
  621. must  be  given in the visible part of the package.  The same rules dictate
  622. that a subprogram declaration must be given if a  call  of  the  subprogram
  623. occurs  textually  before  the  subprogram  body (the declaration must then
  624. occur earlier than the call in the  program  text).   The  rules  given  in
  625. sections   3.9  and  7.1  imply  that  a  subprogram  declaration  and  the
  626. corresponding body must both occur immediately within the same  declarative
  627. region.
  628.  
  629. Example of subprogram body:
  630.  
  631.     procedure PUSH(E : in ELEMENT_TYPE; S : in out STACK) is
  632.     begin
  633.        if S.INDEX = S.SIZE then
  634.           raise STACK_OVERFLOW;
  635.        else
  636.           S.INDEX := S.INDEX + 1;
  637.           S.SPACE(S.INDEX) := E;
  638.        end if;
  639.     end PUSH;
  640.  
  641.  
  642. References:   actual  parameter  6.4.1,  body  stub  10.2,  conform  6.3.1,
  643. declaration 3.1, declarative part 3.9, declarative region  8.1,  designator
  644. 6.1,  elaboration  3.9,  elaboration has no other effect 3.1, exception 11,
  645. exception handler 11.2, formal parameter 6.1, occur immediately within 8.1,
  646. package 7, sequence of statements 5.1, subprogram 6, subprogram  call  6.4,
  647. subprogram  declaration  6.1, subprogram specification 6.1, visibility 8.3,
  648. visible part 7.2
  649.  
  650.  
  651.  
  652.  
  653. 6.3.1  Conformance Rules
  654.  
  655.  
  656.  
  657.                                    6 - 9
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666. Whenever the language rules require or allow the specification of  a  given
  667. subprogram  to be provided in more than one place, the following variations
  668. are allowed at each place:
  669.  
  670.   -  A numeric literal can be replaced by a different  numeric  literal  if
  671.      and only if both have the same value.
  672.  
  673.   -  A simple name can be replaced by an expanded name in which this simple
  674.      name is the selector, if and only if at both places the meaning of the
  675.      simple name is given by the same declaration.
  676.  
  677.   -  A string literal given as an operator symbol  can  be  replaced  by  a
  678.      different  string  literal  if  and  only  if  both represent the same
  679.      operator.
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                   6 - 10
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732. Two subprogram specifications are said to conform if, apart  from  comments
  733. and  the  above  allowed  variations, both specifications are formed by the
  734. same sequence of lexical elements, and corresponding lexical  elements  are
  735. given the same meaning by the visibility and overloading rules.
  736.  
  737. Conformance  is  likewise defined for formal parts, discriminant parts, and
  738. type marks (for deferred constants and for actual parameters that have  the
  739. form of a type conversion (see 6.4.1)).
  740.  
  741. Notes:
  742.  
  743. A  simple  name can be replaced by an expanded name even if the simple name
  744. is itself the prefix of a selected component.   For  example,  Q.R  can  be
  745. replaced by P.Q.R if Q is declared immediately within P.
  746.  
  747. The  following  specifications  do not conform since they are not formed by
  748. the same sequence of lexical elements:
  749.  
  750.     procedure P(X,Y : INTEGER)
  751.     procedure P(X : INTEGER; Y : INTEGER)
  752.     procedure P(X,Y : in INTEGER)
  753.  
  754.  
  755. References:   actual  parameter  6.4  6.4.1,  allow   1.6,   comment   2.7,
  756. declaration   3.1,   deferred   constant   7.4.3,  direct  visibility  8.3,
  757. discriminant part 3.7.1, expanded name  4.1.3,  formal  part  6.1,  lexical
  758. element  2, name 4.1, numeric literal 2.4, operator symbol 6.1, overloading
  759. 6.6 8.7, prefix 4.1, selected component 4.1.3, selector 4.1.3, simple  name
  760. 4.1, subprogram specification 6.1, type conversion 4.6, visibility 8.3
  761.  
  762.  
  763.  
  764.  
  765. 6.3.2  Inline Expansion of Subprograms
  766.  
  767.  
  768. The  pragma  INLINE  is  used  to  indicate  that  inline  expansion of the
  769. subprogram body is desired for every call of each of the named subprograms.
  770. The form of this pragma is as follows:
  771.  
  772.     pragma INLINE (name {, name});
  773.  
  774. Each name is either the name of a subprogram  or  the  name  of  a  generic
  775. subprogram.   The  pragma  INLINE  is  only  allowed  at  the  place  of  a
  776. declarative item in a declarative part or package specification, or after a
  777. library unit in a compilation, but before any subsequent compilation  unit.
  778.  
  779. If  the  pragma  appears at the place of a declarative item, each name must
  780. denote a  subprogram  or  a  generic  subprogram  declared  by  an  earlier
  781. declarative item of the same declarative part or package specification.  If
  782. several  (overloaded)  subprograms  satisfy  this  requirement,  the pragma
  783. applies to all of them.  If the pragma appears after a given library  unit,
  784. the  only  name allowed is the name of this unit.  If the name of a generic
  785. subprogram is mentioned in the pragma, this indicates that inline expansion
  786. is desired for calls of all subprograms obtained by  instantiation  of  the
  787.  
  788.  
  789.                                   6 - 11
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798. named generic unit.
  799.  
  800. The  meaning of a subprogram is not changed by the pragma INLINE.  For each
  801. call of the named subprograms, an implementation is free to  follow  or  to
  802. ignore  the  recommendation expressed by the pragma.  (Note, in particular,
  803. that the recommendation  cannot  generally  be  followed  for  a  recursive
  804. subprogram.)
  805.  
  806.  
  807. References:    allow   1.6,   compilation   10.1,  compilation  unit  10.1,
  808. declarative item  3.9,  declarative  part  3.9,  generic  subprogram  12.1,
  809. generic  unit  12  12.1,  instantiation  12.3, library unit 10.1, name 4.1,
  810. overloading 6.6 8.7, package specification 7.1, pragma 2.8,  subprogram  6,
  811. subprogram body 6.3, subprogram call 6.4
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.                                   6 - 12
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864. 6.4  Subprogram Calls
  865.  
  866.  
  867. A  subprogram call is either a procedure call statement or a function call;
  868. it invokes the execution of the corresponding subprogram  body.   The  call
  869. specifies  the  association  of  the actual parameters, if any, with formal
  870. parameters of the subprogram.
  871.  
  872.     procedure_call_statement ::=
  873.         procedure_name [actual_parameter_part];
  874.  
  875.     function_call ::=
  876.         function_name [actual_parameter_part]
  877.  
  878.     actual_parameter_part ::=
  879.         (parameter_association {, parameter_association})
  880.  
  881.     parameter_association ::=
  882.        [formal_parameter =>] actual_parameter
  883.  
  884.     formal_parameter ::= parameter_simple_name
  885.  
  886.     actual_parameter ::=
  887.        expression | variable_name | type_mark(variable_name)
  888.  
  889. Each  parameter  association  associates  an  actual   parameter   with   a
  890. corresponding  formal  parameter.   A  parameter  association is said to be
  891. named if the formal parameter is named explicitly;  it is otherwise said to
  892. be  positional.   For  a  positional  association,  the  actual   parameter
  893. corresponds  to  the  formal parameter with the same position in the formal
  894. part.
  895.  
  896. Named associations can be given in any order, but if  both  positional  and
  897. named  associations are used in the same call, positional associations must
  898. occur first, at their normal position.  Hence once a named  association  is
  899. used, the rest of the call must use only named associations.
  900.  
  901. For  each  formal parameter of a subprogram, a subprogram call must specify
  902. exactly one corresponding  actual  parameter.   This  actual  parameter  is
  903. specified either explicitly, by a parameter association, or, in the absence
  904. of such an association, by a default expression (see 6.4.2).
  905.  
  906. The parameter associations of a subprogram call are evaluated in some order
  907. that  is not defined by the language.  Similarly, the language rules do not
  908. define in which order the values of in out or  out  parameters  are  copied
  909. back into the corresponding actual parameters (when this is done).
  910.  
  911. Examples of procedure calls:
  912.  
  913.     TRAVERSE_TREE;                                                 --  see 6.1
  914.     TABLE_MANAGER.INSERT(E);                                       --  see 7.5
  915.     PRINT_HEADER(128, TITLE, TRUE);                                --  see 6.1
  916.  
  917.     SWITCH(FROM => X, TO => NEXT);                                 --  see 6.1
  918.     PRINT_HEADER(128, HEADER => TITLE, CENTER => TRUE);            --  see 6.1
  919.  
  920.  
  921.                                   6 - 13
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.     PRINT_HEADER(HEADER => TITLE, CENTER => TRUE, PAGES => 128);   --  see 6.1
  931.  
  932. Examples of function calls:
  933.  
  934.     DOT_PRODUCT(U, V)   --  see 6.1 and 6.5
  935.     CLOCK               --  see 9.6
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.                                   6 - 14
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996. References:   default expression for a formal parameter 6.1, erroneous 1.6,
  997. expression 4.4, formal parameter 6.1, formal part  6.1,  name  4.1,  simple
  998. name 4.1, subprogram 6, type mark 3.3.2, variable 3.2.1
  999.  
  1000.  
  1001.  
  1002.  
  1003. 6.4.1  Parameter Associations
  1004.  
  1005.  
  1006. Each  actual  parameter must have the same type as the corresponding formal
  1007. parameter.
  1008.  
  1009. An actual parameter associated with a formal parameter of mode in  must  be
  1010. an expression;  it is evaluated before the call.
  1011.  
  1012. An  actual  parameter  associated with a formal parameter of mode in out or
  1013. out must be either the name of a  variable,  or  of  the  form  of  a  type
  1014. conversion  whose  argument is the name of a variable.  In either case, for
  1015. the mode in out, the variable must not be a formal parameter of mode out or
  1016. a subcomponent thereof.  For an actual parameter that has  the  form  of  a
  1017. type conversion, the type mark must conform (see 6.3.1) to the type mark of
  1018. the formal parameter;  the allowed operand and target types are the same as
  1019. for type conversions (see 4.6).
  1020.  
  1021. The  variable  name  given for an actual parameter of mode in out or out is
  1022. evaluated before the call.  If the actual parameter has the form of a  type
  1023. conversion,  then  before  the  call,  for  a parameter of mode in out, the
  1024. variable is converted to the specified type;  after (normal) completion  of
  1025. the  subprogram  body,  for  a  parameter of mode in out or out, the formal
  1026. parameter is converted back  to  the  type  of  the  variable.   (The  type
  1027. specified in the conversion must be that of the formal parameter.)
  1028.  
  1029. The  following constraint checks are performed for parameters of scalar and
  1030. access types:
  1031.  
  1032.   -  Before the call:  for a parameter of mode in or in out, it is  checked
  1033.      that  the  value of the actual parameter belongs to the subtype of the
  1034.      formal parameter.
  1035.  
  1036.   -  After (normal) completion of the subprogram body:  for a parameter  of
  1037.      mode  in  out  or  out,  it  is  checked  that the value of the formal
  1038.      parameter belongs to the subtype of the actual variable.  In the  case
  1039.      of  a  type conversion, the value of the formal parameter is converted
  1040.      back and the check applies to the result of the conversion.
  1041.  
  1042. In each of the above cases, the execution of the program  is  erroneous  if
  1043. the checked value is undefined.
  1044.  
  1045. For  other  types,  for  all  modes, a check is made before the call as for
  1046. scalar and access types;  no check is made upon return.
  1047.  
  1048. The exception CONSTRAINT_ERROR is raised at the  place  of  the  subprogram
  1049. call if either of these checks fails.
  1050.  
  1051.  
  1052.  
  1053.                                   6 - 15
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062. Note:
  1063.  
  1064. For array types and for types with discriminants, the check before the call
  1065. is  sufficient (a check upon return would be redundant) if the type mark of
  1066. the formal parameter denotes a constrained  subtype,  since  neither  array
  1067. bounds nor discriminants can then vary.
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.                                   6 - 16
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128. If this type mark denotes an unconstrained array type, the formal parameter
  1129. is constrained with the bounds of the corresponding actual parameter and no
  1130. check  (neither  before  the  call  nor upon return) is needed (see 3.6.1).
  1131. Similarly, no check is needed if the type  mark  denotes  an  unconstrained
  1132. type  with  discriminants,  since  the formal parameter is then constrained
  1133. exactly as the corresponding actual parameter (see 3.7.1).
  1134.  
  1135. References:  actual parameter 6.4, array bound 3.6, array type 3.6, call of
  1136. a subprogram 6.4, conform 6.3.1, constrained subtype 3.3,  constraint  3.3,
  1137. constraint_error   exception   11.1,  discriminant  3.7.1,  erroneous  1.6,
  1138. evaluation 4.5, evaluation of a name 4.1, expression 4.4, formal  parameter
  1139. 6.1,  mode 6.1, name 4.1, parameter association 6.4, subtype 3.3, type 3.3,
  1140. type conversion  4.6,  type  mark  3.3.2,  unconstrained  array  type  3.6,
  1141. unconstrained   type  with  discriminants  3.7.1,  undefined  value  3.2.1,
  1142. variable 3.2.1
  1143.  
  1144.  
  1145.  
  1146.  
  1147. 6.4.2  Default Parameters
  1148.  
  1149.  
  1150. If a parameter specification includes a default expression for a  parameter
  1151. of  mode  in,  then  corresponding  subprogram  calls  need  not  include a
  1152. parameter association for the parameter.  If  a  parameter  association  is
  1153. thus  omitted from a call, then the rest of the call, following any initial
  1154. positional associations, must use only named associations.
  1155.  
  1156. For any omitted parameter association, the default expression is  evaluated
  1157. before  the  call  and  the  resulting  value is used as an implicit actual
  1158. parameter.
  1159.  
  1160. Examples of procedures with default values:
  1161.  
  1162.     procedure ACTIVATE(PROCESS : in PROCESS_NAME;
  1163.                        AFTER   : in PROCESS_NAME := NO_PROCESS;
  1164.                        WAIT    : in DURATION := 0.0;
  1165.                        PRIOR   : in BOOLEAN := FALSE);
  1166.  
  1167.     procedure PAIR(LEFT, RIGHT : PERSON_NAME := new PERSON);
  1168.  
  1169. Examples of their calls:
  1170.  
  1171.     ACTIVATE(X);
  1172.     ACTIVATE(X, AFTER => Y);
  1173.     ACTIVATE(X, WAIT => 60.0, PRIOR => TRUE);
  1174.     ACTIVATE(X, Y, 10.0, FALSE);
  1175.  
  1176.     PAIR;
  1177.     PAIR(LEFT => new PERSON, RIGHT => new PERSON);
  1178.  
  1179. Note:
  1180.  
  1181. If a default expression is used for two or more parameters  in  a  multiple
  1182. parameter  specification, the default expression is evaluated once for each
  1183.  
  1184.  
  1185.                                   6 - 17
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194. omitted parameter.  Hence in the above examples, the two calls of PAIR  are
  1195. equivalent.
  1196.  
  1197. References:   actual  parameter  6.4.1,  default  expression  for  a formal
  1198. parameter 6.1, evaluation  4.5,  formal  parameter  6.1,  mode  6.1,  named
  1199. parameter   association   6.4,   parameter   association   6.4,   parameter
  1200. specification 6.1, positional parameter association  6.4,  subprogram  call
  1201. 6.4
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.                                   6 - 18
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260. 6.5  Function Subprograms
  1261.  
  1262.  
  1263. A function is a subprogram that returns a value (the result of the function
  1264. call).   The  specification  of  a  function  starts with the reserved word
  1265. function, and the parameters, if any, must have the mode in  (whether  this
  1266. mode  is  specified  explicitly  or  implicitly).   The  statements  of the
  1267. function body (excluding statements of program units that are inner to  the
  1268. function  body)  must  include one or more return statements specifying the
  1269. returned value.
  1270.  
  1271. The exception PROGRAM_ERROR is raised if a function body is left  otherwise
  1272. than  by  a  return statement.  This does not apply if the execution of the
  1273. function is abandoned as a result of an exception.
  1274.  
  1275. Example:
  1276.  
  1277.     function DOT_PRODUCT(LEFT, RIGHT : VECTOR) return REAL is
  1278.        SUM : REAL := 0.0;
  1279.     begin
  1280.        CHECK(LEFT'FIRST = RIGHT'FIRST and LEFT'LAST = RIGHT'LAST);
  1281.        for J in LEFT'RANGE loop
  1282.           SUM := SUM + LEFT(J)*RIGHT(J);
  1283.        end loop;
  1284.        return SUM;
  1285.     end DOT_PRODUCT;
  1286.  
  1287. References:  exception 11, formal parameter  6.1,  function  6.1,  function
  1288. body  6.3,  function  call  6.4,  function  specification  6.1,  mode  6.1,
  1289. program_error exception 11.1, raising of exceptions  11,  return  statement
  1290. 5.8, statement 5
  1291.  
  1292.  
  1293.  
  1294.  
  1295. 6.6  Parameter and Result Type Profile - Overloading of Subprograms
  1296.  
  1297.  
  1298. Two  formal  parts  are said to have the same parameter type profile if and
  1299. only if they have the same number of  parameters,  and  at  each  parameter
  1300. position corresponding parameters have the same base type.  A subprogram or
  1301. entry  has the same parameter and result type profile as another subprogram
  1302. or entry if and only if both have the  same  parameter  type  profile,  and
  1303. either both are functions with the same result base type, or neither of the
  1304. two is a function.
  1305.  
  1306. The  same  subprogram  identifier or operator symbol can be used in several
  1307. subprogram specifications.  The identifier or operator symbol is then  said
  1308. to  be  overloaded;   the subprograms that have this identifier or operator
  1309. symbol are also said to be overloaded  and  to  overload  each  other.   As
  1310. explained  in  section  8.3, if two subprograms overload each other, one of
  1311. them can hide the other only if both subprograms have  the  same  parameter
  1312. and  result  type  profile (see section 8.3 for the other requirements that
  1313. must be met for hiding).
  1314.  
  1315.  
  1316.  
  1317.                                   6 - 19
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326. A call to an overloaded subprogram is ambiguous (and therefore illegal)  if
  1327. the  name  of  the  subprogram,  the  number of parameter associations, the
  1328. types and the order of the actual  parameters,  the  names  of  the  formal
  1329. parameters  (if  named  associations  are  used),  and the result type (for
  1330. functions)  are  not  sufficient  to  determine  exactly  one  (overloaded)
  1331. subprogram specification.
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.                                   6 - 20
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392. Examples of overloaded subprograms:
  1393.  
  1394.     procedure PUT(X : INTEGER);
  1395.     procedure PUT(X : STRING);
  1396.  
  1397.     procedure SET(TINT   : COLOR);
  1398.     procedure SET(SIGNAL : LIGHT);
  1399.  
  1400. Examples of calls:
  1401.  
  1402.     PUT(28);
  1403.     PUT("no possible ambiguity here");
  1404.  
  1405.     SET(TINT   => RED);
  1406.     SET(SIGNAL => RED);
  1407.     SET(COLOR'(RED));
  1408.  
  1409.     --  SET(RED) would be ambiguous since RED may
  1410.     --  denote a value either of type COLOR or of type LIGHT
  1411.  
  1412. Notes:
  1413.  
  1414. The  notion of parameter and result type profile does not include parameter
  1415. names, parameter modes, parameter subtypes, default expressions  and  their
  1416. presence or absence.
  1417.  
  1418. Ambiguities  may (but need not) arise when actual parameters of the call of
  1419. an  overloaded  subprogram  are  themselves  overloaded   function   calls,
  1420. literals,  or  aggregates.  Ambiguities may  also (but need not) arise when
  1421. several overloaded subprograms belonging to different packages are visible.
  1422. These ambiguities can usually  be  resolved  in  several  ways:   qualified
  1423. expressions  can  be  used  for  some or all actual parameters, and for the
  1424. result, if  any;   the  name  of  the  subprogram  can  be  expressed  more
  1425. explicitly as an expanded name;  finally, the subprogram can be renamed.
  1426.  
  1427. References:   actual parameter 6.4.1, aggregate 4.3, base type 3.3, default
  1428. expression for a formal parameter 6.1, entry  9.5,  formal  parameter  6.1,
  1429. function  6.5,  function call 6.4, hiding 8.3, identifier 2.3, illegal 1.6,
  1430. literal 4.2, mode 6.1, named parameter  association  6.4,  operator  symbol
  1431. 6.1,  overloading  8.7, package 7, parameter of a subprogram 6.2, qualified
  1432. expression 4.7, renaming declaration 8.5, result subtype 6.1, subprogram 6,
  1433. subprogram specification 6.1, subtype 3.3, type 3.3
  1434.  
  1435.  
  1436.  
  1437.  
  1438. 6.7  Overloading of Operators
  1439.  
  1440.  
  1441. The declaration of a function whose designator is  an  operator  symbol  is
  1442. used  to  overload an operator.  The sequence of characters of the operator
  1443. symbol must be either a logical, a relational, a  binary  adding,  a  unary
  1444. adding, a multiplying, or a highest precedence operator (see 4.5).  Neither
  1445. membership  tests  nor  the  short-circuit  control  forms  are  allowed as
  1446. function designators.
  1447.  
  1448.  
  1449.                                   6 - 21
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458. The subprogram specification  of  a  unary  operator  must  have  a  single
  1459. parameter.  The subprogram specification of a binary operator must have two
  1460. parameters;   for  each use of this operator, the first parameter takes the
  1461. left operand as actual parameter, the  second  parameter  takes  the  right
  1462. operand.   Similarly,  a generic function instantiation whose designator is
  1463. an operator symbol is only allowed if  the  specification  of  the  generic
  1464. function  has  the corresponding number of parameters.  Default expressions
  1465. are not allowed for the parameters of an operator (whether the operator  is
  1466. declared  with  an  explicit  subprogram  specification  or  by  a  generic
  1467. instantiation).
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.                                   6 - 22
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524. For each of the operators "+" and "-", overloading is  allowed  both  as  a
  1525. unary and as a binary operator.
  1526.  
  1527. The explicit declaration of a function that overloads the equality operator
  1528. "=",  other  than  by  a  renaming  declaration,  is  only  allowed if both
  1529. parameters are of the same limited type.  An overloading of  equality  must
  1530. deliver  a  result  of  the  predefined  type  BOOLEAN;  it also implicitly
  1531. overloads the inequality  operator  "/="  so  that  this  still  gives  the
  1532. complementary result to the equality operator.  Explicit overloading of the
  1533. inequality operator is not allowed.
  1534.  
  1535. A  renaming  declaration  whose designator is the equality operator is only
  1536. allowed to rename another equality operator.  (For example, such a renaming
  1537. declaration can be used when equality  is  visible  by  selection  but  not
  1538. directly visible.)
  1539.  
  1540. Note:
  1541.  
  1542. Overloading  of relational operators does not affect basic comparisons such
  1543. as testing for membership in a range or the choices in a case statement.
  1544.  
  1545. Examples:
  1546.  
  1547.     function "+" (LEFT, RIGHT : MATRIX) return MATRIX;
  1548.     function "+" (LEFT, RIGHT : VECTOR) return VECTOR;
  1549.  
  1550.     --  assuming that A, B, and C are of the type VECTOR
  1551.     --  the three following assignments are equivalent
  1552.  
  1553.     A := B + C;
  1554.  
  1555.     A := "+"(B, C);
  1556.     A := "+"(LEFT => B, RIGHT => C);
  1557.  
  1558. References:  allow 1.6, actual parameter 6.4.1, binary adding operator  4.5
  1559. 4.5.3,  boolean  predefined type 3.5.3, character 2.1, complementary result
  1560. 4.5.2, declaration 3.1, default expression  for  a  formal  parameter  6.1,
  1561. designator  6.1,  directly  visible  8.3,  equality  operator  4.5,  formal
  1562. parameter 6.1, function declaration 6.1, highest  precedence  operator  4.5
  1563. 4.5.6,  implicit  declaration  3.1, inequality operator 4.5.2, limited type
  1564. 7.4.4, logical operator 4.5 4.5.1, membership test 4.5  4.5.2,  multiplying
  1565. operator 4.5 4.5.5, operator 4.5, operator symbol 6.1, overloading 6.6 8.7,
  1566. relational  operator  4.5 4.5.2, short-circuit control form 4.5 4.5.1, type
  1567. definition 3.3.1, unary adding operator 4.5 4.5.4, visible by selection 8.3
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.                                   6 - 23
  1582.  
  1583.  
  1584.  
  1585.  
  1586.