× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I found this example for using recursive calls 
in ILE COBOL Programmers Guide (SC09-2540-04) on Page 215:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORIAL RECURSIVE. 

       ENVIRONMENT DIVISION. 
       CONFIGURATION SECTION. 
          SOURCE-COMPUTER. IBM-ISERIES.
          OBJECT-COMPUTER. IBM-ISERIES.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUMB PIC 9(4) VALUE 5. 
       01 FACT PIC 9(8) VALUE 0. 

       LOCAL-STORAGE SECTION. 
       01 NUM PIC 9(4). 

       PROCEDURE DIVISION. 
           MOVE NUMB TO NUM. 
           IF NUMB = 0 
             MOVE 1 TO FACT 
           ELSE 
             SUBTRACT 1 FROM NUMB
             CALL "FACTORIAL" 
             MULTIPLY NUM BY FACT 
           END-IF. 
           DISPLAY NUM "! = " FACT. 
           GOBACK. 
       END PROGRAM FACTORIAL.

Compile it with CRTBNDCBL and it works. The output is:
     0000! = 00000001
     0001! = 00000001
     0002! = 00000002
     0003! = 00000006
     0004! = 00000024
     0005! = 00000120

The program uses for recursion calls the  RECURSIVE Clause in PROGRAM-ID 
and the LOCAL-STORAGE SECTION.

Regards 

Roman




"Shijith Chand" <shijith_c@xxxxxxxxxxx> 
Sent by: cobol400-l-bounces@xxxxxxxxxxxx
30.12.2005 15:08
Please respond to
COBOL Programming on the iSeries/AS400 <cobol400-l@xxxxxxxxxxxx>


To
cobol400-l@xxxxxxxxxxxx
cc

Subject
[COBOL400-L] Back with recursion in COBOL again!






Dear all,
          I have tried to implement  factorial function using recursive 
calls in COBOL.But
          I could not get the expected results although recursive calls 
are 
succesfully
          executed. (I have checked this through debugger).

           The problem is because the work variable declared in the 
program 
gets changed
           after the recursive calls.This problem does not happen if I use 

functions in C or Java
           because the work variable's are scoped to the individual 
calls.So 
the work variable
           when changed in called program it still retains  the old value 
in 
the calling PGM.
           But this is not the case in  COBOL/400.

           For example.

           i am enclosing  the whole code along so that you will be able 
to 
figure out what the problem
          is

         program RICUR is the driver program which calls the ricursive pgm
         RICURTEST.

         problem is beacuse the variable WK-NUMBER-STORE in RICURTEST gets 

changed after
         the recursive call to RICURTEST.This generally don't happen in C 
& 
JAVA for reasons I have
         mentioned before.

         Can anybody suggest me a solution for this?
         One option may be use ILE sub procedure to do the same,though I 
haven't tried it.


Program RICUR
------------------------------

IDENTIFICATION DIVISION.
PROGRAM-ID. RICURTEST.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-AS400.
OBJECT-COMPUTER. IBM-AS400.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WK-BIG-NUMBER  PIC 9(10).
01 WK-NUMBER  PIC 9(4).
01 WK-DUMMY  PIC 9(10).
LINKAGE SECTION.
PROCEDURE DIVISION.
     COMPUTE WK-NUMBER = 4.
     COMPUTE WK-BIG-NUMBER = 0.
  CALL 'RICURTEST' USING WK-NUMBER WK-BIG-NUMBER.
    COMPUTE WK-DUMMY = WK-BIG-NUMBER.
STOP-RUN.


Program RICURTEST
----------------------

IDENTIFICATION DIVISION.
PROGRAM-ID. RICURTEST.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-AS400.
OBJECT-COMPUTER. IBM-AS400.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WK-BIG-NUMBER  PIC 9(10).
01 WK-NUMBER  PIC 9(4).
01 WK-NUMBER-STORE PIC 9(4).
LINKAGE SECTION.
01 WSSP-NUMBER PIC 9(4).
01 WSSP-BIGNUMBER PIC 9(10).
PROCEDURE DIVISION USING WSSP-NUMBER WSSP-BIGNUMBER.
   IF WSSP-NUMBER = 1
      COMPUTE WSSP-BIGNUMBER = 1
   END-IF.

   IF WSSP-NUMBER = 2
      COMPUTE WSSP-BIGNUMBER = 2
   END-IF.

    IF WSSP-NUMBER > 2
       COMPUTE WK-NUMBER-STORE = WSSP-NUMBER
       COMPUTE WK-NUMBER = WSSP-NUMBER - 1
       COMPUTE WK-BIG-NUMBER = 0
      CALL 'RICURTEST' USING WK-NUMBER WK-BIG-NUMBER
       COMPUTE WSSP-BIGNUMBER = WK-BIG-NUMBER * WK-NUMBER-STORE
   END-IF.

  GOBACK.


Thanks in anticipation,
shijith

_________________________________________________________________
Sexy, sultry, sensuous. - see why Bipasha Basu is all that and more. Try 
MSN 
Search http://server1.msn.co.in/Profile/bipashabasu.asp


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.