Velsen
Main St, Anywhere, USA 00000
1-111-222-3333

Hide Menu
gtkada example
Below is a complete example where an Ada main program passes an employee record to a COBOL subprogram, which calculates a bonus and updates the record in place

ADA MAIN PROGRAM
with Ada.Text_IO;      use Ada.Text_IO;
with Interfaces.COBOL;

procedure Main is
   package COBOL renames Interfaces.COBOL;

   -- 1. Define fixed-point decimal types matching PIC 9(5)V99
   type Currency is delta 0.01 digits 7;

   -- Instantiate conversion helpers for binary (USAGE COMP) data
   package Currency_Conv is new COBOL.Decimal_Conversions (Currency);

   -- 2. Define a record matching COBOL's 01 EMP-RECORD layout
   type Employee_Record is record
      Name   : COBOL.Alphanumeric (1 .. 20);
      Salary : COBOL.Binary;
      Bonus  : COBOL.Binary;
   end record
     with Convention => COBOL;

   -- 3. Import the external COBOL subprogram
   procedure Calc_Bonus (Item : in out Employee_Record)
     with Import        => True,
          Convention    => COBOL,
          External_Name => "CALC-BONUS";

   Emp : Employee_Record;
   Base_Pay : constant Currency := 65_000.00;
begin
   -- Populate the COBOL-compatible record
   Emp.Name   := COBOL.To_COBOL ("Alice Smith         ");
   Emp.Salary := Currency_Conv.To_Binary (Base_Pay);
   Emp.Bonus  := Currency_Conv.To_Binary (0.00);

   -- Call the COBOL subprogram
   Calc_Bonus (Emp);

   -- Convert the updated COBOL binary field back to an Ada type
   Put_Line ("Employee: " & COBOL.To_Ada (Emp.Name));
   Put_Line ("Salary:   " & Currency'Image (Currency_Conv.From_Binary (Emp.Salary)));
   Put_Line ("Bonus:    " & Currency'Image (Currency_Conv.From_Binary (Emp.Bonus)));
end Main;

COBOL SUBPROGRAM
with Ada.Text_IO;      use Ada.Text_IO;
with Interfaces.COBOL;

procedure Main is
   package COBOL renames Interfaces.COBOL;

   -- 1. Define fixed-point decimal types matching PIC 9(5)V99
   type Currency is delta 0.01 digits 7;

   -- Instantiate conversion helpers for binary (USAGE COMP) data
   package Currency_Conv is new COBOL.Decimal_Conversions (Currency);

   -- 2. Define a record matching COBOL's 01 EMP-RECORD layout
   type Employee_Record is record
      Name   : COBOL.Alphanumeric (1 .. 20);
      Salary : COBOL.Binary;
      Bonus  : COBOL.Binary;
   end record
     with Convention => COBOL;

   -- 3. Import the external COBOL subprogram
   procedure Calc_Bonus (Item : in out Employee_Record)
     with Import        => True,
          Convention    => COBOL,
          External_Name => "CALC-BONUS";

   Emp : Employee_Record;
   Base_Pay : constant Currency := 65_000.00;
begin
   -- Populate the COBOL-compatible record
   Emp.Name   := COBOL.To_COBOL ("Alice Smith         ");
   Emp.Salary := Currency_Conv.To_Binary (Base_Pay);
   Emp.Bonus  := Currency_Conv.To_Binary (0.00);

   -- Call the COBOL subprogram
   Calc_Bonus (Emp);

   -- Convert the updated COBOL binary field back to an Ada type
   Put_Line ("Employee: " & COBOL.To_Ada (Emp.Name));
   Put_Line ("Salary:   " & Currency'Image (Currency_Conv.From_Binary (Emp.Salary)));
   Put_Line ("Bonus:    " & Currency'Image (Currency_Conv.From_Binary (Emp.Bonus)));
end Main;