Skip to content

RETURN Statement

The RETURN statement returns records from SORT or MERGE operations to the operating program from within an OUTPUT procedure.

General Format:

       RETURN sort-file-1 RECORD [ INTO data-1 ] 
              AT END statement-1 
              [NOT AT END statement-2 ] 
              [END-RETURN ]

Syntax:

  1. sort-file-n is a file described in the File Section with an SD.
  2. data-n is a data item.
  3. statement-n is an imperative statement.

General Rules:

  1. The RETURN statement can only be used in the OUTPUT PROCEDURE of a SORT or MERGE statement.
  2. The INTO phrase causes the data to be MOVE’d to data-1.
  3. If there is no INTO phrase, the data is returned into the sort-record area in the sort file.
  4. When the end of a sort file is reached, the AT END condition is triggered, otherwise the NOT AT END condition is triggered.
  5. The RETURN statement does not update the File Status variable. An excerpt from the sample program illustrates the use of the RETURN statement in an OUTPUT PROCEDURE. In the example below, the program RETURN's records from the sort file, writes them out to a new file.
       OPEN OUTPUT MEMBER-LIST. 
       PERFORM UNTIL EOF-SORT-FILE 
              RETURN MEMBER-SORT 
                     AT END MOVE "Y" TO OUTPUT-SORT-AT-END 
                     NOT AT END 
                            WRITE SORTED-MEMBER-INFO FROM SORT-DATA 
                     END-RETURN 
              END-PERFORM.

Code Sample:

       IDENTIFICATION DIVISION. 
       PROGRAM-ID. RETURN-1. 
       ENVIRONMENT DIVISION. 
       INPUT-OUTPUT SECTION. 
       FILE-CONTROL. 
      *SORT INPUT FILE 
              SELECT MEMBER-FILE ASSIGN TO "MEMBERSHIP.TXT" 
              ORGANIZATION IS LINE SEQUENTIAL 
              ACCESS IS SEQUENTIAL 
              FILE STATUS IS MEMBERSHIP-STAT. 

      *SORT OUTPUT FILE 
              SELECT MEMBER-LIST ASSIGN TO "MEMBERLIST.TXT" 
              ORGANIZATION IS LINE SEQUENTIAL 
              ACCESS IS SEQUENTIAL 
              FILE STATUS IS MEMBERLIST-STAT. 

      *SORTFILE (SD) 
              SELECT MEMBER-SORT ASSIGN TO "SORT-WORK". 

       DATA DIVISION. 
       FILE SECTION. 
       FD MEMBER-FILE. 
       01 MEMBER-INFOPIC X(40). 

       FD MEMBER-LIST. 
       01 SORTED-MEMBER-INFO PIC X(40). 

       SD MEMBER-SORT. 
       01 SORT-DATA. 
              05 MEMBER-FIRST-NAME PIC X(10). 
              05 MEMBER-LAST-NAME PIC X(20). 
              05 YEAR-JOINED PIC X(4). 
              05 MEMBER-RANK PIC X(6). 

       WORKING-STORAGE SECTION. 
       77 MEMBERLIST-STAT PIC XX. 
              88 EOF-MEMBERLIST VALUE "10". 
       77 MEMBERSHIP-STAT PIC XX. 
              88 EOF-MEMBERSHIP VALUE "10". 
       77 OUTPUT-SORT-AT-END PIC X. 
              88 EOF-SORT-FILEVALUE "Y".
       77 DUMMY PIC X. 

       PROCEDURE DIVISION. 
       MEMBER-SORT-PROC. 

              SORT MEMBER-SORT 
                     ON ASCENDING KEY MEMBER-LAST-NAME 
                     ON DESCENDING KEY MEMBER-RANK 
                     WITH DUPLICATES IN ORDER 
                     INPUT PROCEDURE INPUT-PROC 
                     OUTPUT PROCEDURE OUTPUT-PROC. 

              DISPLAY "SORT-3 FINISHED!" LINE 10 COL 10. 
              ACCEPT DUMMY LINE 10 COL 30. 
              STOP RUN. 

       INPUT-PROC SECTION. 
       INPUT-PROCESS. 

              OPEN INPUT MEMBER-FILE. 
              READ MEMBER-FILE NEXT RECORD. 

              PERFORM UNTIL EOF-MEMBERLIST 
                     RELEASE SORT-DATA FROM MEMBER-INFO 

                     READ MEMBER-FILE NEXT RECORD 
                            AT END MOVE "10" TO MEMBERLIST-STAT 
                     END-READ 
              END-PERFORM. 

              CLOSE MEMBER-FILE. 
              EXIT SECTION. 

       OUTPUT-PROC SECTION. 
       OUTPUT-PROCESS. 
              OPEN OUTPUT MEMBER-LIST. 
              PERFORM UNTIL EOF-SORT-FILE 
                     RETURN MEMBER-SORT 
                            AT END MOVE "Y" TO OUTPUT-SORT-AT-END 
                            NOT AT END 
                                   WRITE SORTED-MEMBER-INFO FROM SORT-DATA 
                     END-RETURN 
              END-PERFORM. 

              CLOSE MEMBER-LIST. 
              EXIT SECTION.
Back to top