Ending Sub Routine

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
obfusc88
Pro Scripter
Posts: 89
Joined: Wed Mar 14, 2007 6:22 pm

Ending Sub Routine

Post by obfusc88 » Mon Jan 26, 2009 6:36 pm

Can I use End>Subname more than once?

I want to do this, but am afraid multiple End>RoutineName lines will be a problem:

Code: Select all

SRT>RoutineName
stuff..
IF>something fails
End>RoutineName
ELSE
more stuff

If>something else fails
End>RoutineName
ELSE
more stuff

If>another thing fails
End>RoutineName
ELSE
more stuff

End>RoutineName
----------------------

Right now I am adding a label before the End line if I want to exit early, but wanted to use End instead.

Example:

Code: Select all

SRT>RoutineName
stuff..
IF>something fails
GoTo>LastLine
ELSE
more stuff

If>something else fails
GoTo>LastLine
ELSE
more stuff

If>another thing fails
GoTo>LastLine
ELSE
more stuff

Label>LastLine
End>RoutineName

What I am doing works, but I need to keep making unique label names, and would like to have a "standard" phrase to use to stop the SRT
Last edited by obfusc88 on Mon Jan 26, 2009 10:13 pm, edited 1 time in total.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Jan 26, 2009 7:26 pm

What you're doing with the LastLine label would be the prefered way to handle exiting a subroutine. As for a "standard" labeling method try something like adding the subroutine name to the LastLine label.

Code: Select all

SRT>RoutineName
stuff..
IF>something fails
GoTo>RoutineName_LastLine
ELSE
more stuff

If>something else fails
GoTo>RoutineName_LastLine
ELSE
more stuff

If>another thing fails
GoTo>RoutineName_LastLine
ELSE
more stuff

Label>RoutineName_LastLine
End>RoutineName

obfusc88
Pro Scripter
Posts: 89
Joined: Wed Mar 14, 2007 6:22 pm

Post by obfusc88 » Mon Jan 26, 2009 10:17 pm

Thanks to JRL for such a quick reply. I did start to use GoTo>LastLine_RoutineName already. Before I was using SR1, SR2, etc. for SubRoutine, but if I changed sequence or made too many, then I would get confused.

So, this is the preferred manner that I stumbled into. Can you explain why it is better? Was my fear of compiling confusion correct?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jan 27, 2009 7:07 pm

Can you explain why it is better?
It is better because it is syntactically correct. The first method you mention, though it kinda-sorta looks like it works, does not leave the subroutine intact as an element. This COULD cause issues.

Normally, a subroutine is a block that will be ignored by processing unless it is called by the GoSub> function. Using the method you have invented, the subroutine block is unrecognized and normal processing will run through the subroutine's lines as if it was an average non-subroutine part of the script.

Here's an example script that contains a good subroutine and a bad subroutine. The good uses labels to jump the the last line of the subroutine when conditions are met, while the bad uses subroutine END blocks to pop out of the subroutine. Step through this one line at a time by pressing F8 in the editor and you can see exactly what is happening. The initial subroutine processing will take place correctly with the bad subroutine, but the script processing will run the subroutine again when the GoSub> function has not been called.

To be fair, this could probably be circumvented by placing the subroutine at the end of the script and making sure there is an Exit> function ahead of the subroutine.
Was my fear of compiling confusion correct?
I can be confused. Compilers can only be misinformed.

Hope this makes sense,
Dick

Code: Select all

Let>kk=0
Let>Answer1=0
Let>answer2=0

Label>StartRight
  Add>kk,1
  If>kk>2,ContinueRight
  GoSub>RightWay
Goto>StartRight

Label>ContinueRight
Let>kk=1

SRT>RightWay
  If>kk=1
	Add>Answer1,1
  Else
    Add>Answer2,1
	Goto>End_RightWay
  EndIf
  If>kk=2
	Add>Answer1,1
  Else
    Add>Answer2,1
	Goto>End_RightWay
  EndIf
  Label>End_RightWay
END>RightWay

MDL>Right answers are:%CRLF%Answer1=%Answer1%%CRLF%Answer2=%Answer2%


Let>kk=0
Let>Answer1=0
Let>answer2=0

Label>StartWrong
  Add>kk,1
  If>kk>2,ContinueWrong
  GoSub>WrongWay
Goto>StartWrong

Label>ContinueWrong
Let>kk=1

SRT>WrongWay
  If>kk=1
    Add>Answer1,1
  Else
    Add>Answer2,1
END>WrongWay
  EndIf
  If>kk=2
	Add>Answer1,1
  Else
    Add>Answer2,1
END>WrongWay
  EndIf
END>WrongWay

MDL>Wrong answers are:%CRLF%Answer1=%Answer1%%CRLF%Answer2=%Answer2%

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts