Skip to main content

Problem:

The following example is taken from the Net Express help and shows how to use user defined functions in COBOL.  This syntax is part of the ISO2002 standard but what additional compiler directives do I need to compile and run the program?

FUNCTION-ID. factorial.                           

DATA DIVISION.                                    

LINKAGE SECTION.                                  

01  parm1 BINARY-LONG.                            

01  fact  BINARY-LONG.                            

PROCEDURE DIVISION USING parm1 RETURNING fact.    

   IF parm1 = 0                                   

      COMPUTE fact = 1                            

   ELSE                                           

      COMPUTE fact = parm1 * factorial (parm1 - 1)

   END-IF                                         

   EXIT FUNCTION.                                 

END FUNCTION factorial.                           

PROGRAM-ID. program-1.                            

ENVIRONMENT DIVISION.                             

CONFIGURATION SECTION.                            

REPOSITORY.                                       

    FUNCTION factorial.                           

DATA DIVISION.                                    

WORKING-STORAGE SECTION.                          

01  i  BINARY-LONG.                               

PROCEDURE DIVISION.                               

    COMPUTE i = factorial (10)                    

    .                                             

END PROGRAM program-1.  

Resolution:

To compile and run this program you need to use the REPOSITORY"UPDATE ON" directive so that a repository file is created for the function.  The repository file (.RDF) will be created in the project folder unless the RDFPATH directive is also specified.

You also need ensure that the source file is called the same name as the main PROGRAM-ID.  If not, then you will get a 173 error when you try to run/animate the code.

Old KB# 1415