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

5.8 Different number of parameters<br />

As the compiler can dist<strong>in</strong>guish between overloaded names, several functions that deliver the maximum or larger<br />

of their parameters can be written. With re-use <strong>in</strong> m<strong>in</strong>d the first function Max2 can be written which delivers the<br />

maximum of the two Integer parameters passed to it.<br />

function Max2( A,B:<strong>in</strong> Integer ) return Integer is<br />

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

if A > B then<br />

return A; --a is larger<br />

else<br />

return B;<br />

end if;<br />

end Max2;<br />

--b is larger<br />

This function Max2 can be re-used <strong>in</strong> a function Max3 that will deliver the larger of three parameters passed to it.<br />

with Max2;<br />

function Max3( A,B,C:<strong>in</strong> Integer ) return Integer is<br />

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

return Max2( Max2( A,B ), C );<br />

end Max3;<br />

Then the follow<strong>in</strong>g code can be written:<br />

with <strong>Ada</strong>.Text_io, <strong>Ada</strong>.Integer_Text_Io, Max2, Max3;<br />

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

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

function Max(A,B:<strong>in</strong> Integer) return Integer renames Max2;<br />

function Max(A,B,C:<strong>in</strong> Integer) return Integer renames Max3;<br />

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

Put("Larger of 2 and 3 is "); Put( Max(2,3) ); New_L<strong>in</strong>e;<br />

Put("Larger of 2 3 4 is "); Put( Max(2,3,4) ); New_L<strong>in</strong>e;<br />

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

Note:<br />

The use of renames to overload the name Max with 2 dist<strong>in</strong>ct function def<strong>in</strong>itions.<br />

which when run produces:<br />

Larger of 2 and 3 is 3<br />

Larger of 2 3 4 is 4<br />

Note:<br />

The overload<strong>in</strong>g of names <strong>in</strong> an <strong>Ada</strong> program can provide a simpler <strong>in</strong>terface for a programmer.<br />

However, the overuse of overload<strong>in</strong>g can lead to programs that are difficult to ma<strong>in</strong>ta<strong>in</strong> and debug.<br />

5.9 Default values and named parameters<br />

If a default value is given to a parameter, then it may be omitted by a programmer when they write the call to the<br />

function or procedure.<br />

For example, the function sum whose four parameters have a default value of zero returns the sum of these<br />

parameters. The procedure Answer_Is pr<strong>in</strong>ts the first parameter with an additional message when the second<br />

parameter has the default value True.<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!