× 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.



Here is better example for factorial, 
more likely to factorial's pseudo(python)code

# factorial
def factorial(n):
  if n == 0:
    result = 1
  else:
    result = n * factorial(n-1)
  return result 

I coded it in COBOL as follows

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORIAL RECURSIVE.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. IBM-AS400.
       OBJECT-COMPUTER. IBM-AS400.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 RESULT       PIC 9(20). 
       01 CALL-RESULT  PIC 9(20).
       01 N-MINUS-1    PIC 9(4).
       LINKAGE SECTION.
       01 N            PIC 9(4).

       PROCEDURE DIVISION USING BY VALUE N RETURNING RESULT.
           IF N = 0
              COMPUTE RESULT = 1
           ELSE
              COMPUTE N-MINUS-1 = N - 1
              CALL PROCEDURE 'FACTORIAL' 
                   USING BY VALUE N-MINUS-1 
                   RETURNING INTO CALL-RESULT 
              COMPUTE  RESULT = N * CALL-RESULT
           END-IF.
           GOBACK.

note that I'm using calling by value n

the caller program is here

       IDENTIFICATION DIVISION.
       PROGRAM-ID. RECURTEST.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER. IBM-AS400.
       OBJECT-COMPUTER. IBM-AS400.
       SPECIAL-NAMES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 N           PIC 9(4).
       01 RESULT      PIC 9(20).
       LINKAGE SECTION.
       PROCEDURE DIVISION.
           PERFORM VARYING N FROM 0 BY 1 UNTIL N = 21
             CALL PROCEDURE 'FACTORIAL' 
                  USING BY VALUE N 
                  RETURNING INTO RESULT
             DISPLAY N "! = " RESULT
           END-PERFORM.
       GOBACK.


now compile the FACTORIAL module, e.g.:
CRTCBLMOD MODULE(EBKPGMP/FACTORIAL) SRCFILE(RM/CBLJAVASRC) 
SRCMBR(FACTORIAL) 
DBGVIEW(*SOURCE) 

create service program TMPPROC, e.g.:
CRTSRVPGM SRVPGM(EBKPGMP/TMPPROC) MODULE(EBKPGMP/FACTORIAL)
EXPORT(*ALL) 

compile the RECURTEST module, e.g: 
CRTCBLMOD MODULE(EBKPGMP/RECURTEST) SRCFILE(RM/CBLJAVASRC) 
SRCMBR(RECURTEST) 
DBGVIEW(*SOURCE) 

and create the program RECURTEST with entry module RECURTEST linking it 
with the saervice program TMPPROC, e.g:
CRTPGM PGM(EBKPGMP/RECURTEST) 
ENTMOD(EBKPGMP/RECURTEST) BNDSRVPGM(EBKPGMP/TMPPROC)

Finally by calling that program CALL RECURTEST you will obtain these 
results:

     0000! = 00000000000000000001
     0001! = 00000000000000000001
     0002! = 00000000000000000002
     0003! = 00000000000000000006
     0004! = 00000000000000000024
     0005! = 00000000000000000120
     0006! = 00000000000000000720
     0007! = 00000000000000005040
     0008! = 00000000000000040320
     0009! = 00000000000000362880
     0010! = 00000000000003628800
     0011! = 00000000000039916800
     0012! = 00000000000479001600
     0013! = 00000000006227020800
     0014! = 00000000087178291200
     0015! = 00000001307674368000
     0016! = 00000020922789888000
     0017! = 00000355687428096000
     0018! = 00006402373705728000
     0019! = 00121645100408832000
     0020! = 02432902008176640000


So, from the above example it follows, that using modules and service 
programs 
the recursion in ILE COBOL is possible in the  similar manner as in the 
other 
languages.
 
 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.