![]() |
Functions |
|
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;
|
|
In the main file of the project, access the package. Here is an example: with Ada.Text_IO;
with FunctionX;
use Ada.Text_IO;
use FunctionX;
procedure Exercise is
StaffMember : Employee;
begin
StaffMember.EmployeeNumber := 704702;
StaffMember.FirstName := "Jonathan";
StaffMember.LastName := "Jensen";
StaffMember.HourlySalary := 22.84;
Put_Line("Employee Record");
Put_Line("Employee #: " & Integer'Image(StaffMember.EmployeeNumber));
Put_Line("First Name: " & StaffMember.FirstName);
Put_Line("Last Name: " & StaffMember.LastName);
Put_Line("Hourly Salary: " & Float'Image(StaffMember.HourlySalary));
end Exercise;
|
|
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
|
|
|
||
|
|
|
|
||
| Previous | Copyright © 2010 FunctionX, Inc. | Next |
|
|
||