comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: how to make a package????
Date: 1996/08/15
Date: 1996-08-15T00:00:00+00:00	[thread overview]
Message-ID: <4uvec2$nrf@newsbf02.news.aol.com> (raw)
In-Reply-To: 4uu2kp$32u@news.us.net


mhall59@us.net (Michael W. Hall) asks about
converting a Sqrt function to a package.
     Your Sqrt function is excellent as it stands.  You wouldn't *convert*
it to a package, but you would *move* it into a new package.  That way,
other programs besides your Square_Root could make use of your excellent
Sqrt function.  Here's how to move your function into a new package:

package Math is  -- This is the package specification.
   function Sqrt (Value : in Float) return Float;
   -- You could add other subprogram specifications here.
end Math;

package body Math is  -- This is the package body.
   function Sqrt (Value : in Float) return Float is
   ...
   end Sqrt;
   -- The bodies of any other subprograms would go here.
end Math;

with Text_IO, Math;  -- Your main program.
procedure Square_Root is
   package Float_IO is new Text_IO.Float_IO (Num => Float);
begin
   Float_IO.Put (Math.Sqrt (16.0));
   Text_IO.New_Line;
   Float_IO.Put (Math.Sqrt (15.0));
   Text_IO.New_Line;
end Square_Root;

     You must compile the package specification first, but you can compile
either the package body or the main program second.  Most compilers will
let you put all three parts into one file and compile that, but the
package specification must come first.
     Now other programs that need Sqrt can "with" your Math package and
gain access to Sqrt.
     There's much more to packages.  For example, the above package can be
improved by using "is separate."  Packages can also help contain the
effects of certain changes, so they affect only a small part of your
program.
     For more information, you can download my Ada Tutor program from the
Web or FTP site below my signature.  Besides more information on packages,
it has a sample Sqrt function similar to yours, but generic so that it can
be used with other floating point types besides the standard Float.
     Also, feel free to ask more questions here.  You'll find the people
in comp.lang.ada eager to help.

- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




      parent reply	other threads:[~1996-08-15  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-08-15  0:00 how to make a package???? Michael W. Hall
1996-08-15  0:00 ` Kevin J. Weise
1996-08-15  0:00   ` Michael W. Hall
1996-08-15  0:00 ` John Herro [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox