Variadic Functions in COBOL

Here’s the translation of the provided code example to COBOL:

Here’s a function that will take an arbitrary number of integers as arguments.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. VariadicFunctions.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 nums PIC S9(9) COMP OCCURS 10 TIMES.
       01 nums-size PIC S9(9) COMP.
       01 total PIC S9(9) COMP.
       01 i PIC S9(4) COMP.
       
       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL i > nums-size
               ADD nums(i) TO total
           END-PERFORM
           DISPLAY total.
           STOP RUN.

       ENTRY "C" USING BY REFERENCE nums, nums-size.

Variadic functions can be called in the usual way with individual arguments.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. Main.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 nums1 PIC S9(9) COMP OCCURS 2.
       01 nums1-size PIC S9(9) COMP VALUE 2.
       01 nums2 PIC S9(9) COMP OCCURS 3.
       01 nums2-size PIC S9(9) COMP VALUE 3.
       01 nums3 PIC S9(9) COMP OCCURS 4.
       01 nums3-size PIC S9(9) COMP VALUE 4.

       PROCEDURE DIVISION.
           MOVE 1 TO nums1(1).
           MOVE 2 TO nums1(2).
           CALL 'VariadicFunctions' USING nums1, nums1-size.
           
           MOVE 1 TO nums2(1).
           MOVE 2 TO nums2(2).
           MOVE 3 TO nums2(3).
           CALL 'VariadicFunctions' USING nums2, nums2-size.
           
           MOVE 1 TO nums3(1).
           MOVE 2 TO nums3(2).
           MOVE 3 TO nums3(3).
           MOVE 4 TO nums3(4).
           CALL 'VariadicFunctions' USING nums3, nums3-size.
           
           STOP RUN.

To run the program, compile the COBOL code and then execute it.

$ cobc -x VariadicFunctions.cob Main.cob
$ ./Main
3
6
10

This example demonstrates how to translate a variadic function concept to COBOL. Although COBOL does not have built-in support for variadic functions, similar functionality can be achieved using arrays and dynamic calls with passed arguments.