In COBOL, is it possible to recursively call a paragraph?
Asked Answered
A

3

5

In COBOL for an IBM Mainframe is it possible to call a paragraph recursively?

200-PARAGRAPH SECTION.

    IF WS-COUNTER < 10
       ADD 1 TO WS-COUNTER
       PERFORM 200-PARAGRAPH
    ELSE
       DISPLAY 'I'M DONE'
    END-IF.

 200-EXIT.
    EXIT.
Anett answered 6/1, 2015 at 20:40 Comment(3)
No. An imlmenehtstion is free to support recursive PEFFROM, but it isn't required.Febrile
@EJP imlmenehtstion?Salyer
"Implementation" is my best guess :-DFikes
T
5

Check the current COBOL Language Reference, but AFAIR a para cannot PERFORM itself. The wording is something like you will get unpredictable results, as recursion is not supported.

If you need to wind over the same code perhaps refactoring to use PERFORM UNTIL is what you need?

Trinidadtrinitarian answered 6/1, 2015 at 21:7 Comment(1)
Note: A PERFORM statement must not cause itself to be executed. A recursive PERFORM statement can cause unpredictable results.Salyer
S
4

@jdv is correct.

If you want recursion, you can make an entire program recursive, look at the same manual (the Enterprise COBOL Language Reference) and it's sister, the Enterprise COBOL Programming Guide, specifically at the PROGRAM-ID and its options.

However I'd only recommend recursion if you have no other way to do it, which is rare. There is a substantial overhead in having a recursive program.

You show this:

200-PARAGRAPH SECTION.

The 200-PARAGRAPH bit is just a label, just a name. The word SECTION tells you what it is, and it isn't a paragraph. You do have a paragraph within that SECTION.

And No, a SECTION cannot be used recursively either.

SECTIONs in the PROCEDURE DIVISION used to be more important. With various changes in the COBOL 1985 Standard, SECTIONs became less important.

This is how I would code that (subject to local standards):

    PERFORM                      200-descriptive-and-meaningful-name
        invariant-data-item-with-a-VALUE-of-ten
                                 TIMES
    DISPLAY "I'M DONE"

200-descriptive-and-meaningful-name.
    CONTINUE (just representative of whatever code you need)
    .

Or an inline-perform doing the same thing (I like PERFORMing paragraphs as it assists the self-documenting of the program).

Note that the names are to explain to you reading this. You should use different names which are meaningful to the business task at hand.

The Language Reference and Programming Guide are substantial documents, available for free as PDFs to download, or searchable on-line. Make these one point-of-call before SO. Also don't forget you should have colleagues who can also help.

Salyer answered 6/1, 2015 at 23:48 Comment(1)
I dunno, your names are much more descriptive than many I've come across during my decades of looking at COBOL programs. :)Baroscope
B
2

I'm not sure that the code you're showing technically qualifies as recursion. However, the following code works and is platform independent.

       PERFORM
         VARYING ws-counter FROM 1 BY 1
           UNTIL ws-counter > 9
       END-PERFORM.
       DISPLAY 'I' QUOTE 'M DONE'.

If you want to really start a fight between COBOL programmers, read this.

   200-SECTION SECTION.
   200-PARAGRAPH.
       IF ws-counter < 10
           ADD 1 TO ws-counter
           GO TO 200-PARAGRAPH
       ELSE
           DISPLAY 'I' QUOTE 'M DONE'
       END-IF.

Although I'd probably prefer this, if one were to go with a GO TO approach.

   200-SECTION SECTION.
   200-PARAGRAPH.
       IF ws-counter < 10
           ADD 1 TO ws-counter
           GO TO 200-PARAGRAPH
       END-IF.
       DISPLAY 'I' QUOTE 'M DONE'.

I must ask what is it that you were trying to accomplish? Assuming your code worked, all it does is count to ten. This does that and is simpler still.

       MOVE 10 TO ws-counter.
       DISPLAY 'I' QUOTE 'M DONE'.

I think this is what I would recommend.

       05  ws-counter               PIC 9(02).
       05  max-passes               PIC 9(02) BINARY VALUE 10.

       MOVE ZERO TO ws-counter.
       PERFORM 200-PARAGRAPH
           UNTIL ws-counter >= max-passes.
       DISPLAY 'I' QUOTE 'M DONE'.

   200-PARAGRAPH.
       ADD 1 TO ws-counter.
Bendigo answered 16/1, 2015 at 23:22 Comment(4)
The section/paragraph is performing itself, unless my understanding of recursion is wrong that is recursion. This was just a technical question as I'm coming from Java and I'm trying to figure out how to do things in Cobol.Anett
The documentation from IBM calls it recursion. I know what you mean, but it was just an example of a paragraph/SECTION PERFORMing itself directly. Making it calculate a factorial wouldn't change anything, it still can't be done like that. I do like the final cut-to-the-chase anyway :-) There's often code where something similar can be done...Salyer
Also PERFORM 10 TIMES ADD 1 TO ws-counter END-PERFORM DISPLAY "I'M DONE" is a further intermediary to your last :-) Prefixed of course by INITIALIZE ws-counter.Salyer
ADD 10 TO ws-counter. DISPLAY "I'M DONE". :-pFikes

© 2022 - 2024 — McMap. All rights reserved.