View previous topic :: View next topic
|
Author |
Message |
cvnlynn
New User
Joined: 14 Jun 2017 Posts: 31 Location: US
|
|
|
|
What's the correct way to end the REXX program. Thanks. |
|
Back to top |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1787 Location: Bloomington, IL
|
|
|
|
If the program is a top-level routine (i.e., invoked directly from TSO or IRXJCL), there is no difference, although exit may be clearer. Otherwise, of course, return should be used. |
|
Back to top |
|
|
cvnlynn
New User
Joined: 14 Jun 2017 Posts: 31 Location: US
|
|
|
|
Thanks. |
|
Back to top |
|
|
Pedro
Global Moderator
Joined: 01 Sep 2006 Posts: 2596 Location: Silicon Valley
|
|
|
|
Quote: |
If the program is a top-level routine (i.e., invoked directly from TSO or IRXJCL) |
I do not think that is the best description. Within a PDS member, you have a main routine (the first lines of the member) and it can call subroutines that are included also within the member. For this main routine of the member, the EXIT or RETURN do not make a difference. For any internal subroutines, use RETURN. If you use EXIT from an internal subroutine, it will flush the entire member.
It makes no difference if the PDS member was called from TSO or IRXJCL or from a separate rexx PDS member. |
|
Back to top |
|
|
cvnlynn
New User
Joined: 14 Jun 2017 Posts: 31 Location: US
|
|
|
|
so, would it be safe to always use RETURN ? |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2454 Location: Hampshire, UK
|
|
|
|
You RETURN from a sub-routine to a higher level. At the highest level you END (i.e. EXIT) the task (program).
Edit: added (i.e. EXIT) to clarify as per Pedro's comment following. |
|
Back to top |
|
|
Pedro
Global Moderator
Joined: 01 Sep 2006 Posts: 2596 Location: Silicon Valley
|
|
|
|
Quote: |
You RETURN from a sub-routine to a higher level. At the highest level you END the task (program). |
It is not clear what you mean by 'END'; perhaps you meant EXIT. You can issue RETURN from the highest level and it will return the caller which might be the operating system.
I think it is almost always safe to use RETURN.
You might want to issue EXIT during various error situations where you intend to flush the entire program. |
|
Back to top |
|
|
daveporcelan
Active Member
Joined: 01 Dec 2006 Posts: 792 Location: Pennsylvania
|
|
|
|
This my personal preference.
As a former COBOL programmer before coding Rexx full time, I believe in the single entry/single exit philosophy.
I code my programs as such.
I also like one EXIT statement at the end of my main code. This way I can easily tell that everything after that is an internal subroutine.
I use labels to signify the beginning and end of each subroutine.
Here is a sample of my structure. Feel free to use or ignore. Nasty remarks not required.
Code: |
/* REXX PROGRAM A*/
/* MAINLINE CODE */
V1 = 'VALUE1'
CALL SUBROUTINE1
IF V1 = 'ERROR' THEN SIGNAL EXIT99
CALL SUBROUTINE2
EXIT99: NOP
EXIT
/* SUBROUTINE1 CODE */
SUBROUTINE1: NOP
V1 = 'ERROR'
EXIT_SUBROUTINE1: NOP
RETURN
/* SUBROUTINE2 CODE */
SUBROUTINE2: NOP
V1 = 'OK'
EXIT_SUBROUTINE2: NOP
RETURN |
|
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2454 Location: Hampshire, UK
|
|
|
|
According to TRL2:
Quote: |
If no internal routine (subroutine or function) is active, then RETURN has the identical effect on the program that is being executed as EXIT (see page 54). |
But, like Dave, I like to RETURN from subroutines/functions and EXIT from my program at the end. |
|
Back to top |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2147 Location: USA
|
|
|
|
The rule of thumb: use RETURN at every point of normal program/function/(sub)routine end, and use EXIT at every point of abnormal termination, e.g. unrecoverable error encountered. |
|
Back to top |
|
|
Willy Jensen
Active Member
Joined: 01 Sep 2015 Posts: 736 Location: Denmark
|
|
|
|
I like to have a common exit point like in the following snippet, because then I don't have to return up through levels with the bad RC.
Code: |
Call Init
Call Main
Call Close 0 'Normal end of program'
Close:
. . . do whatever cleanup is necessary . . .
parse arg n m
if m<>'' then say m
Exit word(n 0,1)
Init: /* initialization routine */
. . .
deflib=userid()'.DEF.LIB'
cc=Sysdsn("'"deflib"'")
if cc<>'OK' then call close 8 'Sysdsn' deflib 'failed -' cc
. . .
Return 0
|
|
|
Back to top |
|
|
cvnlynn
New User
Joined: 14 Jun 2017 Posts: 31 Location: US
|
|
|
|
Quote: |
/* SUBROUTINE1 CODE */
SUBROUTINE1: NOP
V1 = 'ERROR'
EXIT_SUBROUTINE1: NOP
RETURN
|
please tell me the meaning for NOP in the LABEL statement, and the EXIT_SUBROUTINE1:NOP.
Thanks. |
|
Back to top |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1787 Location: Bloomington, IL
|
|
Back to top |
|
|
cvnlynn
New User
Joined: 14 Jun 2017 Posts: 31 Location: US
|
|
|
|
thanks |
|
Back to top |
|
|
cvnlynn
New User
Joined: 14 Jun 2017 Posts: 31 Location: US
|
|
|
|
Quote: |
SUBROUTINE2: NOP
V1 = 'OK'
EXIT_SUBROUTINE2: NOP
|
Hi Daveporcelan,
please tell me why we need to code NOP in the LABEL statement. Thanks. |
|
Back to top |
|
|
daveporcelan
Active Member
Joined: 01 Dec 2006 Posts: 792 Location: Pennsylvania
|
|
|
|
This is again personal preference.
The CALL SUBROUTINE2 statement will execute the first statement after the colon :
So without the NOP, it would execute the V1= 'OK'.
I want to always execute a NOP rather than an unknown statement.
Also, if I search for ': NOP' all, I can see the beginning and end of each subroutine.
So, I do not need to code NOP, it is my preference. |
|
Back to top |
|
|
cvnlynn
New User
Joined: 14 Jun 2017 Posts: 31 Location: US
|
|
|
|
thanks. |
|
Back to top |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2147 Location: USA
|
|
|
|
daveporcelan wrote: |
This is again personal preference.
The CALL SUBROUTINE2 statement will execute the first statement after the colon :
So without the NOP, it would execute the V1= 'OK'.
I want to always execute a NOP rather than an unknown statement.
Also, if I search for ': NOP' all, I can see the beginning and end of each subroutine.
So, I do not need to code NOP, it is my preference. |
1) Either with NOP, or without NOP, the next statement V1= 'OK' shall be executed mandatory. Either it is an "unknown" statement, or a "known" one. What is the difference?
2) NOP is used much more often for other more useful purposes in different places of the code. For instance (but not only) - to minimize required changes in a big if-then-else group after one part of it becomes empty, it can be replaced with:
Code: |
if . . . . . . . . . . . . .
{multi-line logical expression}
. . . . . . . . . . . . . . . . . . . . . . then
nop
else do
. . . . . . . . . . . . . . .
{multi-line sequence of statements}
. . . . . . . . . . . . . . .
end |
|
|
Back to top |
|
|
daveporcelan
Active Member
Joined: 01 Dec 2006 Posts: 792 Location: Pennsylvania
|
|
|
|
Quote: |
1) Either with NOP, or without NOP, the next statement V1= 'OK' shall be executed mandatory. Either it is an "unknown" statement, or a "known" one. What is the difference?
|
Like I said:
Quote: |
So, I do not need to code NOP, it is my preference. |
Like I also said:
Quote: |
Here is a sample of my structure. Feel free to use or ignore. Nasty remarks not required. |
|
|
Back to top |
|
|
|