inc variable then gosub

jinx

Senior Member
hi,

am not sure this is going to make sense but am trying to work out if/how i can. have b1 count up each time it goes too a subroutine adds one then when it gets to 5 it goes to another routine.



Code:
#picaxe 20x2
#no_data
#no_table





 main:do
    sertxd("   am awake    ",13,10)
    
    gosub bckspin
    pause 1000
    loop
    
    
    
    
 bckspin:   
  inc b1 
   pause 1000
 sertxd("The value of b1 is ",#b1,13,10)
 let b1 = b3
 if b3 = 5 then gosub hello
 return
 
 
 
 
 hello:
 
 sertxd("The value of b3 is ",#b3,13,10)
 pause 1000
 let b3 = 0
 return
any suggestions would be helpful.
jinx
 

jinx

Senior Member
haha, ive done it,

Code:
#picaxe 20x2
#no_data
#no_table





 main:do
    sertxd("   am awake    ",13,10)
    
    gosub bckspin
    pause 1000
    loop
    
    
    
    
 bckspin:   
 inc b1 
 pause 1000
 sertxd("The value of b1 is ",#b1,13,10)

 if b1 = 5 then gosub hello
 return
 
 
 
 
 hello:
 sertxd("   well hello   ",13,10)
 sertxd("The value of b1 is ",#b1,13,10)
 pause 1000
 return
am being stupid.
 

PaulRB

Senior Member
Jinx, what are you attempting to do with variable b3? You don't assign a value to it before you assign its value to b1, so you loose your count. How will b3 ever get to 3?

Paul
 

jinx

Senior Member
i was trying to move the value of b1 too b3 but i did'nt have to just making things harder than it had to be.
 

boriz

Senior Member
If I understand correctly, you should do an IF ... GOSUB ELSE GOSUB type test on the variable, and increment the variable inside the subroutine. Does that make sense?
 

g6ejd

Senior Member
Although it is not an issue on the picaxe it is a poor programming technique to jump out of a gosub unless you intend to return to each in turn.

The reason is the address of the first routine is saved on a stack so when the interpreter processes RETURN it knows where to return to, so if you keep jumping out of a go sub eventually your stack would overflow and your code would crash.

As suggested use a conditional IF GOSUB method or simple if b = 1 then gosub x method
 

AllyCat

Senior Member
Hi jinx,

You appear to have omitted a b1 = 0 from the "hello" routine, but apart from that IMHO the code is ok with two legitimate nested subroutines.

Purists may object, but using if b1 = 5 then GOTO hello will also work! In this case hello is NOT a subroutine in itself, but part of a single subroutine bckspin, which happens to have an internal label (hello) and two exit points (returns).

Cheers, Alan.
 
Top