home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / mbug / mbug175.arc / JRTMAN2.LBR / JRTMAN.1Z2 / JRTMAN.102
Text File  |  1979-12-31  |  7KB  |  218 lines

  1. .OP
  2.  
  3.       JRT Pascal  User's Guide     version 3.0         NOT FOR SALE   -136-
  4.  
  5.  
  6.  
  7.       12.   External Procedures and Functions
  8.  
  9.  
  10.            External  procedures are a facility for segmenting programs into
  11.       separately compiled modules.  With these,  the  size  of  the  entire
  12.       program  can  be practically unlimited.  This is because, unlike with
  13.       segment procedures, overlays or chaining, the virtual storage manager
  14.       loads, and when necessary deletes,  program  sections  automatically.
  15.       This  makes  the actual storage of the computer seem much larger than
  16.       it actually is.
  17.  
  18.            Refer to the previous section on storage management for  a  full
  19.       description of virtual/dynamic storage.
  20.  
  21.            External procedures are loaded into dynamic storage by EXEC when
  22.       they  are  first  referenced,  unless  they were linked with the main
  23.       program to form one module (see section 8 for a  description  of  the
  24.       linker).   The  loading  is  transparent to the programmer in that no
  25.       planning or effort is required.
  26.  
  27.            External procedures which are not linked with the  main  program
  28.       remain  in  storage  once  they  are loaded unless a short-on-storage
  29.       condition occurs,  then  the  least-recently-used  procedure  may  be
  30.       deleted.   If  this  happens,  the control blocks associated with the
  31.       procedure ARE KEPT so that reloading, if necessary, can be done  more
  32.       rapidly.  When main storage is severely overloaded, frequent deleting
  33.       and  reloading  of  external procedures may occur.  This condition is
  34.       called  "thrashing".   Thrashing  can  be  recognized  by   unusually
  35.       frequent  disk  accessing  and little useful processing being done by
  36.       the program.  It is necessary in this  case  to  reduce  the  storage
  37.       requirements of the program.
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.                  SECTION 12:  External Procedures and Functions
  56. .PAè
  57.       JRT Pascal  User's Guide     version 3.0         NOT FOR SALE   -137-
  58.  
  59.  
  60.  
  61.       12.1  Coding external procedures and functions
  62.  
  63.  
  64.            The external procedure Pascal file is very similar to a standard
  65.       "internal"  procedure  in format.  In many cases the only differences
  66.       from a standard procedure format are that the PROCEDURE reserved word
  67.       is preceded by the reserved word EXTERN and that the  whole  file  is
  68.       ended  with  a  period  to  signify  the end of the compile unit.  An
  69.       example of this basic case follows:
  70.  
  71.                   EXTERN
  72.  
  73.                   (* PRINT THE TOTAL AND AVERAGE OF 4 NUMBERS *) 
  74.                   PROCEDURE XDEMO (A,B,C,D : REAL );
  75.                   VAR
  76.                   TOTAL : REAL;
  77.  
  78.                   BEGIN
  79.                   TOTAL := A + B + C + D;
  80.                   WRITELN('TOTAL =',TOTAL,
  81.                           '  AVERAGE =',TOTAL / 4.0);
  82.                   END;.
  83.  
  84.  
  85.       ****** IMPORTANT ****** READ THE FOLLOWING CAREFULLY
  86.  
  87.            JRT Pascal external procedures can  access  all  of  the  global
  88.       variables in the main program.  The GLOBAL VARIABLES are those IN THE
  89.       MAIN  PROGRAM DECLARED BEFORE ANY PROCEDURE OR FUNCTION DECLARATIONS.
  90.       They are variables that are available globally and not only local  to
  91.       some  procedure.  In the preceding example, TOTAL is a local variable
  92.       - it is not accessible outside of the procedure XDEMO.
  93.  
  94.            To access global variables  or  files,  their  declarations  are
  95.       inserted  in  the  external  procedure file AFTER the word EXTERN and
  96.       BEFORE the procedure header.  The three declaration  sections  CONST,
  97.       TYPE,  VAR  may be inserted at this point.  They must be identical to
  98.       the global declarations in the main program, except  that  additional
  99.       constants and type identifiers may be added here.
  100.  
  101.            Type  identifiers  may  be  required  in  the  procedure  header
  102.       parameter list or  in  a  function  return  value  declaration.   The
  103.       declaration  of  these  type  identifiers  should  appear IN THE SAME
  104.       LOCATION as the global declarations -- just after EXTERN.
  105.  
  106.  
  107.  
  108.  
  109.                  SECTION 12:  External Procedures and Functions
  110. .PAè
  111.       JRT Pascal  User's Guide     version 3.0         NOT FOR SALE   -138-
  112.  
  113.  
  114.  
  115.                   EXTERN
  116.  
  117.                   CONST
  118.  
  119.                   NAME_SIZE = 32;
  120.  
  121.                   TYPE
  122.  
  123.                   NAME = ARRAY [1..NAME_SIZE] OF CHAR;
  124.  
  125.                   CUSTOMER_RECORD = RECORD
  126.                         CUST_NAME, CUST_ADDR    : NAME;
  127.                         BALANCE                 : REAL;
  128.                         END;
  129.  
  130.                   VAR   (* MAIN PROGRAM GLOBAL VARIABLES *)
  131.  
  132.                   CUSTOMER_LIST : ARRAY [1..100] OF 
  133.                                           CUSTOMER_RECORD;
  134.  
  135.                   (**** SEARCH CUSTOMER LIST FOR GIVEN NAME ****)
  136.                   FUNCTION SEARCH ( N : NAME ) : CUSTOMER_RECORD;
  137.                   VAR
  138.                   I : INTEGER;
  139.  
  140.                   BEGIN
  141.                   I:=1;
  142.  
  143.                   WHILE (N <> CUSTOMER_LIST[I].CUST_NAME
  144.                         AND (I <= 100) DO   I:=I+1;
  145.  
  146.                   IF  N =  CUSTOMER_LIST[I].CUST_NAME THEN
  147.                            SEARCH:=CUSTOMER_LIST[I]
  148.                   ELSE     SEARCH:=' ';
  149.  
  150.                   END;.
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.                  SECTION 12:  External Procedures and Functions
  164. .PAè
  165.       JRT Pascal  User's Guide     version 3.0         NOT FOR SALE   -139-
  166.  
  167.  
  168.  
  169.       12.2  Referencing external procedures and functions
  170.  
  171.  
  172.            External procedures and functions MUST be declared in  the  main
  173.       programs  which  reference them.  Their declaration is identical to a
  174.       regular procedure except that the entire body  of  the  procedure  is
  175.       replaced with the reserved word EXTERN.
  176.  
  177.  
  178.                   PROCEDURE PLOTTER ( X,Y : INTEGER ); EXTERN;
  179.  
  180.                   FUNCTION CUBEROOT ( A : REAL ): REAL; EXTERN;
  181.  
  182.  
  183.            For  clarity,  it  is  useful  to  group  all external procedure
  184.       declarations as the first  procedure  declarations  in  the  program.
  185.       External  procedures  may  reference  other  external  procedures  if
  186.       appropriate declarations are included in the referencing procedure.
  187.  
  188.            EXEC indentifies  external  procedures  by  a  SEQUENCE  NUMBER.
  189.       External  procedures  SHOULD ALWAYS BE DECLARED IN THE SAME SEQUENCE,
  190.       in the main program or in another external procedure.
  191.  
  192.            NOTE  that  THE  USER  MUST  ENSURE  that   external   procedure
  193.       declarations  and  parameter  lists  are  CONSISTENT  among different
  194.       files, since the compiler does not validate this.
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.                  SECTION 12:  External Procedures and Functions
  218. .PAè