Skip to content

Commit 752a63d

Browse files
Resolve "Break 070_subprograms into chapters"
1 parent e469162 commit 752a63d

13 files changed

+1283
-1275
lines changed

courses/fundamentals_of_ada/070_subprograms.rst

Lines changed: 47 additions & 1173 deletions
Large diffs are not rendered by default.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
==============
2+
Introduction
3+
==============
4+
5+
--------------
6+
Introduction
7+
--------------
8+
9+
* Are syntactically distinguished as :ada:`function` and :ada:`procedure`
10+
11+
- Functions represent *values*
12+
- Procedures represent *actions*
13+
14+
.. code:: Ada
15+
16+
function Is_Leaf (T : Tree) return Boolean
17+
procedure Split (T : in out Tree;
18+
Left : out Tree;
19+
Right : out Tree)
20+
21+
* Provide direct syntactic support for separation of specification from implementation
22+
23+
.. code:: Ada
24+
25+
function Is_Leaf (T : Tree) return Boolean;
26+
function Is_Leaf (T : Tree) return Boolean is
27+
begin
28+
...
29+
end Is_Leaf;
30+
31+
--------------------------------------
32+
Recognizing Procedures and Functions
33+
--------------------------------------
34+
35+
* Functions' results must be treated as values
36+
37+
- And cannot be ignored
38+
39+
* Procedures cannot be treated as values
40+
* You can always distinguish them via the call context
41+
42+
.. code:: Ada
43+
:number-lines: 10
44+
45+
Open (Source, "SomeFile.txt");
46+
while not End_of_File (Source) loop
47+
Get (Next_Char, From => Source);
48+
if Found (Next_Char, Within => Buffer) then
49+
Display (Next_Char);
50+
Increment;
51+
end if;
52+
end loop;
53+
54+
* Note that a subprogram without parameters (:ada:`Increment` on line 15) does not allow an empty set of parentheses
55+
56+
----------------------------------
57+
A Little "Preaching" About Names
58+
----------------------------------
59+
60+
* Procedures are abstractions for actions
61+
* Functions are abstractions for values
62+
* Use names that reflect those facts!
63+
64+
- Imperative verbs for procedure names
65+
- Nouns for function names, as for mathematical functions
66+
67+
+ Questions work for boolean functions
68+
69+
.. code:: Ada
70+
71+
procedure Open (V : in out Valve);
72+
procedure Close (V : in out Valve);
73+
function Square_Root (V: Float) return Float;
74+
function Is_Open (V: Valve) return Boolean;
75+
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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

Comments
 (0)