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

Can we use perform in perform statement


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

New User


Joined: 18 May 2010
Posts: 12
Location: chennai

PostPosted: Tue May 18, 2010 3:31 pm
Reply with quote

can we use recursion ( performing para X in the same para X)
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue May 18, 2010 4:25 pm
Reply with quote

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
View user's profile Send private message
Pravesh

New User


Joined: 30 Jul 2009
Posts: 32
Location: Gurgaon

PostPosted: Tue May 18, 2010 5:15 pm
Reply with quote

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
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Wed May 19, 2010 1:23 am
Reply with quote

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
View user's profile Send private message
madhus

New User


Joined: 18 May 2010
Posts: 12
Location: chennai

PostPosted: Fri May 21, 2010 1:39 pm
Reply with quote

Could you please suggest alternative way for recursion
Back to top
View user's profile Send private message
madhus

New User


Joined: 18 May 2010
Posts: 12
Location: chennai

PostPosted: Fri May 21, 2010 1:55 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri May 21, 2010 4:33 pm
Reply with quote

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
View user's profile Send private message
mallik4u

New User


Joined: 17 Sep 2008
Posts: 75
Location: bangalore

PostPosted: Fri May 21, 2010 4:53 pm
Reply with quote

is it not possible to keep UNTIL condition on the initial PERFORM step?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri May 21, 2010 8:45 pm
Reply with quote

Hello,

Which part of "Don't do that" was not clear icon_confused.gif

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
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Sat May 22, 2010 12:45 am
Reply with quote

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
View user's profile Send private message
madhus

New User


Joined: 18 May 2010
Posts: 12
Location: chennai

PostPosted: Wed May 26, 2010 11:34 am
Reply with quote

Can anyone explain me(in simple terms) why recursion should not be used . What rae the disadvantages
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Wed May 26, 2010 4:22 pm
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed May 26, 2010 8:28 pm
Reply with quote

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
View user's profile Send private message
rocky_balboa

New User


Joined: 24 Mar 2010
Posts: 61
Location: Cape of Good Hope

PostPosted: Fri May 28, 2010 1:21 am
Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri May 28, 2010 3:19 am
Reply with quote

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
View user's profile Send private message
Ronald Burr

Active User


Joined: 22 Oct 2009
Posts: 293
Location: U.S.A.

PostPosted: Fri May 28, 2010 4:12 am
Reply with quote

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
View user's profile Send private message
rocky_balboa

New User


Joined: 24 Mar 2010
Posts: 61
Location: Cape of Good Hope

PostPosted: Fri May 28, 2010 11:23 pm
Reply with quote

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
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
No new posts Use of Perform Thru Exit COBOL Programming 6
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts Relate COBOL statements to EGL statement All Other Mainframe Topics 0
No new posts process statement for SUPREC, CMPCOLM... TSO/ISPF 4
No new posts SYNCSORT/ICETOOL JOINKEYS SORT Statem... DFSORT/ICETOOL 13
Search our Forums:

Back to Top