|
| 1 | +======== |
| 2 | +Syntax |
| 3 | +======== |
| 4 | + |
| 5 | +------------------------- |
| 6 | + Specification and Body |
| 7 | +------------------------- |
| 8 | + |
| 9 | +* Subprogram specification is the external (user) **interface** |
| 10 | + |
| 11 | + - **Declaration** and **specification** are used synonymously |
| 12 | + |
| 13 | +* Specification may be required in some cases |
| 14 | + |
| 15 | + - eg. recursion |
| 16 | + |
| 17 | +* Subprogram body is the **implementation** |
| 18 | + |
| 19 | +------------------------------------------- |
| 20 | +Procedure Specification Syntax (Simplified) |
| 21 | +------------------------------------------- |
| 22 | + |
| 23 | +.. code:: Ada |
| 24 | +
|
| 25 | + procedure Swap (A, B : in out Integer); |
| 26 | +
|
| 27 | +.. code:: Ada |
| 28 | +
|
| 29 | + procedure_specification ::= |
| 30 | + procedure program_unit_name |
| 31 | + { (parameter_specification |
| 32 | + ; parameter_specification)}; |
| 33 | +
|
| 34 | + parameter_specification ::= |
| 35 | + identifier_list : mode subtype_mark [ := expression ] |
| 36 | +
|
| 37 | + mode ::= [in] | out | in out |
| 38 | +
|
| 39 | +------------------------------------------ |
| 40 | +Function Specification Syntax (Simplified) |
| 41 | +------------------------------------------ |
| 42 | + |
| 43 | +.. code:: Ada |
| 44 | +
|
| 45 | + function F (X : Float) return Float; |
| 46 | +
|
| 47 | +* Close to :ada:`procedure` specification syntax |
| 48 | + |
| 49 | + + With :ada:`return` |
| 50 | + + Can be an operator: :ada:`+ - * / mod rem` ... |
| 51 | + |
| 52 | +.. container:: latex_environment footnotesize |
| 53 | + |
| 54 | + .. code:: Ada |
| 55 | +
|
| 56 | + function_specification ::= |
| 57 | + function designator |
| 58 | + { (parameter_specification |
| 59 | + ; parameter_specification) } |
| 60 | + return result_type; |
| 61 | +
|
| 62 | + designator ::= program_unit_name | operator_symbol |
| 63 | +
|
| 64 | +------------- |
| 65 | +Body Syntax |
| 66 | +------------- |
| 67 | + |
| 68 | +.. code:: Ada |
| 69 | +
|
| 70 | + subprogram_specification is |
| 71 | + [declarations] |
| 72 | + begin |
| 73 | + sequence_of_statements |
| 74 | + end [designator]; |
| 75 | +
|
| 76 | +.. code:: Ada |
| 77 | +
|
| 78 | + procedure Hello is |
| 79 | + begin |
| 80 | + Ada.Text_IO.Put_Line ("Hello World!"); |
| 81 | + Ada.Text_IO.New_Line (2); |
| 82 | + end Hello; |
| 83 | +
|
| 84 | + function F (X : Float) return Float is |
| 85 | + Y : constant Float := X + 3.0; |
| 86 | + begin |
| 87 | + return X * Y; |
| 88 | + end F; |
| 89 | +
|
| 90 | +-------------- |
| 91 | +Completions |
| 92 | +-------------- |
| 93 | + |
| 94 | +* Bodies **complete** the specification |
| 95 | + |
| 96 | + - There are **other** ways to complete |
| 97 | + |
| 98 | +* Separate specification is **not required** |
| 99 | + |
| 100 | + - Body can act as a specification |
| 101 | + |
| 102 | +* A declaration and its body must **fully** conform |
| 103 | + |
| 104 | + - Mostly **semantic** check |
| 105 | + - But parameters **must** have same name |
| 106 | + |
| 107 | +.. code:: Ada |
| 108 | +
|
| 109 | + procedure P (J, K : Integer) |
| 110 | + procedure P (J : Integer; K : Integer) |
| 111 | + procedure P (J, K : in Integer) |
| 112 | + -- Invalid |
| 113 | + procedure P (A : Integer; B : Integer) |
| 114 | +
|
| 115 | +--------------------- |
| 116 | +Completion Examples |
| 117 | +--------------------- |
| 118 | + |
| 119 | +* Specifications |
| 120 | + |
| 121 | + .. code:: Ada |
| 122 | +
|
| 123 | + procedure Swap (A, B : in out Integer); |
| 124 | + function Min (X, Y : Person) return Person; |
| 125 | +
|
| 126 | +* Completions |
| 127 | + |
| 128 | + .. code:: Ada |
| 129 | +
|
| 130 | + procedure Swap (A, B : in out Integer) is |
| 131 | + Temp : Integer := A; |
| 132 | + begin |
| 133 | + A := B; |
| 134 | + B := Temp; |
| 135 | + end Swap; |
| 136 | +
|
| 137 | + -- Completion as specification |
| 138 | + function Less_Than (X, Y : Person) return Boolean is |
| 139 | + begin |
| 140 | + return X.Age < Y.Age; |
| 141 | + end Less_Than; |
| 142 | +
|
| 143 | + function Min (X, Y : Person) return Person is |
| 144 | + begin |
| 145 | + if Less_Than (X, Y) then |
| 146 | + return X; |
| 147 | + else |
| 148 | + return Y; |
| 149 | + end if; |
| 150 | + end Min; |
| 151 | +
|
| 152 | +------------------------------------------ |
| 153 | +Direct Recursion - No Declaration Needed |
| 154 | +------------------------------------------ |
| 155 | + |
| 156 | +* When :ada:`is` is reached, the subprogram becomes **visible** |
| 157 | + |
| 158 | + - It can call **itself** without a declaration |
| 159 | + |
| 160 | +.. code:: Ada |
| 161 | +
|
| 162 | + type Vector_T is array (Natural range <>) of Integer; |
| 163 | + Empty_Vector : constant Vector_T (1 .. 0) := (others => 0); |
| 164 | +
|
| 165 | + function Get_Vector return Vector_T is |
| 166 | + Next : Integer; |
| 167 | + begin |
| 168 | + Get (Next); |
| 169 | +
|
| 170 | + if Next = 0 then |
| 171 | + return Empty_Vector; |
| 172 | + else |
| 173 | + return Get_Vector & Next; |
| 174 | + end if; |
| 175 | + end Input; |
| 176 | +
|
| 177 | +---------------------------- |
| 178 | +Indirect Recursion Example |
| 179 | +---------------------------- |
| 180 | + |
| 181 | +* Elaboration in **linear order** |
| 182 | + |
| 183 | +.. code:: Ada |
| 184 | +
|
| 185 | + procedure P; |
| 186 | +
|
| 187 | + procedure F is |
| 188 | + begin |
| 189 | + P; |
| 190 | + end F; |
| 191 | +
|
| 192 | + procedure P is |
| 193 | + begin |
| 194 | + F; |
| 195 | + end P; |
| 196 | +
|
| 197 | +------ |
| 198 | +Quiz |
| 199 | +------ |
| 200 | + |
| 201 | +Which profile is semantically different from the others? |
| 202 | + |
| 203 | + A. ``procedure P (A : Integer; B : Integer);`` |
| 204 | + B. ``procedure P (A, B : Integer);`` |
| 205 | + C. :answermono:`procedure P (B : Integer; A : Integer);` |
| 206 | + D. ``procedure P (A : in Integer; B : in Integer);`` |
| 207 | + |
| 208 | +.. container:: animate |
| 209 | + |
| 210 | + Parameter names are important in Ada. The other selections have |
| 211 | + the names in the same order with the same mode and type. |
| 212 | + |
0 commit comments