procedure PUSH(E : in ELEMENT; S : in out STACK) is -- local quantities could be declared here 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; |
However, Ada allows the subprogram declaration to be separated from the subprogram body. For example, the subprogram declaration
procedure PUSH(E : in ELEMENT; S : in out STACK);
may appear grouped with other subprogram, variable, constant, and type declarations in a given declarative part, while its body may appear later, in the list of bodies of the declarative part.
The declaration consists of the subprogram specification followed by a semicolon.
The main reason for permitting such separation is readability. If the body and the specification appear together (as in Algol 60), the potentially large body is mixed with the smaller interface specification. The specification provides the information needed to call the subprogram; it may be hard for the reader to find, especially when examining a program with a large number of subprograms spread over several pages of text. In addition, an isolated variable declaration between two large subprograms is a well-known source of error in Algol 60 (the neglected variable may hide an outer variable that is in consequence never used - see 3.2).
These inconveniences are avoided in Ada. All bodies must be grouped at the end of a declarative part without any variable declaration in between them. Furthermore the user is provided with the ability to regroup subprogram declarations within a small space of text, so as to provide an immediate overview of all subprograms that are local to a given program unit. The split of the subprogram declaration from its body is merely a convenience for large subprograms; but it is a necessity for subprograms declared in the visible part of a package, and for subprograms that are mutually recursive. However, requiring a split in all cases, including small subprograms, would add only verbosity without compensating advantages. In Ada, the decision to split is therefore left to the programmer, except in the cases just mentioned where it is necessary.
Although this decision is left to the programmer, no semantic problems are involved since the information provided by the subprogram declaration is repeated in full in the subprogram body, and the two specifications must agree.