Page 1 of 1

Private Subroutine for (NB) Function

PostPosted: Wed Jul 11, 2012 1:45 pm
by dpayer
In an effort to keep a function as a separate item that is transportable to others, I ran into a situation where I wanted to use a subroutine several times in a process.

I found that I could use the GoToLine command to call the subroutine and then instead of using :return as is done in a NB subroutine, I use a GoToLine command to return to the place where I called the Sub. The "Return" command does not work

Of course, the subroutine commands are still in the function and would be enacted again if you don't put a condition in front of them. I do so with the setting of a variable within the subroutine and then again at the end of the sub.

Here is a sample of what I mean. Save the function below in your functions folder and call it using a button's action. It will do a 10 cycle loop and pop up an alertbox after each loop. After 7 times, it will break out and do a different loop 3 times and then return to the original loop.

I'm still thinking about how to use the private subroutine when you enter it from different places in your script. You will need conditions to determine the return point.

Code: Select all
{NeoBook Function}
Version=5.70
Language=NeoBook
{End}
Loop "1" "10" "[WILL]"
Delay "1000"
Alertbox "Test" "This is loop position [will]"
         If "[will]" "=" "7"
            GotoLine "mybreakout"
         Endif
         :returnfrom7
Endloop
Alertbox "end" "end of test"


.subroutine below - only accessible if SubRoutineVar is on

If "[SubRoutineVar]" "=" "ON"
:mybreakout
SetVar "[SubRoutineVar]" "ON"
loop "1" "3" "[WILL2]"
Alertbox "Test 2" "Breakout from first loop.|[will2] of 3 alerts"
endloop
SetVar "[SubRoutineVar]" "OFF"
GoToLine "returnfrom7"
Endif

PostPosted: Wed Jul 11, 2012 4:11 pm
by Gaev
DavidP:

I do not use GoToLine in any of my Applications (in any language) ... it is kind of a circa 1990's command that is discouraged (or not available) in modern languages ... was the source of what used to be called "spaghetti code" ... which is what the code printout looked like when programmers connected the code flow (often with multi colored lines) when attempting to review/debug such code written by others ... see http://en.wikipedia.org/wiki/Spaghetti_code

As an alternative, have you considered doing a Call (to another Function) from within your first/main Function ? ... something like ...
Code: Select all
{NeoBook Function}
Version=5.70
Language=NeoBook
{End}
Loop "1" "10" "[WILL]"
Delay "1000"
Alertbox "Test" "This is loop position [will]"
         If "[will]" "=" "7"
            Call "myFunctionSubRoutine"
         Endif
Endloop
Alertbox "end" "end of test"

... and (for myFunctionSubRoutine) ...
Code: Select all
{NeoBook Function}
Version=5.70
Language=NeoBook
{End}
loop "1" "3" "[will2]"
Alertbox "Test 2" "Breakout from first loop.|[will2] of 3 alerts"
endloop


If needed, the Call command can also pass parameters to the called Function.

PostPosted: Wed Jul 11, 2012 4:17 pm
by dpayer
Gaev wrote:DavidP:

I do not use GoToLine in any of my Applications (in any language) ... it is kind of a circa 1990's command that is discouraged (or not available) in modern languages .


Yes, it is a throwback, I agree.

It is cleaner and clearer to use multiple functions. What you gain in clarity you lose in portability. I had hoped to create something akin to a plugin in one file.

Thanks for your comments.

David

PostPosted: Wed Jul 11, 2012 8:13 pm
by Gaev
DavidP:

Haven't checked this out myself but how about ...
Code: Select all
{NeoBook Function}
Version=5.70
Language=NeoBook
{End}
Loop "1" "10" "[WILL]"
   Delay "1000"
   Alertbox "Test" "This is loop position [will]"
   If "[will]" "=" "7"
      GotoLine "mybreakout"
   Endif
   :returnfrom7
Endloop
Alertbox "end" "end of test"
GotoLine "theVeryEnd"

:mybreakout
loop "1" "3" "[WILL2]"
   Alertbox "Test 2" "Breakout from first loop.|[will2] of 3 alerts"
endloop
GoToLine "returnfrom7"

:theVeryEnd
SetVar "[dummy]" "True"
... i.e. after the Alertbox "end" "end of test" command, jump to the label at the very end of the Function ... hopefully NeoBook will return control to the line after the original Call to your Function.

Note how uncomplicated it makes the "pseudo-SubRoutune" called :mybreakout

Good Luck.

PostPosted: Wed Jul 11, 2012 8:25 pm
by Gaev
DavidP:

Might be even more structured if you scripted it like ...
Code: Select all
Loop "1" "10" "[WILL]"
   Delay "1000"
   Alertbox "Test" "This is loop position [will]"
   If "[will]" "=" "7"
      GotoLine "mybreakout"
      :returnfrom7
   Endif
Endloop
... i.e. return from breakout to the line immediately after the GoToLine command