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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Procedures and functions 61<br />

The function M_To_K_Fun is used <strong>in</strong> the follow<strong>in</strong>g program that will pr<strong>in</strong>t a conversion table for miles to<br />

kilometres. In <strong>Ada</strong>, a function or procedure may be declared <strong>in</strong> the declaration section of a procedure or function.<br />

By us<strong>in</strong>g this technique, the types for Miles and Kilometres can be made visible to both the function<br />

M_To_K_Fun and the ma<strong>in</strong> body of code that implements the pr<strong>in</strong>t<strong>in</strong>g of the conversion table.<br />

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

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

procedure Ma<strong>in</strong> 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 />

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

No_Miles : Miles;<br />

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

Put("Miles Kilometres"); New_L<strong>in</strong>e;<br />

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;<br />

No_Miles := No_Miles + 1.0;<br />

end loop;<br />

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

When compiled, and run the above program will pr<strong>in</strong>t the follow<strong>in</strong>g results:<br />

Miles Kilometres<br />

0.00 0.00<br />

1.00 1.61<br />

2.00 3.22<br />

3.00 4.83<br />

4.00 6.44<br />

5.00 8.05<br />

6.00 9.66<br />

7.00 11.27<br />

8.00 12.87<br />

9.00 14.48<br />

10.00 16.09<br />

5.2.1 Local variables<br />

When a variable is declared <strong>in</strong>side a function, its lifetime is that of the function. When the function is entered,<br />

space for any local variables is created automatically on a run-time stack. On exit from the function, the space<br />

created for the local variables is returned to the system.<br />

5.2.2 Separate compilation of functions<br />

It is possible to compile the above function separately. However, if this is done the same types for Miles and<br />

Kilometres must be used <strong>in</strong> both the ma<strong>in</strong> program and the function. One way of ensur<strong>in</strong>g this is to use a<br />

package that acts as a conta<strong>in</strong>er for the types. This package is then made visible to both program units. The<br />

follow<strong>in</strong>g program illustrates this idea.<br />

Firstly, the package that conta<strong>in</strong>s the types Miles and Kilometres is constructed.<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!