Upload
gneuromante-canaladaorg
View
1.210
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Author: Franco Gasperoni License: GFDL
Citation preview
1http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Franco [email protected]://libre.act-europe.fr
2http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Copyright NoticeCopyright 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
3http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
4http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
http://wwwhttp://www--inf.inf.enstenst..frfr/~/~domdldomdl
• Programming in Ada 95by John Barnes (Addison Wesley)
• LovelaceOn line Ada 95 tutorial
5http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Course ObjectivesCourse Objectives
• Building reliable software systems
• Comparing structured & OO software design
• Programming distributed systems
• Practice, practice, practice
6http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
LecturesLectures• Introduction to Ada 95• Building reliable SW systems• Structured design• TP 1
• Introduction to Ada 95• Building reliable SW systems• Structured design• TP 1
• OO design• Java• TP 2
• OO design• Java• TP 2
• Distributed programming• TP 3• Distributed programming• TP 3
3+3 h
4.5+3 h
3+3 h
7http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Course EvaluationCourse Evaluation
• TP OO: 6 points
• TP distributed programming: 4 points
8http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Background on Ada 95
• Programming with Ada 95
9http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
1950 1960 1970 1980 1990 2000
Fortran(54)
PL/I(66)
Ada(83)
Ada(95)
Java(96)
Basic(66)
C(72)
Pascal(70)
Cobol(58)
Algol(60)
Simula(67) Smalltalk(80)
C++(89)
imperative
A S
S E
M B
L Y
10http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Software Crisis: 1976Software Crisis: 1976
• Many languages (>450)– Many dialects
• Low interoperability• High maintenance costs• Low reliability
11http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
The 70s The 70s -- Mid 80sMid 80s
• Biggest SW contractor
• SW with long life
cycles (10-30 years)
• Safety critical apps
12http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Safety Critical Safety Critical
• Drinking water contaminated for 1 h/month
• 2 accidents/month at O’Hare International Airport
• 22,000 checks drawn from the wrong account / hour
Is 99.9% acceptable for defects?
13http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
AdaAda
• 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
14http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Ada goals:
– Reliability & maintainability
– Large, long-lasting, complex projects
– Standardization & validation
In 1998, still nothing else exists!
15http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
0000
200200200200
400400400400
600600600600
800800800800
1000100010001000
1200120012001200
1400140014001400
1600160016001600
1800180018001800
350350350350 700700700700 1,0501,0501,0501,050 1,4001,4001,4001,400 1,7501,7501,7501,750 2,1002,1002,1002,100
Function PointsFunction PointsFunction PointsFunction Points
10
00
s o
f 1
99
4 D
olla
rs1
00
0s
of
19
94
Do
llars
10
00
s o
f 1
99
4 D
olla
rs1
00
0s
of
19
94
Do
llars AdaAdaAdaAda
Other HOLsOther HOLsOther HOLsOther HOLsCCCC
Source: MITRE (Avionics domain)
270,000 LOC
225,000 LOC
150,000 LOC
135,000 LOC
112,500 LOC
75,000 LOC
Average Annual Costs for Average Annual Costs for Software MaintenanceSoftware Maintenance
16http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Industrial Applications in AdaIndustrial 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
17http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Background on Ada 95
• Programming with Ada 95
18http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
The C programming style:The C programming style:Conventions Conventions && NO VerificationNO Verification
void main (){
printf (“Hello world.\n”);}
void main (){
printf (“Hello world.\n”);}
void main (){
printf (12345); }
void main (){
printf (12345); }
19http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
#include <stdio.h>
void main (){
printf (“Hello world in C\n”);}
#include <stdio.h>
void main (){
printf (“Hello world in C\n”);}
• NO guarantee you will link with – the correct library– the good version of the correct library
Even with ...Even with ...
20http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
with Text_IO;
procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
with Text_IO;
procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
hello.adbhello.adb
#include <stdio.h>
void main (){
printf (“Hello world in C\n”);}
#include <stdio.h>
void main (){
printf (“Hello world in C\n”);}
hello.chello.c
21http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
with Text_IO;
procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
with Text_IO;
procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
hello.adbhello.adb
% gnatmake -q hello% helloHello world in Ada%
% gnatmake -q hello% helloHello world in Ada%
Predefined Ada library
subprogram in library
Checks you are usingthe correct version of every module & library
22http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
with Text_IO; use Text_IO;
procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
with Text_IO; use Text_IO;
procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
hello.adbhello.adb
23http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
with Hello;procedure Two_Hello isbegin
Hello;Hello;
end Two_Hello;
with Hello;procedure Two_Hello isbegin
Hello;Hello;
end Two_Hello;
two_hello.adbtwo_hello.adb
with Text_IO;procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
with Text_IO;procedure Hello isbegin
Text_IO.Put_Line (“Hello world in Ada”);end Hello;
hello.adbhello.adb
%gnatmake -q two_hello% two_helloHello world in AdaHello world in Ada%
%gnatmake -q two_hello% two_helloHello world in AdaHello world in Ada%
24http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
with Hello;with Fact;procedure Main isbegin
for I in 1 .. Fact (4) loopHello;
end loop;end Main;
with Hello;with Fact;procedure Main isbegin
for I in 1 .. Fact (4) loopHello;
end loop;end Main; main.adb
with Text_IO; use Text_IO;procedure Hello isbegin
Put_Line (“Hello”);end Hello;
with Text_IO; use Text_IO;procedure Hello isbegin
Put_Line (“Hello”);end Hello; hello.adb
function Fact (N : Integer) return Integer isbegin
if N <= 1 thenreturn 1;
elsereturn N * Fact (N-1);
end if;end Fact;
function Fact (N : Integer) return Integer isbegin
if N <= 1 thenreturn 1;
elsereturn N * Fact (N-1);
end if;end Fact; fact.adb
25http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
General StructureGeneral Structure
with ...procedure Some_Main isbegin
….end Some_Main;
with ...procedure Some_Main isbegin
….end Some_Main;
with ...
with ...
with ...
Ada Library
with ...
Ada Library
26http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95– Scalar data types– Checks & Exceptions– Access types (pointers)– Arrays– Records– Parameter passing
27http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Elementary Ada typesElementary Ada types
• Scalar– Discrete
• integer (Integer)• enumeration (Boolean, Character)
– Real• floating point (Float)
• Access (pointers)
28http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Integer typesInteger types
function Compute (P, Q : Integer) return Integer isR : Integer;M : Integer := 2 * P;
beginR := Q / M;return R;
end Compute;
29http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
function Compute (P, Q : Integer) return Integer is
type My_Int is range -100 .. 1_000_000;
T : My_Int;
beginT := P + 1;
T := My_Int (P) + 1;
return Integer (T) + Q;end Compute;
Compilation ERRORAda is strongly typed
Introduces a new type
Explicit conversionneeded
30http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Enumeration typesEnumeration 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’;
beginWeek_Day := Week_Day or B;
end Compute;
31http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Real typesReal types
procedure Compute (M : Integer) isPi : constant := 3.141;
F : constant Float := Float (M);
R : Float := F * Pi;
A : Integer := Integer (R);
beginnull;
end Compute;
Explicit conversionsneeded
32http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Type AttributesType Attributes
• TYPE ’ First : smallest value in TYPE
• TYPE ’ Last : biggest value in TYPE
• TYPE ’ Image (X) : String representation of X(X in TYPE)
33http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
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;
beginText_IO.Put (Integer ’ Image (A));Text_IO.Put (Float ’ Image (P));
end Compute;
34http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95– Scalar data types
– Checks & Exceptions– Access types (pointers)– Arrays– Records– Parameter passing
35http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Overflow in COverflow in C
#include <limits>
void main {
int k = INT_MAX;
k = k + 1;} Semantics
undefined
36http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
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)
Potential problems ...Potential problems ...
37http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Overflow & Constraint ChecksOverflow & Constraint Checks
procedure Checks is
K : Integer := Integer ’ Last;
beginK := K + 1;
end Checks;
exceptionConstraint_Error
raisedduring execution
% gnatmake -q checks% checks
raised CONSTRAINT_ERROR%
38http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
procedure Checks (X : Integer) is
type My_Int is range -100 .. 1_000_000;
T : My_Int;
beginT := My_Int (X);
end Checks;
Constraint_Errorraised
if X not in -100 .. -1_000_000
39http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
SubtypesSubtypes
type Day_Of_A_Month is range 1 .. 31;type Day_Of_February is range 1 .. 28;
D1 : Day_Of_February := 25;
D2 : Day_Of_A_Month := D1;
D2 : Day_Of_A_Month := Day_Of_A_Month (D1);
Compilation ERRORAda is strongly typed
OK, but tedious
40http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
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_Errorraised if
D3 not in 1 .. 28
D3 : Day_Of_A_Month;
D4 : Day_Of_February := D3;
41http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Predefined SubtypesPredefined Subtypes
subtype Natural is Integer range 0 .. Integer ’ Last;
subtype Positive is Natural range 1 .. Natural ’ Last;
42http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
ExceptionsExceptions
procedure Checks isA : Integer := Integer ’ First;
beginA := A - 1;
end Checks;
exceptionConstraint_Error
raisedduring execution
% gnatmake -q checks% checks
raised CONSTRAINT_ERROR%
43http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Predefined ExceptionsPredefined 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)
44http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Catching an ExceptionCatching 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% checksOverflow occurred%
45http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
procedure Checks is
Internal_Error : exception;
CreatingCreatingyour ownyour own
exceptionsexceptions
procedure Foo isbegin
raise Internal_Error;end Foo;
procedure Bar is begin
Foo;end Bar;
beginBar;
exception...
end Checks;Exception Handler
46http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
procedure Foo isbegin
raise Internal_Error;end Foo;procedure Bar is begin
Foo;end Bar;
beginBar;
exceptionwhen Internal_Error =>
Put_Line (“problem occurred”);when others =>
Put_Line (“some other exception”);end Checks;
Exception Handler
47http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
procedure Foo isbegin
raise Internal_Error;end Foo;
procedure Bar is begin
Foo;end Bar;
beginBar;
exceptionwhen Internal_Error =>
Put_Line (“problem occurred”);when others =>
Put_Line (“some other exception”);end Checks;
Exception Handler
1
2
4
5
3
48http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
procedure Foo isbegin
raise Internal_Error;end Foo;
procedure Bar is begin
Foo;end Bar;
beginBar;
exceptionwhen Internal_Error =>
Put_Line (“problem occurred”);when others =>
Put_Line (“some other exception”);end Checks;
1
2
4
5
3
49http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Catching an exceptionCatching an exceptionwhere YOU wantwhere YOU want
Want to catch someexception in a regionof code without exitingfrom the subprogram}
procedure Checks is...
begin
end Checks;
50http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Use Use declaredeclare
BlocksBlocks
procedure Checks is...
begin
end Checks;
Some_Label : declare
begin
exception
end Some_Label;
Statements
Declarations
Handler
51http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
ExampleExample
procedure Calc (A, B : Float) isC, D : Float;
begin
declareOld_C : Float := C;
beginC := A * B;D := C ** 2;
exceptionwhen Constraint_Error =>
C := Old_C;D := 0.0;
end;
52http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95– Scalar data types– Checks & Exceptions
– Access types (pointers)– Arrays– Records– Parameter passing
53http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Access TypesAccess Types
type Int_Ptr is access Integer;
P : Int_Ptr;pointers
are initializedto null
by default
pointersare initialized
to nullby default
Memory
54http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;
P : Int_Ptr;
P := new Integer;
Memory
P
???
55http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;
P : Int_Ptr;
P := new Integer;
P.all := 3344;
P
Memory
3334
56http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;
P : Int_Ptr;
P := new Integer ’ (1234);
P
Memory
1234
57http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;
P : Int_Ptr;
X : Integer := 567;
P := new Integer ’ (X);
P
Memory
567
58http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;
type Another_Int_Ptr is access Integer;
P : Int_Ptr := new Integer;
Q : Another_Int_Ptr;
Q := Another_Int_Ptr (P);
567
COMPILATION ERRORyou must use
general access typesto perform pointer
conversions
59http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Ptr_Int is access all Integer;
P : Ptr_Int;
Y : aliased Integer := 9999;
Memory
9999Y
General Access TypesGeneral Access Types
60http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Ptr_Int is access all Integer;
P : Ptr_Int;
Y : aliased Integer := 9999;
P := Y ’ access;
Memory
P
9999Y
61http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Ptr_Int is access all Integer;
P : Ptr_Int;
Y : aliased Integer := 9999;
P := Y ’ access;
P.all := 1234;
P
Memory
1234Y
62http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Ptr_Int is access all Integer;
P : Ptr_Int;
X : Integer;
P := X ’ access;
compilationerror
X is not aliased
63http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
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
64http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95– Scalar data types– Checks & Exceptions– Access types (pointers)
– Arrays– Records– Parameter passing
65http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Composite Ada typesComposite Ada types
• array (String)• record• tagged record• protected types• tasks
66http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
One of a Kind ArraysOne of a Kind Arrays
procedure Compute (N : Integer) is
A : array (1 .. N) of Float;
beginfor I in 1 .. N loop
A (I) := 3.141;end loop;
end Compute;
Arrays can have- dynamic bounds- dynamic size
67http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Typed ArraysTyped Arrays
procedure Compute (N : Integer) istype Arr is array (Integer range <>) of Float;
A : Arr (1 .. N);
B : Arr := A; B takes its bounds from A
Constraint_Errorif C’Length /= A’Length
Constraint_Errorif A’Last < 8
C : Arr (11 .. 20);
beginC := A
C (15 .. 18) := A (5 .. 8);
68http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
A : array (10 .. 20) of Float;
B : array (10 .. 20) of Float;
A := B;
Compilation errorA and B are one of a kind
69http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
11--Dim Array AttributesDim 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
70http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Vector is array (Natural range <>) of Float;
function Max (V : Vector) return Float isM : Float := Float ’ First;
beginfor I in V ’ range loop
if V (I) > M thenM := V (I);
end if;end loop;
return M;end Max;
71http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
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);
V2 : Vector (1 .. 100) := (1.0, 2.0, others => 99.0);
X : Float := Max (V1);Y : Float := Max (V2);
V1’First = 0 V1’Last = 6 V1’Length = 7
V2’First = 1 V2’Last = 100 V2’Length = 100
72http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Predefined Array TypePredefined 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”
73http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95– Scalar data types– Checks & Exceptions– Access types (pointers)– Arrays
– Records– Parameter passing
74http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Record TypesRecord Types
type Date is recordDay : 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;
75http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Node;type Node_Ptr is access Node;
type Node is recordD : Date := (1, 1, 1900);Next : Node_Ptr;
end record;
P1 : Node_Ptr := new Node;
Memory
P11
1
1900
null
3
9
1975
P2
P2 : Node_Ptr := new Node ’ ((3, 9, 1975), P1);
76http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
N : Node := ((31, 12, 1999), null);
P3 : Node_Ptr := new Node ’ (N);
Memory
P331
12
1999
null
77http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Record fields: simple ruleRecord 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
78http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Node is recordD : Date := (1, 1, 1900);Next : Node_Ptr;
end record;
P : Node_Ptr := new Node;
DD : Date := P.D;NN : Node_Ptr := P.Next;
79http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
ParametrizingParametrizing Records: Records: DiscriminantsDiscriminants
type Q_array (Natural range <>) of Integer;
type Queue (Max_Size : Natural) is recordFirst : Natural;Last : Natural;Size : Natural;
Q : Q_Array (0 .. Max_Size);end record;
80http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
type Q_Array (Positive range <>) of Integer;
type Queue (Max_Size : Positive) is recordFirst : Positive := 1;Last : Positive := 1;Size : Natural := 0;Q : Q_Array (1 .. Max_Size);
end record;
X : Queue (4); X.Max_Size = 4
81http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
X : Queue :=(Max_Size => 4,First => 2,Last => 3,Size => 2,Q => (0, 11, 22, 0));
X : Queue;
Compilation errorQueue is an
unconstrained typemust specify
discriminant value
X.Max_Size = ???
82http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95– Scalar data types– Checks & Exceptions– Access types (pointers)– Arrays– Records
– Parameter passing
83http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Parameter PassingParameter Passing
• in (functions & procedures)
• in out (procedures only)
• out (procedures only)
84http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
function Inc (X : Integer) return Integer isbegin
X := X + 1;return X;
end Inc; Compilation errorX is like a constantinside Inc
in parameters arecopied IN during a subprogram call
85http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
procedure Inc (X : in out Integer) isbegin
X := X + 1;end Inc;
Val : Integer := 3;
Inc (Val);-- here Val = 4
X is a regularvariable
inside Inc
in out parameters arecopied IN during a subprogram call
and copied OUT upon return
86http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
procedure Random (X : out Integer) isbegin
-- compute random numberX := …;
end Random;
Val : Integer;Random (Val);
X is a regularvariable inside Incwithout initial value
out parameters arecopied OUT upon return
87http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
Control StructuresControl Structures
• if-then-else• case statements• loops
– for– while– generic