More Related Content
Similar to Ada 95 - Introduction (20)
More from Gneuromante canalada.org (8)
Ada 95 - Introduction
- 1. Franco Gasperoni
gasperon@act-europe.fr
http://libre.act-europe.fr
1
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 2. Copyright Notice
• © ACT Europe under the GNU Free Documentation License
• Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Free Documentation
License, Version 1.1 or any later version published by the Free
Software Foundation; provided its original author is mentioned
and the link to http://libre.act-europe.fr/ is kept at the bottom of
every non-title slide. A copy of the license is available at:
• http://www.fsf.org/licenses/fdl.html
2
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 4. http://www-inf.enst.fr/~domdl
• Programming in Ada 95
by John Barnes (Addison Wesley)
• Lovelace
On line Ada 95 tutorial
4
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 5. Course Objectives
• Building reliable software systems
• Comparing structured & OO software design
• Programming distributed systems
• Practice, practice, practice
5
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 6. Lectures
•• Introduction to Ada 95
Introduction to Ada 95
•• Building reliable SW systems
Building reliable SW systems 3+3 h
•• Structured design
Structured design
•• TP 1
TP 1
•• OO design
OO design
•• Java
Java
4.5+3 h •• TP 2
TP 2
•• Distributed programming
Distributed programming
•• TP 3
TP 3 3+3 h
6
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 7. Course Evaluation
• TP OO: 6 points
• TP distributed programming: 4 points
7
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 8. • Background on Ada 95
• Programming with Ada 95
8
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 9. 1950 1960 1970 1980 1990 2000
PL/I(66)
Ada(95)
Ada(83)
Cobol(58)
A S S E M B L Y
Pascal(70)
Java(96)
Algol(60)
C(72) C++(89)
Fortran(54)
Simula(67) Smalltalk(80)
imperative
Basic(66)
9
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 10. Software Crisis: 1976
• Many languages (>450)
– Many dialects
• Low interoperability
• High maintenance costs
• Low reliability
10
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 11. The 70s - Mid 80s
• Biggest SW contractor
• SW with long life
cycles (10-30 years)
• Safety critical apps
11
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 12. Safety Critical
Is 99.9% acceptable for defects?
• Drinking water contaminated for 1 h/month
• 2 accidents/month at O’Hare International Airport
• 22,000 checks drawn from the wrong account / hour
12
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 13. Ada
• 1975 study: No language was adequate for
safety critical applications
• 1976-80: International competition -> Ada
• 1983: Ada made an ISO standard
– No dialects allowed
– Formal compiler validation procedure (> 4,000 tests)
• 1995: Ada ISO standard brought up to date
13
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 14. Ada goals:
– Reliability & maintainability
– Large, long-lasting, complex projects
– Standardization & validation
In 1998, still nothing else exists!
14
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 15. Average Annual Costs for
Software Maintenance
1800
1600
1000s of 1994 Dollars
Ada 270,000 LOC
1400
Other HOLs
1200 C 225,000 LOC
1000 135,000 LOC
800 150,000 LOC
600 112,500 LOC
400
75,000 LOC
200
0
350 700 1,050 1,400 1,750 2,100
Function Points
Source: MITRE (Avionics domain) 15
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 16. Industrial Applications in Ada
• Weirton Steel - process controller
• Volvo manufacturing plant
• Orson & Prompt - videotape applications
• Astree -French developed, Europe-wide railroad operation
• Helsinki Radiotelescope
• CANAL+ - decoder boxes
• BNP - trading systems
• Flight Warning System - Airbus A340
• Boeing 777
16
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 17. • Background on Ada 95
• Programming with Ada 95
17
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 18. The C programming style:
Conventions & NO Verification
void main () void main ()
void main ()
void main ()
{{ {{
printf (“Hello world.n”); printf (12345);
printf (12345);
printf (“Hello world.n”);
}} }}
18
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 19. Even with ...
#include <stdio.h>
#include <stdio.h>
void main ()
void main ()
{{
printf (“Hello world in Cn”);
printf (“Hello world in Cn”);
}}
• NO guarantee you will link with
– the correct library
– the good version of the correct library
19
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 20. hello.adb
hello.adb
with Text_IO;
with Text_IO;
procedure Hello is
procedure Hello is
begin
begin
Text_IO.Put_Line (“Hello world in Ada”);
Text_IO.Put_Line (“Hello world in Ada”);
end Hello;
end Hello;
hello.c
hello.c
#include <stdio.h>
#include <stdio.h>
void main ()
void main ()
{{
printf (“Hello world in Cn”);
printf (“Hello world in Cn”);
}}
20
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 21. Predefined Ada library
hello.adb
hello.adb subprogram in library
with Text_IO;
with Text_IO;
procedure Hello is
procedure Hello is
begin
begin
Text_IO.Put_Line (“Hello world in Ada”);
Text_IO.Put_Line (“Hello world in Ada”);
end Hello;
end Hello;
% gnatmake -q hello
% gnatmake -q hello
% hello
% hello
Hello world in Ada
Hello world in Ada Checks you are using
%
% the correct version
of every module & library
21
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 22. hello.adb
hello.adb
with Text_IO; use Text_IO;
with Text_IO; use Text_IO;
procedure Hello is
procedure Hello is
begin
begin
Text_IO.Put_Line (“Hello world in Ada”);
Text_IO.Put_Line (“Hello world in Ada”);
end Hello;
end Hello;
22
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 23. hello.adb
hello.adb
with Text_IO;
with Text_IO;
procedure Hello is
procedure Hello is
begin
begin
Text_IO.Put_Line (“Hello world in Ada”);
Text_IO.Put_Line (“Hello world in Ada”);
end Hello;
end Hello;
%gnatmake -q two_hello
%gnatmake -q two_hello
% two_hello
% two_hello
two_hello.adb
two_hello.adb Hello world in Ada
Hello world in Ada
Hello world in Ada
Hello world in Ada
with Hello;
with Hello; %
%
procedure Two_Hello is
procedure Two_Hello is
begin
begin
Hello;
Hello;
Hello;
Hello;
end Two_Hello;
end Two_Hello;ACT Europe under the GNU Free Documentation License 23
http://libre.act-europe.fr ©
- 24. with Text_IO; use Text_IO;
with Text_IO; use Text_IO; function Fact (N ::Integer) return Integer is
function Fact (N Integer) return Integer is
procedure Hello is
procedure Hello is begin
begin
begin
begin if N <= 1 then
if N <= 1 then
Put_Line (“Hello”);
Put_Line (“Hello”); return 1;
return 1;
end Hello;
end Hello; else
hello.adb else
return N **Fact (N-1);
return N Fact (N-1);
end if;
end if;
end Fact;
end Fact;
fact.adb
with Hello;
with Hello;
with Fact;
with Fact;
procedure Main is
procedure Main is
begin
begin
for IIin 1 .. Fact (4) loop
for in 1 .. Fact (4) loop
Hello;
Hello;
end loop;
end loop;
end Main;
end Main;
main.adb 24
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 25. General Structure
Ada Library Ada Library
with ...
with ...
with ...
with ...
with ...
with ...
procedure Some_Main is
procedure Some_Main is
begin
begin
….
….
end Some_Main;
end Some_Main; 25
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 26. • Programming with Ada 95
– Scalar data types
– Checks & Exceptions
– Access types (pointers)
– Arrays
– Records
– Parameter passing
26
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 27. Elementary Ada types
• Scalar
– Discrete
• integer (Integer)
• enumeration (Boolean, Character)
– Real
• floating point (Float)
• Access (pointers)
27
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 28. Integer types
function Compute (P, Q : Integer) return Integer is
R : Integer;
M : Integer := 2 * P;
begin
R := Q / M;
return R;
end Compute;
28
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 29. Introduces a new type
function Compute (P, Q : Integer) return Integer is
type My_Int is range -100 .. 1_000_000;
T : My_Int; Compilation ERROR
Ada is strongly typed
begin
T := P + 1;
T := My_Int (P) + 1; Explicit conversion
needed
return Integer (T) + Q;
end Compute;
29
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 30. Enumeration types
procedure Compute (A : Character; B : Boolean) is
type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
D : Day := Wed;
C : Character := ‘W’;
Week_Day : Boolean := D in Mon .. Fri;
Lower_Case : Boolean := A in ‘a’ .. ‘z’;
begin
Week_Day := Week_Day or B;
end Compute; 30
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 31. Real types
procedure Compute (M : Integer) is
Pi : constant := 3.141;
F : constant Float := Float (M);
R : Float := F * Pi; Explicit conversions
needed
A : Integer := Integer (R);
begin
null;
end Compute;
31
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 32. Type Attributes
• TYPE ’ First : smallest value in TYPE
• TYPE ’ Last : biggest value in TYPE
• TYPE ’ Image (X) : String representation of X
(X in TYPE)
32
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 33. with Text_IO;
procedure Print (A : Integer; P : Float) is
type My_Int is range -100 .. 1_000_000;
T : My_Int := My_Int ’ Last;
type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
D : Day := Day ’ First;
B : Integer := Integer ’ First;
begin
Text_IO.Put (Integer ’ Image (A));
Text_IO.Put (Float ’ Image (P));
end Compute;
33
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 34. • Programming with Ada 95
– Scalar data types
– Checks & Exceptions
– Access types (pointers)
– Arrays
– Records
– Parameter passing
34
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 35. Overflow in C
#include <limits>
void main
{
int k = INT_MAX;
k = k + 1;
} Semantics
undefined
35
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 36. Potential problems ...
void do_something (int m, int n)
{
int k;
for (k = m; k <= n; k++)
{
/* Do something */
...
}
}
• Reliability: may loop forever
• Portability: may only arise on certain platforms
(16 / 32 / 64 bit architecture) 36
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 37. Overflow & Constraint Checks
procedure Checks is
K : Integer := Integer ’ Last;
begin exception
K := K + 1; Constraint_Error
end Checks; raised
during execution
% gnatmake -q checks
% checks
raised CONSTRAINT_ERROR
%
37
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 38. procedure Checks (X : Integer) is
type My_Int is range -100 .. 1_000_000;
T : My_Int;
Constraint_Error
begin raised
T := My_Int (X); if X not in
end Checks; -100 .. -1_000_000
38
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 39. Subtypes
type Day_Of_A_Month is range 1 .. 31;
type Day_Of_February is range 1 .. 28;
Compilation ERROR
D1 : Day_Of_February := 25; Ada is strongly typed
D2 : Day_Of_A_Month := D1;
D2 : Day_Of_A_Month := Day_Of_A_Month (D1);
OK, but tedious
39
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 40. type Day_Of_A_Month is range 1 .. 31;
subtype Day_Of_February is Day_Of_A_Month range 1 .. 28;
D1 : Day_Of_February := 25;
D2 : Day_Of_A_Month := D1; OK
Constraint_Error
D3 : Day_Of_A_Month;
raised if
D3 not in 1 .. 28
D4 : Day_Of_February := D3;
40
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 41. Predefined Subtypes
subtype Natural is Integer range 0 .. Integer ’ Last;
subtype Positive is Natural range 1 .. Natural ’ Last;
41
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 42. Exceptions
procedure Checks is
A : Integer := Integer ’ First;
exception
Constraint_Error
begin
raised
A := A - 1;
during execution
end Checks;
% gnatmake -q checks
% checks
raised CONSTRAINT_ERROR
%
42
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 43. Predefined Exceptions
• Constraint_Error: overflow, computation error
(divide by zero), array index out of range, …
• Storage_Error: no more memory available
• Program_Error: fundamental program error (e.g.
end of function with no return statement)
43
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 44. Catching an Exception
with Text_IO; use Text_IO;
procedure Checks is
A : Integer := Integer ’ First;
begin
A := A - 1;
exception
when Constraint_Error =>
Put_Line (“Overflow occurred”);
end Checks;
% gnatmake -q checks
% checks
Overflow occurred
%
44
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 45. procedure Checks is
Internal_Error : exception;
Creating procedure Foo is
begin
your own raise Internal_Error;
exceptions end Foo;
procedure Bar is
begin
Foo;
end Bar;
begin
Bar;
exception
Exception Handler
...
45
http://libre.act-europe.fr end Checks;
© ACT Europe under the GNU Free Documentation License
- 46. procedure Foo is
begin
raise Internal_Error;
end Foo;
procedure Bar is
begin
Foo;
end Bar;
begin
Bar;
exception
when Internal_Error =>
Put_Line (“problem occurred”);
Exception Handler
when others =>
Put_Line (“some other exception”);
end Checks; 46
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 47. procedure Foo is
begin
raise Internal_Error;
1
end Foo;
procedure Bar is 2
begin
Foo;
3
end Bar;
begin 4
Bar;
5
exception
when Internal_Error =>
Put_Line (“problem occurred”);
Exception Handler
when others =>
Put_Line (“some other exception”);
end Checks; 47
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 48. procedure Foo is
begin
raise Internal_Error;
1
end Foo;
procedure Bar is 2
begin
Foo;
3 end Bar;
begin 4
Bar;
5
exception
when Internal_Error =>
Put_Line (“problem occurred”);
when others =>
Put_Line (“some other exception”);
end Checks; 48
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 49. Catching an exception
where YOU want
procedure Checks is
...
begin
} Want to catch some
exception in a region
of code without exiting
from the subprogram
49
http://libre.act-europe.fr Checks;
end © ACT Europe under the GNU Free Documentation License
- 50. procedure Checks is
...
Use begin
declare
Blocks Some_Label : declare
Declarations
begin
Statements
exception
Handler
end Some_Label;
end Checks; 50
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 51. procedure Calc (A, B : Float) is
C, D : Float;
Example begin
declare
Old_C : Float := C;
begin
C := A * B;
D := C ** 2;
exception
when Constraint_Error =>
C := Old_C;
D := 0.0;
end;
51
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 52. • Programming with Ada 95
– Scalar data types
– Checks & Exceptions
– Access types (pointers)
– Arrays
– Records
– Parameter passing
52
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 53. Access Types
type Int_Ptr is access Integer;
P : Int_Ptr;
pointers
pointers
are initialized
are initialized
to null
to null
by default
by default
Memory
53
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 54. type Int_Ptr is access Integer;
P : Int_Ptr;
P
P := new Integer; ???
Memory
54
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 55. type Int_Ptr is access Integer;
P : Int_Ptr;
P
P := new Integer; 3334
P.all := 3344;
Memory
55
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 56. type Int_Ptr is access Integer;
P : Int_Ptr;
P
P := new Integer ’ (1234); 1234
Memory
56
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 57. type Int_Ptr is access Integer;
P : Int_Ptr;
P
X : Integer := 567;
567
P := new Integer ’ (X);
Memory
57
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 58. type Int_Ptr is access Integer;
type Another_Int_Ptr is access Integer;
P : Int_Ptr := new Integer;
567
Q : Another_Int_Ptr;
COMPILATION ERROR
Q := Another_Int_Ptr (P);
you must use
general access types
to perform pointer
conversions
58
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 59. General Access Types
type Ptr_Int is access all Integer;
P : Ptr_Int;
Y : aliased Integer := 9999;
9999
Y
Memory
59
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 60. P
type Ptr_Int is access all Integer;
P : Ptr_Int;
Y : aliased Integer := 9999;
9999
P := Y ’ access;
Y
Memory
60
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 61. P
type Ptr_Int is access all Integer;
P : Ptr_Int;
Y : aliased Integer := 9999;
1234
P := Y ’ access;
Y
P.all := 1234;
Memory
61
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 62. type Ptr_Int is access all Integer;
P : Ptr_Int;
X : Integer; compilation
error
P := X ’ access; X is not
aliased
62
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 63. type Int_Ptr is access all Integer;
type Another_Int_Ptr is access all Integer;
P : Int_Ptr := new Integer;
Q : Another_Int_Ptr;
Q := Another_Int_Ptr (P);
OK
63
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 64. • Programming with Ada 95
– Scalar data types
– Checks & Exceptions
– Access types (pointers)
– Arrays
– Records
– Parameter passing
64
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 65. Composite Ada types
• array (String)
• record
• tagged record
• protected types
• tasks
65
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 66. One of a Kind Arrays
procedure Compute (N : Integer) is
A : array (1 .. N) of Float; Arrays can have
- dynamic bounds
begin - dynamic size
for I in 1 .. N loop
A (I) := 3.141;
end loop;
end Compute;
66
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 67. Typed Arrays
procedure Compute (N : Integer) is
type Arr is array (Integer range <>) of Float;
A : Arr (1 .. N);
B takes its bounds from A
B : Arr := A;
C : Arr (11 .. 20);
begin
Constraint_Error
C := A
if C’Length /= A’Length
C (15 .. 18) := A (5 .. 8);
Constraint_Error
if A’Last < 8 67
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 68. A : array (10 .. 20) of Float;
B : array (10 .. 20) of Float;
A := B;
Compilation error
A and B are
one of a kind
68
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 69. 1-Dim Array Attributes
• ARRAY ’ First : smallest index value in ARRAY
• ARRAY ’ Last : biggest index value in ARRAY
• ARRAY ’ Length : # of elements in ARRAY
• ARRAY ’ range : ARRAY ’ First .. ARRAY ’ Last
69
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 70. type Vector is array (Natural range <>) of Float;
function Max (V : Vector) return Float is
M : Float := Float ’ First;
begin
for I in V ’ range loop
if V (I) > M then
M := V (I);
end if;
end loop;
return M;
end Max;
70
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 71. type Vector is array (Natural range <>) of Float;
function Max (V : Vector) return Float;
V1 : Vector := (0.0, 1.0, 2.0, 3.0, 4.0, 5.0);
V1’First = 0
V1’Last = 6
V1’Length = 7
V2 : Vector (1 .. 100) := (1.0, 2.0, others => 99.0);
V2’First = 1
X : Float := Max (V1); V2’Last = 100
Y : Float := Max (V2); V2’Length = 100
71
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 72. Predefined Array Type
type String is array (Positive range <>) of Character;
R : String (1 .. 10);
S : String := (‘H’, ‘e’, ‘l’, ‘l’, ‘o’);
T : String := “Hello”;
Q : String := S & “ “ &T “ you”; Q = “Hello Hello You”
72
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 73. • Programming with Ada 95
– Scalar data types
– Checks & Exceptions
– Access types (pointers)
– Arrays
– Records
– Parameter passing
73
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 74. Record Types
type Date is record
Day : Positive range 1 .. 31;
Month : Positive range 1 .. 12;
Year : Integer;
end record;
D : Date := (3, 9, 1975);
A : Date := (Day => 31, Month => 12, Year => 1999);
B : Date := A;
Y : Integer := B . Year;
74
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 75. P2
type Node; 3
type Node_Ptr is access Node; 9
1975
type Node is record
D : Date := (1, 1, 1900);
Next : Node_Ptr;
end record; 1
P1 1
P1 : Node_Ptr := new Node; 1900
null
P2 : Node_Ptr := new Node ’ ((3, 9, 1975), P1); Memory
75
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 76. N : Node := ((31, 12, 1999), null);
P3 : Node_Ptr := new Node ’ (N);
31
P3 12
1999
null
Memory
76
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 77. Record fields: simple rule
•P pointer to a record
• P.all points to the WHOLE record
• P.all.Field points to Field in the record
• P.Field same as P.all.Field
77
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 78. type Node is record
D : Date := (1, 1, 1900);
Next : Node_Ptr;
end record;
P : Node_Ptr := new Node;
DD : Date := P.D;
NN : Node_Ptr := P.Next;
78
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 79. Parametrizing Records:
Discriminants
type Q_array (Natural range <>) of Integer;
type Queue (Max_Size : Natural) is record
First : Natural;
Last : Natural;
Size : Natural;
Q : Q_Array (0 .. Max_Size);
end record;
79
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 80. type Q_Array (Positive range <>) of Integer;
type Queue (Max_Size : Positive) is record
First : Positive := 1;
Last : Positive := 1;
Size : Natural := 0;
Q : Q_Array (1 .. Max_Size);
end record;
X : Queue (4); X.Max_Size = 4
80
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 81. X : Queue :=(Max_Size => 4,
First => 2,
Last => 3,
Size => 2,
Q => (0, 11, 22, 0));
X : Queue; X.Max_Size = ???
Compilation error
Queue is an
unconstrained type
must specify
discriminant value
81
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 82. • Programming with Ada 95
– Scalar data types
– Checks & Exceptions
– Access types (pointers)
– Arrays
– Records
– Parameter passing
82
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 83. Parameter Passing
• in (functions & procedures)
• in out (procedures only)
• out (procedures only)
83
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 84. in parameters are
copied IN during a subprogram call
function Inc (X : Integer) return Integer is
begin
X := X + 1;
return X;
Compilation error
end Inc;
X is like a
constant
inside Inc
84
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 85. in out parameters are
copied IN during a subprogram call
and copied OUT upon return
procedure Inc (X : in out Integer) is
begin
X := X + 1; X is a regular
end Inc; variable
inside Inc
Val : Integer := 3;
Inc (Val);
-- here Val = 4
85
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 86. out parameters are
copied OUT upon return
procedure Random (X : out Integer) is
begin
-- compute random number X is a regular
X := …; variable inside Inc
end Random; without initial value
Val : Integer;
Random (Val);
86
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
- 87. Control Structures
• if-then-else
• case statements
• loops
– for
– while
– generic
87
http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License