Can the competent scripters here please check the logic of the following message? Maybe I have missed a better way of implementing subroutines?
A subroutine is a labelled section in the same script which automatically returns to wherever it was called from. I was thinking about making a feature request for GoSub and Return, for use inside one script file. As usual, before making a feature request it's good to check whether we already have a usable workaround. It seems we do. The best workaround which I can think of allows global and static variables, but not local vars. ________________________________________________ - why Return is needed - In Basic languages there is a pair of commands: GoSub and Return. Every GoSub adds the current line number (+1) to a push-pop stack (last in first out) before jumping to the subroutine. Every subroutine ends with a Return command, which pops the most recently pushed line number and jumps back to it. A separate stack is required for each running script. PowerPro scripts have "Jump label" for a GoTo. But GoTo is not the same as GoSub because the place jumped from is not recorded in a stack for returning to; and there is no Return command in PP. Of course one could add a Jump command to the end of each labelled section which you want to act as a subroutine; but the destination of that returning jump needs to be different each time the subroutine is used. "Quit" will act as a Return, only if the subroutine is called with a Script Run command to a labelled section in a script file, but not if the subroutine is Jumped to as a labelled section in the same script. The best one-file (or one command list) solution I could think of is to tell the script to run itself (which sounds illegal but it works). The following script demonstrates using Script Run with @label, versus using a Jump. ___________________________________________________ ;; this is test1.powerpro debug one ;; uncomment ONE of the next two lines ; jump subroutine ; [EMAIL PROTECTED] debug success quit @subroutine debug two quit ________________________________ If you use the line: jump subroutine debug says: one two If you use the line: [EMAIL PROTECTED] debug says: one two success In the latter case, the Quit at the end of the subroutine will act as Basic's "Return", and returns to the appropriate line in the same script. _______________________________________________ - preserving variables - With the method shown above, PowerPro is actually running two instances of the same script, one nested inside the other. Therefore local variables are not transferred from the main section to the subroutine; also local vars set in the subroutine are not available after returning to the main section. To work around that, use static instead of local variables. They will be transferred when going into a subroutine and when returning with a Quit, because the nested script has the same name as the calling script. ;; this is test1.powerpro local mylocal1= "mylocal1 before sub" static mystatic1= "mystatic1 before sub" [EMAIL PROTECTED] win.debug(local1,";",static1) quit @subroutine mylocal1= "local1 after sub" mystatic1= "static1 after sub" quit ______________________________ Debug will say: "mylocal1 before sub ; mystatic1 after sub" which demonstrates that the change made by the subroutine to mystatic1 survived, but the change it tried making to mylocal1 did not survive returning to the main section. In fact the subroutine has created a global var called "mylocal1" because mylocal1 is not local to the second instance. The main section cannot see that new global because it already has a local var of the same name. Personally, I think the current feature set is good enough. I don't mind using "[EMAIL PROTECTED]" instead of jump label; and I don't mind having to remember to use static or global vars rather than locals in scripts with subroutines. But I wonder: have I missed something? Is there a more efficient way of implementing subroutines better than what I described? ------------------------ Yahoo! Groups Sponsor --------------------~--> Yahoo! Domains - Claim yours for only $14.70 http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/JV_rlB/TM --------------------------------------------------------------------~-> Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/power-pro/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
