18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

62 Procedures and functions<br />

package Pack_Types is<br />

type Miles is digits 8 range 0.0 .. 25_000.0;<br />

type Kilometres is digits 8 range 0.0 .. 50_000.0;<br />

end Pack_Types;<br />

Then this package is made visible to the function M_To_K_Fun.<br />

with Pack_Types; use Pack_Types;<br />

function M_To_K_Fun(M:<strong>in</strong> Miles) return Kilometres is<br />

Kilometres_Per_Mile : constant := 1.609_344;<br />

beg<strong>in</strong><br />

return Kilometres( M * Kilometres_Per_Mile );<br />

end M_To_K_Fun;<br />

F<strong>in</strong>ally, <strong>in</strong> the ma<strong>in</strong> procedure <strong>in</strong> addition to the normal <strong>in</strong>put and output packages the package Pack_Types<br />

and the function M_To_K_Fun are made visible.<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io, Pack_Types, M_To_K_Fun;<br />

use <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Float_Text_Io, Pack_Types;<br />

procedure Ma<strong>in</strong> is<br />

No_Miles : Miles;<br />

beg<strong>in</strong><br />

Put("Miles Kilometres"); New_L<strong>in</strong>e; No_Miles := 0.0;<br />

while No_Miles 2, Exp=>0 ); Put(" ");<br />

Put( Float( M_To_K_Fun( No_Miles ) ), Aft=>2, Exp=>0 );<br />

New_L<strong>in</strong>e; No_Miles := No_Miles + 1.0;<br />

end loop;<br />

end Ma<strong>in</strong>;<br />

Note:<br />

5.3 Procedures<br />

It is only required to with the function M_To_K_Fun. It would be an error to use the function<br />

M_To_K_Fun.<br />

A procedure is a program unit that, unlike a function, does not return a result. If <strong>in</strong>formation has to be returned to<br />

the call<strong>in</strong>g environment this is done <strong>in</strong>stead by writ<strong>in</strong>g to a formal parameter that has been declared as mode out.<br />

Writ<strong>in</strong>g to the formal parameter of a procedure updates the value of the actual parameter passed to the procedure.<br />

The full implications of modes of a parameter are discussed <strong>in</strong> Section 5.5.<br />

type Miles is digits 8 range 0.0 .. 25_000.0;<br />

type Kilometres is digits 8 range 0.0 .. 50_000.0;<br />

procedure M_To_K_Proc(M:<strong>in</strong> Miles; Res:out Kilometres) is<br />

Kilometres_Per_Mile : constant := 1.609_344;<br />

beg<strong>in</strong><br />

Res := Kilometres( M * Kilometres_Per_Mile );<br />

end M_To_K_Proc;<br />

Note:<br />

A procedure can only export values back to the caller’s environment by writ<strong>in</strong>g to a parameter that has<br />

mode out or <strong>in</strong> out.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!