View previous topic :: View next topic
|
Author |
Message |
smile_rajeev
New User
Joined: 22 Apr 2005 Posts: 24
|
|
|
|
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 |
|
|
jz1b0c
Active User
Joined: 25 Jan 2004 Posts: 160 Location: Toronto, Canada
|
|
|
|
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 |
|
|
smile_rajeev
New User
Joined: 22 Apr 2005 Posts: 24
|
|
|
|
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 |
|
|
jz1b0c
Active User
Joined: 25 Jan 2004 Posts: 160 Location: Toronto, Canada
|
|
|
|
Rajeev,
It doesn't work this way..
Perhaps you can use "Go To" to achieve this.. |
|
Back to top |
|
|
smile_rajeev
New User
Joined: 22 Apr 2005 Posts: 24
|
|
|
|
Thanks for ur reply.. |
|
Back to top |
|
|
|