Search code examples
cobol

Is there a way to parameterize functions in COBOL?


I'm coding routines like:

READ-A.
       READ FILE-A
           AT END
             MOVE 1 TO EOF-A
           NOT AT END 
             ADD 1 TO CN-READ-A
       END-READ.
F-READ-A. EXIT.

to read several files and I was wondering if there's a way to code a routine that is able to read the filename from a variable so I don't have to code the same thing for each file. Thanks!


Solution

  • One solution as said above is to use multiple programs or nested program, for which I have included an example below, which is solution 1.

    Another solution is to COBOL classes, which might not be to your liking but I like them, so I've included an example, which is solution 2.

    Solution 1:

       program-id. TestProgram.
       working-storage section.
       01 file-name        pic x(128).
       01 file-lines       pic 9(9).
       procedure division.
           move 0 to file-lines
           move "d:\rts_win32.txt" to file-name
           call "program1" using file-name file-lines
           display file-lines
           stop run
       end program TestProgram.
    
       program-id. Program1.
    
       file-control.
           select file-a assign to myfile
               organization is line sequential.
    
       data division.
       fd file-a.
       01 file-a-line      pic x(80).
    
       working-storage section.
       01 EOF-A            pic 9 value 0.
       linkage section.
       01 lk-filename      pic x(128).
       01 CN-READ-A        pic 9(9).
       procedure division using lk-filename
                                CN-READ-A.
    
           move lk-filename to myfile
           open input file-a
    
           perform READ-A until EOF-A equals 1
           close file-a
           goback.
    
           READ-A.
           READ FILE-A
               AT END
                 MOVE 1 TO EOF-A
               NOT AT END 
                 ADD 1 TO CN-READ-A
           END-READ.
           F-READ-A. 
           EXIT.
    
    
       end program Program1.
    

    Solution 2

      program-id. TestProgram.:
       working-storage section.
       01 file-counter type FileLineCounter.
    
       procedure division.
           set file-counter to new type FileLineCounter("d:\rts_win32.txt")
           display file-counter::LineCount
           stop run
       end program TestProgram.
    
       class-id FileLineCounter.
    
       file-control.
           select file-a assign to myfile
               organization is line sequential.
    
       data division.
       fd file-a.
       01 file-a-line      pic x(80).
    
       working-storage section.
    
       01 cn-read-a binary-long property as "LineCount".
    
       method-id New.
       01 EOF-A            pic 9 value 0.
       procedure division using by value filename as string.
    
           set myfile to filename
           open input file-a
    
           perform READ-A until EOF-A equals 1
           close file-a
           goback.
    
           READ-A.
           READ FILE-A
               AT END
                 MOVE 1 TO EOF-A
               NOT AT END 
                 ADD 1 TO CN-READ-A
           END-READ.
           F-READ-A. 
           EXIT.
    
       end method.
    
       end class.