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.

Procedures and functions 65<br />

Mode Allowed as a Effect<br />

parameter to:<br />

<strong>in</strong> a function or a<br />

procedure.<br />

The formal parameter is <strong>in</strong>itialized to the contents of the actual<br />

parameter and may only be read from.<br />

<strong>in</strong> out only a procedure The formal parameter is <strong>in</strong>itialized to the contents of the actual<br />

parameter and may be read from or written to. When the<br />

procedure is exited, the new value of the formal parameter<br />

replaces the old contents of the actual parameter.<br />

out only a procedure The formal parameter is not <strong>in</strong>itialized to the contents of the<br />

actual parameter and may be read from or written to. When the<br />

procedure is exited, the new value of the formal parameter<br />

replaces the old contents of the actual parameter.<br />

In <strong>Ada</strong> 83 an out formal parameter may not be read from.<br />

Note:<br />

The implementation of the above for simple objects is usually performed by copy<strong>in</strong>g the contents of the<br />

object, whilst for large objects the compiler may implement this by us<strong>in</strong>g references to the actual<br />

object.<br />

5.5.1 Example of mode <strong>in</strong> out<br />

A procedure swap which <strong>in</strong>terchanges the contents of the actual parameters passed to it is as follows:<br />

procedure Swap(First:<strong>in</strong> out Integer; Second:<strong>in</strong> out Integer) is<br />

Temp : Integer;<br />

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

Temp := First;<br />

First := Second; Second := Temp;<br />

end Swap;<br />

5.5.2 Putt<strong>in</strong>g it all together<br />

The function swap may then be used <strong>in</strong> a program as follows:<br />

with <strong>Ada</strong>.Text_Io, <strong>Ada</strong>.Integer_Text_Io, Swap;<br />

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

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

Books_Room_1 : Integer;<br />

Books_Room_2 : Integer;<br />

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

Books_Room_1 := 10; Books_Room_2 := 20;<br />

Put("Books <strong>in</strong> room 1 ="); Put(Books_Room_1); New_L<strong>in</strong>e;<br />

Put("Books <strong>in</strong> room 2 ="); Put(Books_Room_2); New_L<strong>in</strong>e;<br />

Put("Swap around"); New_L<strong>in</strong>e;<br />

Swap(Books_Room_1, Books_Room_2);<br />

Put("Books <strong>in</strong> room 1 ="); Put(Books_Room_1); New_L<strong>in</strong>e;<br />

Put("Books <strong>in</strong> room 2 ="); Put(Books_Room_2); New_L<strong>in</strong>e;<br />

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

which when run produces:<br />

Books <strong>in</strong> room 1 = 10<br />

Books <strong>in</strong> room 2 = 20<br />

Swap around<br />

Books <strong>in</strong> room 1 = 20<br />

Books <strong>in</strong> room 2 = 10<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!