Package
|
|
To start a package, create a project as seen in the
first lesson.
Add a file and write code to it. The code must specify
a package. Here is an example:
package FunctionX is
type Employee is
record
EmployeeNumber : Integer;
FirstName : String(1 .. 8);
LastName : String(1 .. 6);
HourlySalary : Float;
end record;
end FunctionX;
|
|
This would produce:
Employee Record Employee #: 704702 First Name: Jonathan Last Name: Jensen Hourly Salary: 2.28400E+01
Start a project. Add a file with the desired contents. Here is an example:
package Example1 is
procedure SayWelcome(x : in Integer);
end Example1;
Save the file the file with a .ads extension. In
this case, that would be example.ads
Add another file. Here is an example:
with Ada.Text_IO; use Ada.Text_IO;
package body Example1 is
procedure SayWelcome(x : in Integer) is
begin
put_line("We say welcome to you!!!");
end SayWelcome;
end Example1;
Save the file with a .adb extension. In this case, that
would be example.adb
To access the procedure in the main file, here is an
example:
with Ada.Text_IO; use Ada.Text_IO; with Example1; use Example1; procedure Exercise is begin SayWelcome(1); end Exercise;
This would produce:
We say welcome to you!!!
Start a project. Add a file with the desired contents. Here is an example:
package Example1 is
function Doubles(x : in Float) return Float;
end Example1;
Save the file the file with a .ads extension. In
this case, that would be example.ads
Add another file. Here is an example:
package body Example1 is
function Doubles(x : in Float) return Float is
begin
return x * 2.00;
end Doubles;
end Example1;
Save the file with a .adb extension. In this case, that
would be example.adb
To access the function in the main file, here is an
example:
with Ada.Text_IO; use Ada.Text_IO; with Example1; use Example1; procedure Exercise is number : Float; result : Float; begin number := 48.36; result := Doubles(number); put_line(Float'Image(number) & " * 2 = " & Float'Image(result)); end Exercise;
This would produce:
4.83600E+01 * 2 = 9.67200E+01 |
No comments:
Post a Comment