Developing User-Written Functions
The Need for Functions
The FUNCTION clause, which is described from the user's point of view under FUNCTION clause, allows you to develop your own function routines as well as to use certain built-in functions, such as DAY, MDATE, and TIME. A user-written function allows you to display report data in a way peculiar to your installation.
User-written functions are especially effective when needed in many separate programs, as they provide a standard way of displaying certain codes or other items of information peculiar to the user site. Indeed a commitment to "think functionally" may greatly improve efficiency throughout a programming department. To convince yourself of this, consider the following scenario, before the introduction of functions:
Many report programs require the programmer to code a table of descriptions or names directly into the Working-Storage of the program. Perhaps at first only a few report programs of this type are written, so that, even when some extra codes are added to the table and all those programs have to be re-compiled, no one objects too strongly.
Suddenly a large number of new programs are written that require the same names. Some programmers code the table from scratch; others prefer to take a copy from a working program. Business conditions then change and many new codes are added to the table. Dozens of programs have to be changed. Any change requires retesting of whole suites of programs. Each program has its own particular way of encoding the table, so an amendment becomes a major undertaking. At the end of it, no one is absolutely sure that all the programs affected have been found and amended.
When the next major change becomes necessary, some one decides that from now on all the codes will be held in a static "table file". New programs are guaranteed to be independent of the codes and names, but there is a penalty to be paid in each program, because of all the extra steps needed to OPEN, READ and CLOSE the file in the program. Later, there might be a general change from traditional files to a database, so all the programs have to be amended again.
Through the use of functions from the outset, programs can be made independent of all these changes. The program will be shorter and the programmer need not worry about how the report field is created. Control over specification and change becomes more assured.
How to Write a Function Routine
As designer of a function routine, you have a free hand to transform the data in any desired way, provided that the print data that results fits within the limits of the print field described by the programmer in the associated PICTURE clause.
Most function routines are COBOL subprograms with a standard LINKAGE SECTION, but they may also be written in any programming language that can be CALLed by COBOL. The use of COBOL normally prevents the number of parameters passed to the FUNCTION from being variable (a minor benefit). Since they are "normal" programs, function routines may use any files or databases that are available to other programs, may CALL other subprograms, and may store intermediate results in their own Working-Storage.
The number of relevant characters returned in the output (report) field may vary, thus enabling the same function routine to handle different sizes and formats, for example "date" formats with different arrangements of day, month, and year. The size and format of the user-coded parameters (if any) are usually prescribed by the function routine and cannot vary, unless the designer decides to specify an additional parameter to indicate which of a choice of formats the input is in. All parameters are passed "by reference" so, although they normally pass data only into the function routine, they may also be used to pass updated data back to the user program.
The function routine is used in the program via the FUNCTION clause, and the programmer need not be aware of the precise mode of operation of the function routine. The description of the built-in functions is also given in 3.7 FUNCTION clause. User-written functions may be similarly specified and added to this publication in an appropriate place.
The program name of the function routine should be Rnxxxxxx, where xxxxxx is the mnemonic name of the function of no more than 6 alphanumeric characters and n is the number of parameters to the function. For example, if the function is called COUNTY and takes one parameter, you must write a function module with the program-id: R1COUNTY otherwise. If the same function has a variable number of parameters, separate function routines must be written (although they may all call a common subordinate routine).
The parameters to the function routine are as follows: