Previous topic | Ada Home Page | Index

Procedures example: drawing outsize letters

First we see how a program to write "ADA" in giant letters would be written as a monolithic program. Then we look at it when it is broken into procedures.

You can see that a procedure only needs to be written once, and can then be invoked as many times as necessary. The resultant shortening of the program is one of the benefits of procedures.

Monolithic program




--------------------------------------------------------
-- Write "ADA" in large letters
-- Skansholm, section 2.4.4, page 44
--------------------------------------------------------

with TEXT_io;
use TEXT_IO;

procedure GIANT_ADA is
begin
    -- draw a giant "A"
    new_line;
    put_line ("     *     ");
    put_line ("    * *    ");
    put_line ("   *   *   ");
    put_line ("  *******  ");
    put_line (" *       * ");
    put_line ("*         *");
    new_line;

    -- draw a giant "D"
    new_line;
    put_line (" *******   ");
    put_line (" *      *  ");
    put_line (" *       * ");
    put_line (" *       * ");
    put_line (" *       * ");
    put_line (" *      *  ");
    put_line (" *******   ");
    new_line;

    -- draw a giant "A"
    new_line;
    put_line ("     *     ");
    put_line ("    * *    ");
    put_line ("   *   *   ");
    put_line ("  *******  ");
    put_line (" *       * ");
    put_line ("*         *");
    new_line;

end GIANT_ADA;

Using procedures




--------------------------------------------------------
-- Write "ADA" in large letters
-- Skansholm, section 2.4.4, page 46
--------------------------------------------------------

with TEXT_io;
use TEXT_IO;

procedure GIANT_ADA is

    procedure DRAW_GIANT_A is
    begin
        -- draw a giant "A"
        new_line;
        put_line ("     *     ");
        put_line ("    * *    ");
        put_line ("   *   *   ");
        put_line ("  *******  ");
        put_line (" *       * ");
        put_line ("*         *");
        new_line;
    end DRAW_GIANT_A;

    procedure DRAW_GIANT_D is
    begin
        -- draw a giant "D"
        new_line;
        put_line (" *******   ");
        put_line (" *      *  ");
        put_line (" *       * ");
        put_line (" *       * ");
        put_line (" *       * ");
        put_line (" *      *  ");
        put_line (" *******   ");
        new_line;
    end DRAW_GIANT_D;


begin -- DRAW_GIANT_ADA
    DRAW_GIANT_A;
    DRAW_GIANT_D;
    DRAW_GIANT_A;
end GIANT_ADA;


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