GOSUB procedure indexed ?

zorgloub

Member
Hello to the Forum,
What would be the syntax of a command to call “indexed procedures”?
Let me explain :
Consider an index variable ranging from 1 to 3, for example.
And three routines that would be called depending on the value of this index.

An “GOSUB Routine &1” style command to call routine1.

Is it possible ?
Thanks

------------------------------------------------------
Program structure:
i = 2 ‘identifier of the routine to call, which can vary from 1 to 3


GOSUB Routine &2 ‘ ???

Routine1

Return

Routine2

Return

Routine3

Return
 
Last edited:

Flenser

Senior Member
Fron the manual there is only one syntax for GOSUB:
Syntax:
GOSUB address
- Address is a label which specifies where to gosub to.

and there is an added complication that there is a special dedicated GOSUB form of the IF syntax that does not support an ELSIF clause:
Syntax:
IF variable ?? value {AND/OR variable ?? value ...} THEN GOSUB address
- Address is a label which specifies where to gosub if condition is true.

So instead of your example:
Code:
i = 2  ‘identifier of the routine to call, which can vary from 1 to 3
GOSUB Routine &2  ‘ ???
Routine1
Return
Routine2
Return
Routine3
Return
The code that I came up with is this:
Code:
symbol i = b1
i = 2  ;identifier of the routine to call, which can vary from 1 to 3
IF i=1 then GOSUB Routine1
IF i=2 then GOSUB Routine2
IF i=3 then GOSUB Routine3
Routine1:
Return
Routine2:
Return
Routine3:
Return
Perhaps someone else can suggest a better approach.
 

oracacle

Senior Member
You could also use case select depending on how you want to setup yopur code. It hard to say without knowing more about the code and your personal style of coding. As an idea you don't say if you want to be able to call from multiple locations within the code, if this is the case then it will be easier using an "if then gosub" as suggested by Flenser but if it only executes inline then the select command may work better.
I cant remember if you can use and/or statement in the case part of the command though which may effect what commant you use.

select - BASIC Commands - PICAXE

Code:
select case b1      'this would hold the vlaue i
  case 1
     'do your code for if i is 1
  case 2
     'do your code for if i is 2
  case 3
     'do you code for if i is 3
endselect
 

Technoman

Senior Member
Note that your index value should be bounded to the number of subroutines or in case of index overflow a goto should redirect the program flow (error handling routine, ...) after the on bx gosub command.
 

zorgloub

Member
Thank you for your contributions to my request.
For an optimization of my program, I preferred to avoid conditional sorting and the select case which I had also thought of.
In fact, the ON...GOSUB command therefore seems to fully satisfy my needs.
Thanks again everyone !
 

AllyCat

Senior Member
Hi,

Note that the index starts from zero, not 1 and actually compiles to a list of, for example : IF b0 = 0 THEN GOSUB ... : IF b0 = 1 THEN GOSUB : ... etc..

If the index is out of (the listed) range, the program simply "falls through" into the next instruction. So there is no need for a specific range test and if processing speed is critical you can even omit the final index and simply fall through instead.

Cheers, Alan..
 

hippy

Ex-Staff (retired)
ON var GOSUB sub0, sub1, sub2 ...

Would be the recommended option. That can sometimes be a little large in code size so, if that is problematic, a slightly different structure can be used ...

Recommended
Code:
MainlLoop:
  For b0 = 0 To 2
    On b0 Gosub sub0, sub1, sub2
  Next
  End

sub0: ... return
sub1: ... return
sub2: ... return
Alternative
Code:
MainlLoop:
  For b0 = 0 To 2
    Gosub subX
  Next
  End

subX:
  On b0 Goto sub0, sub1, sub2                 ; Note GOTO, not GOSUB

sub0: ... return
sub1: ... return
sub2: ... return
 
Top