The COBOL-85 Tutorial, Chapter 7

Subprograms.

Copyright © 1998-2017

Kim Kjærsulf.


This chapter (TOC)

Small is exquisite
Local versus external subprograms.
Declaration.
CALL and types of parameter.
CALL and CANCEL of external subprograms.
Example.
Exercises

Small is exquisite

[This chapter] [TOC] [Tutorial]


Even though COBOL is traditionally structured using paragraph blocks, there are still situations in which it is a good idea to have facilities for making calls between separate programs.

First, there are design criteria in which many small programs with well-defined functions are preferable.

Second, there are limits to just how many large programs a computer can cope with, a limit which is of course very much dependent on computer size and the operating system used.

This chapter deals with the COBOL facilities for packaging several COBOL programs together when they are run.



Local versus external subprograms.

[This chapter] [TOC] [Tutorial]


Local subprograms roughly correspond to the procedure concept in Pascal, used where there are several "programs" in the same source file. These communicate with each other and with the main program via parameters.

External subprograms are compiled independently and are stored in a file of their own.



Declaration.

[This chapter] [TOC] [Tutorial]


Subprograms are loaded dynamically, which means that their physical file name and the name used in the PROGRAM-ID must be the same. A subprogram is different to a normal program in that:

A special characteristic of local subprograms is that the source file must start with a compiler directive: $SET NESTCALL, where the $ character must be placed in position 7. At the end of every program in the source file it is necessary to specify which program is being ended (scope rules).

It is possible to give a subprogram more than one entry point, which is relevant, for example, when implementing abstract data types. However, remember that the program must first be called by its primary name, i.e. that which corresponds to PROCEDURE DIVISION and the name used in PROGRAM-ID.

These alternative entry points are established using the ENTRY ... USING statement.



CALL and types of parameter.

[This chapter] [TOC] [Tutorial]


When using CALL the default method for transferring parameters is "by reference", but it is also possible to use "by value" (which is the same as "by content").

It is also possible to define variables (including files) so that they are globally accessible - though this should be avoided as it undermines the quality of the program design.



CALL and CANCEL of external subprograms.

[This chapter] [TOC] [Tutorial]


When calling an external program (one that has its own source file), it will be loaded with all of the variables having the value stated in the corresponding "VALUE" clause. On subsequent occasions when the program is called it will remember the values of the last call unless the program contains an INITIAL after the PROGRAM-ID or if a CANCEL has been carried out on the program since the last load. The primary purpose of CANCEL is to remove the program from memory.


This chapter

Example.

[This chapter] [TOC] [Tutorial]


An example of a COBOL program using a local subprogram for entering data. All elements related to the use of local subprograms are nested.

      $SET NESTCALL      

      *
       identification division.
      *  ------ program identification.
       program-id.    subprog.
       author.        kik.
     
       environment division.
      *  ------  program environment.
       configuration section.
       special-names.
            console is crt
            decimal-point is comma.
     
       data division.
      *  ------  variable declarations .
       working-storage section.
       77  teller         pic 9 comp.
      
       
	 procedure division.
      *  ------  executable instructions.
       main section.
           display space.
           display "Hello world from main program" 
                         line 1 position 30.
           perform varying teller from 1 by 1 
                   until teller > 3
                   call "ENTER" using teller

                   end-perform.
           stop run.

          end program subprog.

      * See next page for the subprogram.  
      *
      * A local COBOL program.
      *
       identification division.
       program-id.    enter initial.
       environment division.
      *  ------  program environment.
       configuration section.
       special-names.
            console is crt
            decimal-point is comma.
     
       data division.
       working-storage section.
       77  text-field     pic x(10).
       77  disp-line      pic 99 comp.

       linkage section.
       01  tel            pic 9 comp.

      * Note:  the data description must be identical 
      *        with the description in the calling program.

       procedure division using tel.
       enter section.
       The-begin.
           display "In subprogram" line 5 position 10.
           accept text-field line 10 position 10.
           add 10, tel giving disp-line.
           display text-field line disp-line position 10.
       The-end.

           goback.

Exercise 1.

[This chapter] [TOC] [Tutorial]


Enter, compile and run the examples in this chapter.


Copyright © 1998-2017

Kim Kjærsulf


Last Updated october 3, 2003
For more information contact: Kim Kjærsulf