View previous topic :: View next topic
|
Author |
Message |
madhus
New User
Joined: 18 May 2010 Posts: 12 Location: chennai
|
|
|
|
can we use recursion ( performing para X in the same para X) |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
If you click on the manuals link at the top of the page, find the COBOL Language Reference manual, you can find:
Quote: |
6.2.27.1 Basic PERFORM statement
The procedures referenced in the basic PERFORM statement are executed once, and control then passes to the next executable statement following the PERFORM statement.
Note: A PERFORM statement must not cause itself to be executed. A recursive PERFORM statement can cause unpredictable results. |
When IBM tells you something produces unpredictable results, wise programmers tread carefully. |
|
Back to top |
|
|
Pravesh
New User
Joined: 30 Jul 2009 Posts: 32 Location: Gurgaon
|
|
|
|
we can use go to PARA-X for recursion purpose but we can not use perform para-X within the para-X.
Regards,
Pravesh |
|
Back to top |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
Recursion normally presume each recursion instance to have local storage.
The code inside a performed paragraph normally reference the module's global storage. |
|
Back to top |
|
|
madhus
New User
Joined: 18 May 2010 Posts: 12 Location: chennai
|
|
|
|
Could you please suggest alternative way for recursion |
|
Back to top |
|
|
madhus
New User
Joined: 18 May 2010 Posts: 12 Location: chennai
|
|
|
|
I have program like below
PERFORM 23000-CHANGE-SHIP-ID THRU 23000-EXIT
23000-CHANGE-SHIP-ID.
IF ( WS-SPSH-SHIP-ID-SFX = '9999')
STRING WS-SPSH-SHIP-I(1:14) WC-AL(WC-INCRI:1) DELIMITED
BY SPACE INTO WS-SPSH-SHIP-I
PERFORM 23000-CHANGE-SHIP-ID THRU 23000-EXIT
ELSE
DISPLAY 'COMING OUT OF CHANGE SHIP ID(FROM MAX)'
END-IF
DISPLAY 'COMING OUT OF CHANGE SHIP ID 'WS-SPSH-SHIP-I.
23000-EXIT.
EXIT.
I am facing looping issues like below
COMING OUT OF CHANGE SHIP ID(FROM MAX)
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
.
.
.
.
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
Can anyone suggest me alternative way for above code( instaed of perform) |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
Why do you think you need recursion at all? Step WAY back and tell us, in simple terms, just what you are attempting to accomplish. IIRC from my computer science classes (so many years ago), recursion can always be replaced by non-recursive code. Since you've already been told it is not a good thing to attempt recursion in COBOL, you need to start looking into ways to do whatever you want to accomplish without recursion. |
|
Back to top |
|
|
mallik4u
New User
Joined: 17 Sep 2008 Posts: 75 Location: bangalore
|
|
|
|
is it not possible to keep UNTIL condition on the initial PERFORM step? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Which part of "Don't do that" was not clear
There is no reason to implement things that IBM has warned not to do. "Unpredictable" means unacceptable to any good developer or management. . . It doesn't matter if it works today - it is still wrong.
One of the most common uses of "recursion" is a Bill of Materials Process (BOMP). There are many implementations of this that do not require code "perform itself". . . |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
madhusudhanreddysurakanti wrote: |
I have program like below
PERFORM 23000-CHANGE-SHIP-ID THRU 23000-EXIT
23000-CHANGE-SHIP-ID.
IF ( WS-SPSH-SHIP-ID-SFX = '9999')
STRING WS-SPSH-SHIP-I(1:14) WC-AL(WC-INCRI:1) DELIMITED
BY SPACE INTO WS-SPSH-SHIP-I
PERFORM 23000-CHANGE-SHIP-ID THRU 23000-EXIT
ELSE
DISPLAY 'COMING OUT OF CHANGE SHIP ID(FROM MAX)'
END-IF
DISPLAY 'COMING OUT OF CHANGE SHIP ID 'WS-SPSH-SHIP-I.
23000-EXIT.
EXIT.
I am facing looping issues like below
COMING OUT OF CHANGE SHIP ID(FROM MAX)
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
.
.
.
.
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
COMING OUT OF CHANGE SHIP ID EXPXXP10509792B
Can anyone suggest me alternative way for above code( instaed of perform) |
Since you are never getting a new value for "WS-SPSH-SHIP-ID-SFX " you would never get out of the loop! I'm suprised that compiler even accepted that code. |
|
Back to top |
|
|
madhus
New User
Joined: 18 May 2010 Posts: 12 Location: chennai
|
|
|
|
Can anyone explain me(in simple terms) why recursion should not be used . What rae the disadvantages |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
From the manual quoted earlier:
Quote: |
A recursive PERFORM statement can cause unpredictable results. |
Unpredictable results could include anything from code not performing as expected (returning to an unexpected point, for example) to abends. The whole point is that you do not know what the results will be -- NOT a good situation for a program. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
Can anyone explain me(in simple terms) why recursion should not be used . |
Keep in mind that code can be written so that things like a Bill of Materials Process are implemented without a need for code "calling itself".
Having a "drill down" process is fine - just do not implement by calling the code from within itself. |
|
Back to top |
|
|
rocky_balboa
New User
Joined: 24 Mar 2010 Posts: 61 Location: Cape of Good Hope
|
|
|
|
Quote: |
why recursion should not be used |
in COBOl....
A subroutine call involves a stack operation.At runtime the system pushes a return address on the stack.When the subroutine exits , it pops the entire stack and transfers the control. If one subroutine calls second subr,the second stack frame is pushed on top of the first and so on.As COBOL does not follow this fashion...it does not support recursions directly and can cause unpredictable results.
But this does not mean that you can't implement recursion in COBOL.You need to have appropriate data structures( for push(call) and pop(return)). But it won't be as graceful as other languages like C which directly support it. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
Rocky Balboa,
Enterprise COBOL brought RECURSIVE to COBOL on IBM.
The reason PERFORM is not suitable for RECURSIVE programming,
is
as you said - concept of stack (multiple returns) and data integrity,
that a PERFORM can not invoke anything that could establish
an environment that would support the implementation of RECURSIVE.
The only way IBM provides RECURSIVE CALLs in COBOL is to CALL a module that has been 'built' for RECURSIVE CALLs.
Do a search in both
Programmers Guide
and the
Programmers Reference
for RECURSIVE. |
|
Back to top |
|
|
Ronald Burr
Active User
Joined: 22 Oct 2009 Posts: 293 Location: U.S.A.
|
|
|
|
madhusudhanreddysurakanti wrote: |
Can anyone suggest me alternative way for above code( instaed of perform) |
Rather than continue the sidebar discussion of why Recursion is/is not supported in COBOL - here is the answer to that question (untested):
Code: |
PERFORM 23000-CHANGE-SHIP-ID THRU 23000-EXIT
23000-CHANGE-SHIP-ID.
PERFORM
WITH TEST BEFORE
UNTIL WS-SPSH-SHIP-ID-SFX NOT = '9999'
STRING WS-SPSH-SHIP-I(1:14) WC-AL(WC-INCRI:1)
DELIMITED BY SPACE
INTO WS-SPSH-SHIP-I
DISPLAY 'COMING OUT OF CHANGE SHIP ID(FROM MAX)'
END-PERFORM
DISPLAY 'COMING OUT OF CHANGE SHIP ID ' WS-SPSH-SHIP-I.
23000-EXIT.
EXIT. |
Note that the IF statement comparison operator was negated to 'NOT =' and made part of a perform operation. |
|
Back to top |
|
|
rocky_balboa
New User
Joined: 24 Mar 2010 Posts: 61 Location: Cape of Good Hope
|
|
|
|
dbzTHEdinosauer
Whatever I posted was in line of the motion of the discussion....that is reagrding hazards of a recursive PERFORM and not the availablity of RECURSION per se.
Of course ER-COBOL supports indirect recursion(through CALLs)....which is different from programming(as in C and other languages) perspective and the definition derived from mathematical induction.
regret that it caused confusion... |
|
Back to top |
|
|
|