IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Possible to do recursion..


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
smile_rajeev

New User


Joined: 22 Apr 2005
Posts: 24

PostPosted: Tue Aug 23, 2005 6:28 pm
Reply with quote

Is it possible to call main-para by itself (just like recursion)
in cobol program.if so, is the cobol have a stack concept.
Back to top
View user's profile Send private message
jz1b0c

Active User


Joined: 25 Jan 2004
Posts: 160
Location: Toronto, Canada

PostPosted: Tue Aug 23, 2005 10:08 pm
Reply with quote

Rajeev,

Its possible to do recursion in COBOL.

Here is the example to do so. Cobol also has stack concept
but this is object oriented cobol, one can even write methods and classes.

I believe there are better ways to write a recursive program than the one below. Below one is just an example for the concept.

ID DIVISION.
PROGRAM-ID RECURSVE RECURSIVE.
*** SAMPLE RECURSIVE.
AUTHOR. JZ1B0C.
INSTALLATION. MY OWN COMPANY.
DATE-WRITTEN. 08/23/05.
DATE-COMPILED.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
DATA DIVISION.
******************************************************************
* WORKING STORAGE *
******************************************************************

WORKING-STORAGE SECTION.
*
01 WS01-WORK-AREA.
05 WS-NUM PIC 9(02) VALUE 4.
05 WS-ITEM PIC 9(2) VALUE 20.
05 READ-COUNT VALUE ZERO PIC S9(13) COMP-3.
05 WS-FACTORIAL VALUE ZERO PIC S9(13) COMP-3.
05 WS-RESULT VALUE +1 PIC S9(13) COMP-3.
**********************************************************

LINKAGE SECTION.
01 LS-VAR.
05 LS-LENGTH PIC S9(04) COMP.
05 LS-NUM PIC 9(02).

**********************************************************
**********************************************************

PROCEDURE DIVISION USING LS-VAR.
IF LS-NUM <= 1 THEN
GOBACK.
COMPUTE WS-RESULT= WS-RESULT * LS-NUM
COMPUTE LS-NUM = LS-NUM - 1.
CALL 'RECURSVE' USING LS-VAR.
DISPLAY ' FACTORIAL : ' WS-RESULT.
GOBACK.
Back to top
View user's profile Send private message
smile_rajeev

New User


Joined: 22 Apr 2005
Posts: 24

PostPosted: Wed Aug 24, 2005 12:43 pm
Reply with quote

Thanks for reply with a new concept which i dont know.

Is it possible to call Main-para by itself without using call statement..
Using perform statement..

Eg:
Main-para.
statements for checking the if conditions..
if <cond>
stmts...
else
perform main-para
endif.

question:

1. will it work this way???
2. will it work in VS COBOLII???

Anyone could u clarify my doubt.
Back to top
View user's profile Send private message
jz1b0c

Active User


Joined: 25 Jan 2004
Posts: 160
Location: Toronto, Canada

PostPosted: Wed Aug 24, 2005 7:10 pm
Reply with quote

Rajeev,

It doesn't work this way..

Perhaps you can use "Go To" to achieve this..
Back to top
View user's profile Send private message
smile_rajeev

New User


Joined: 22 Apr 2005
Posts: 24

PostPosted: Sun Aug 28, 2005 7:25 pm
Reply with quote

Thanks for ur reply..
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
This topic is locked: you cannot edit posts or make replies. DB2 query Using Recursion, Converting... DB2 3
Search our Forums:

Back to Top