87
1 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License Franco Gasperoni [email protected] http://libre.act-europe.fr

Ada 95 - Introduction

Embed Size (px)

DESCRIPTION

Author: Franco Gasperoni License: GFDL

Citation preview

Page 1: Ada 95 - Introduction

1http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Franco [email protected]://libre.act-europe.fr

Page 2: Ada 95 - Introduction

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

Page 3: Ada 95 - Introduction

3http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Page 4: Ada 95 - Introduction

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

Page 5: Ada 95 - Introduction

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

Page 6: Ada 95 - Introduction

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

Page 7: Ada 95 - Introduction

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

Page 8: Ada 95 - Introduction

8http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Background on Ada 95

• Programming with Ada 95

Page 9: Ada 95 - Introduction

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

Page 10: Ada 95 - Introduction

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

Page 11: Ada 95 - Introduction

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

Page 12: Ada 95 - Introduction

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?

Page 13: Ada 95 - Introduction

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

Page 14: Ada 95 - Introduction

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!

Page 15: Ada 95 - Introduction

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

Page 16: Ada 95 - Introduction

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

Page 17: Ada 95 - Introduction

17http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Background on Ada 95

• Programming with Ada 95

Page 18: Ada 95 - Introduction

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); }

Page 19: Ada 95 - Introduction

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 ...

Page 20: Ada 95 - Introduction

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

Page 21: Ada 95 - Introduction

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

Page 22: Ada 95 - Introduction

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

Page 23: Ada 95 - Introduction

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%

Page 24: Ada 95 - Introduction

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

Page 25: Ada 95 - Introduction

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

Page 26: Ada 95 - Introduction

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

Page 27: Ada 95 - Introduction

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)

Page 28: Ada 95 - Introduction

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;

Page 29: Ada 95 - Introduction

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

Page 30: Ada 95 - Introduction

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;

Page 31: Ada 95 - Introduction

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

Page 32: Ada 95 - Introduction

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)

Page 33: Ada 95 - Introduction

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;

Page 34: Ada 95 - Introduction

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

Page 35: Ada 95 - Introduction

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

Page 36: Ada 95 - Introduction

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 ...

Page 37: Ada 95 - Introduction

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%

Page 38: Ada 95 - Introduction

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

Page 39: Ada 95 - Introduction

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

Page 40: Ada 95 - Introduction

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;

Page 41: Ada 95 - Introduction

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;

Page 42: Ada 95 - Introduction

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%

Page 43: Ada 95 - Introduction

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)

Page 44: Ada 95 - Introduction

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%

Page 45: Ada 95 - Introduction

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

Page 46: Ada 95 - Introduction

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

Page 47: Ada 95 - Introduction

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

Page 48: Ada 95 - Introduction

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

Page 49: Ada 95 - Introduction

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;

Page 50: Ada 95 - Introduction

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

Page 51: Ada 95 - Introduction

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;

Page 52: Ada 95 - Introduction

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

Page 53: Ada 95 - Introduction

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

Page 54: Ada 95 - Introduction

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

???

Page 55: Ada 95 - Introduction

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

Page 56: Ada 95 - Introduction

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

Page 57: Ada 95 - Introduction

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

Page 58: Ada 95 - Introduction

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

Page 59: Ada 95 - Introduction

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

Page 60: Ada 95 - Introduction

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

Page 61: Ada 95 - Introduction

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

Page 62: Ada 95 - Introduction

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

Page 63: Ada 95 - Introduction

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

Page 64: Ada 95 - Introduction

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

Page 65: Ada 95 - Introduction

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

Page 66: Ada 95 - Introduction

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

Page 67: Ada 95 - Introduction

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);

Page 68: Ada 95 - Introduction

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

Page 69: Ada 95 - Introduction

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

Page 70: Ada 95 - Introduction

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;

Page 71: Ada 95 - Introduction

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

Page 72: Ada 95 - Introduction

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”

Page 73: Ada 95 - Introduction

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

Page 74: Ada 95 - Introduction

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;

Page 75: Ada 95 - Introduction

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);

Page 76: Ada 95 - Introduction

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

Page 77: Ada 95 - Introduction

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

Page 78: Ada 95 - Introduction

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;

Page 79: Ada 95 - Introduction

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;

Page 80: Ada 95 - Introduction

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

Page 81: Ada 95 - Introduction

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 = ???

Page 82: Ada 95 - Introduction

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

Page 83: Ada 95 - Introduction

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)

Page 84: Ada 95 - Introduction

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

Page 85: Ada 95 - Introduction

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

Page 86: Ada 95 - Introduction

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

Page 87: Ada 95 - Introduction

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