JRT Pascal User's Guide version 3.0 NOT FOR SALE -136- 12. External Procedures and Functions External procedures are a facility for segmenting programs into separately compiled modules. With these, the size of the entire program can be practically unlimited. This is because, unlike with segment procedures, overlays or chaining, the virtual storage manager loads, and when necessary deletes, program sections automatically. This makes the actual storage of the computer seem much larger than it actually is. Refer to the previous section on storage management for a full description of virtual/dynamic storage. External procedures are loaded into dynamic storage by EXEC when they are first referenced, unless they were linked with the main program to form one module (see section 8 for a description of the linker). The loading is transparent to the programmer in that no planning or effort is required. External procedures which are not linked with the main program remain in storage once they are loaded unless a short-on-storage condition occurs, then the least-recently-used procedure may be deleted. If this happens, the control blocks associated with the procedure ARE KEPT so that reloading, if necessary, can be done more rapidly. When main storage is severely overloaded, frequent deleting and reloading of external procedures may occur. This condition is called "thrashing". Thrashing can be recognized by unusually frequent disk accessing and little useful processing being done by the program. It is necessary in this case to reduce the storage requirements of the program. SECTION 12: External Procedures and Functions JRT Pascal User's Guide version 3.0 NOT FOR SALE -137- 12.1 Coding external procedures and functions The external procedure Pascal file is very similar to a standard "internal" procedure in format. In many cases the only differences from a standard procedure format are that the PROCEDURE reserved word is preceded by the reserved word EXTERN and that the whole file is ended with a period to signify the end of the compile unit. An example of this basic case follows: EXTERN (* PRINT THE TOTAL AND AVERAGE OF 4 NUMBERS *) PROCEDURE XDEMO (A,B,C,D : REAL ); VAR TOTAL : REAL; BEGIN TOTAL := A + B + C + D; WRITELN('TOTAL =',TOTAL, ' AVERAGE =',TOTAL / 4.0); END;. ****** IMPORTANT ****** READ THE FOLLOWING CAREFULLY JRT Pascal external procedures can access all of the global variables in the main program. The GLOBAL VARIABLES are those IN THE MAIN PROGRAM DECLARED BEFORE ANY PROCEDURE OR FUNCTION DECLARATIONS. They are variables that are available globally and not only local to some procedure. In the preceding example, TOTAL is a local variable - it is not accessible outside of the procedure XDEMO. To access global variables or files, their declarations are inserted in the external procedure file AFTER the word EXTERN and BEFORE the procedure header. The three declaration sections CONST, TYPE, VAR may be inserted at this point. They must be identical to the global declarations in the main program, except that additional constants and type identifiers may be added here. Type identifiers may be required in the procedure header parameter list or in a function return value declaration. The declaration of these type identifiers should appear IN THE SAME LOCATION as the global declarations -- just after EXTERN. SECTION 12: External Procedures and Functions JRT Pascal User's Guide version 3.0 NOT FOR SALE -138- EXTERN CONST NAME_SIZE = 32; TYPE NAME = ARRAY [1..NAME_SIZE] OF CHAR; CUSTOMER_RECORD = RECORD CUST_NAME, CUST_ADDR : NAME; BALANCE : REAL; END; VAR (* MAIN PROGRAM GLOBAL VARIABLES *) CUSTOMER_LIST : ARRAY [1..100] OF CUSTOMER_RECORD; (**** SEARCH CUSTOMER LIST FOR GIVEN NAME ****) FUNCTION SEARCH ( N : NAME ) : CUSTOMER_RECORD; VAR I : INTEGER; BEGIN I:=1; WHILE (N <> CUSTOMER_LIST[I].CUST_NAME AND (I <= 100) DO I:=I+1; IF N = CUSTOMER_LIST[I].CUST_NAME THEN SEARCH:=CUSTOMER_LIST[I] ELSE SEARCH:=' '; END;. SECTION 12: External Procedures and Functions JRT Pascal User's Guide version 3.0 NOT FOR SALE -139- 12.2 Referencing external procedures and functions External procedures and functions MUST be declared in the main programs which reference them. Their declaration is identical to a regular procedure except that the entire body of the procedure is replaced with the reserved word EXTERN. PROCEDURE PLOTTER ( X,Y : INTEGER ); EXTERN; FUNCTION CUBEROOT ( A : REAL ): REAL; EXTERN; For clarity, it is useful to group all external procedure declarations as the first procedure declarations in the program. External procedures may reference other external procedures if appropriate declarations are included in the referencing procedure. EXEC indentifies external procedures by a SEQUENCE NUMBER. External procedures SHOULD ALWAYS BE DECLARED IN THE SAME SEQUENCE, in the main program or in another external procedure. NOTE that THE USER MUST ENSURE that external procedure declarations and parameter lists are CONSISTENT among different files, since the compiler does not validate this. SECTION 12: External Procedures and Functions ations and parameter lists are CONSISTENT among different files, since the compiler does