Previous topic | Ada Home Page | Index

Subunits Example

Program structure

Main program

A program to compute overall subject marks for students in Computer Science 2A might require several procedures. When separate compilation with subunits is used, the main program would be as follows:

------------------------------------------------------------
-- procedure cs2a
--
-- Purpose: find and print the CS2A marks and grades for
-- several students
--
-- Author:  Chris Lokan
------------------------------------------------------------

with TEXT_IO;
procedure cs2a is

    package int_io is new TEXT_IO.INTEGER_IO (INTEGER); 
    use  TEXT_IO, int_io;

    mark  : INTEGER;    -- subject mark for CS2A (0..100)
    grade : CHARACTER;  -- first character of grade
    answer: CHARACTER;

    procedure get_integer
      ( val  : out INTEGER;    -- value entered by the user
        low  : in  INTEGER;    -- low  bound of range
        high : in  INTEGER )   -- high bound of range
    is separate;

    procedure get_marks
       (m1,m3,m4,m4 : out INTEGER)   -- unit marks 
    is separate;

    procedure compute
       (m1,m2,m3,m4 : in  INTEGER;   -- unit marks
        mark        : out INTEGER;   -- subject mark
        grade       : out CHARACTER) -- first char of grade
    is separate;

    procedure process_student 
       (mark  : out INTEGER;         -- subject mark
        grade : out CHARACTER)       -- subject grade
    is separate;

begin -- cs2a

    loop
        process_student (mark, grade);
        put ("The overall mark and grade are ");
        put (mark,width=>3);
        put ("  " & grade);
        new_line;

        put ("Another student ? " );
        get (answer); skip_line;
        exit when (answer = 'n') or (answer = 'N');
    end loop;

end cs2a;

Note that stubs have been provided for four procedures. The definitions of those procedures would come in separate files, compiled separately.

process_student.a

-----------------------------------------------------------
-- procedure process_student
--
-- Purpose: find and return the CS2A mark and grade for a
-- student
--
-- Author:  c-lokan
-- Date:    19 July 1994
-----------------------------------------------------------

separate(cs2a)

procedure process_student 
   (
    mark  : out INTEGER;        -- subject mark
    grade : out CHARACTER       -- subject grade
   ) 
is
    m1 : INTEGER;               -- mark in first  CS2 unit
    m2 : INTEGER;               -- mark in second CS2 unit
    m3 : INTEGER;               -- mark in third  CS2 unit
    m4 : INTEGER;               -- mark in fourth CS2 unit

begin -- process_student
    get_marks (m1, m2, m3, m4);
    compute (m1, m2, m3, m4, mark, grade);
end process_student;


Compilation commands

First the main program needs to be compiled. When the procedure stubs are encountered, the compiler puts information about those stubs into the Ada library. The stubs provide enough information for the main program itself to be compiled successfully.

Then the procedures are compiled separately. As each is compiled, the stub information in the library is replaced by the actual information from the procedure definition.

The precise commands you need to use depend on the Ada environment you are using:

Sun ada

In our sun ada environment, your source files should each have the suffix ".a". To compile them you would use the following steps:

  1. create a new library
    adas
  2. compile main program first
    adac cs2a.a
  3. compile each separate procedure
    adac get_integer.a
    adac get_marks.a
    adac compute.a
    adac process_student.a
  4. binding is possible after all compilations
    adae cs2a
  5. run the program
    a.out

gnat ada

In the gnat ada environment, your source files should each have the suffix ".adb". To compile them you would use the following steps:

  1. compile main program first
    gnatcc cs2a.adb
  2. compile each separate procedure
    gnatcc get_integer.adb
    gnatcc get_marks.adb
    gnatcc compute.adb
    gnatcc process_student.adb
  3. binding is possible after all compilations
    gnatbl cs2a.ali
  4. run the program
    cs2a


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 17 Feb 1996