Source language: Translate to:

Private Subroutine for (NB) Function

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

Private Subroutine for (NB) Function

Postby dpayer » Wed Jul 11, 2012 1:45 pm

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
User avatar
dpayer
 
Posts: 1383
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Gaev » Wed Jul 11, 2012 4:11 pm

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.
User avatar
Gaev
 
Posts: 3733
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby dpayer » Wed Jul 11, 2012 4:17 pm

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
User avatar
dpayer
 
Posts: 1383
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Gaev » Wed Jul 11, 2012 8:13 pm

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.
User avatar
Gaev
 
Posts: 3733
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby Gaev » Wed Jul 11, 2012 8:25 pm

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
User avatar
Gaev
 
Posts: 3733
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada


Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 0 guests