|
I personally would prefer this solution with service program before the solution with tle LOCAL-STORAGE SECTION from ILE COBOL Programmers Guide. The service program is here needed, because without the service program we could not realise calling with returning a value, i.e. CALL PROCEDURE ... RETURNING INTO... Only the ILE-COBOL procedure can return a value and the procedure needs to be placed in a service program. On the one side using the procedures and service programs makes possible porcedures/functions with local variables, which brings to the COBOL-programming more elegancy and makes possible to solve e.g. such recursion problems at analogical way as to solve it in any other programming language like Python, C, Pascal ... On the other side however, using the procedures and service programs needs more effort to create the program: First you need to create the modules with CRTCBLMOD, then to create or update your service program with CRTSRVPGM or UPDSRVPGM and finally to link the entry module with the service program together in a runable program with CRTPGM. :-) Regards Roman "Shijith Chand" <shijith_c@xxxxxxxxxxx> Sent by: cobol400-l-bounces@xxxxxxxxxxxx 05.01.2006 06:40 Please respond to COBOL Programming on the iSeries/AS400 <cobol400-l@xxxxxxxxxxxx> To cobol400-l@xxxxxxxxxxxx cc Subject Re: [COBOL400-L] Back with recursion in COBOL again! Hello Roman, sorry I checked the mail a bit late.Thanks a lot for taking the pain of giving such an elaborate solution. It really worked! Now I realise ILE version of AS/400 Languages are feature rich enough to be at par with any other open system languages like c or java. Thanks for the Enlightenment! Regards, s.chand >--------------------------------------------------------------------------------------------------- >------------------------------------------------------------------------------------------------------ >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 _________________________________________________________________ A Camera Sells every 2 Minutes on eBay. Get Your's at a Great Price Now! http://adfarm.mediaplex.com/ad/ck/4686-26272-10936-699?ck=Cameras
As an Amazon Associate we earn from qualifying purchases.
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.