Nesting subroutines

Hi all,
is it possible (and correct)to nest subroutines using a 'then ' statment as in the 'i/o' section of code below)
e.g.
loop:
gosub i2clock 'get time
gosub i2 display 'display time
gosub i/o 'check pins
let b13 = 253 'load a test value
gosub keya 'to check keypad
if b13 <> 253 then keyb 'against
goto loop
end
i2clock:
"clock stuff here"
return
i2disp:
"lcd stuff here"
return
i/o: 'check for change of
if pin1 = 1 and $50 = 0 then delrc ' state
if pin1 = 0 and $50 = 1 then delro ' test
return

delrc
pause 50
if pin 1 = 0 then loop
let b0 = 1
peek $50,b0
gosub i2clock 'get time
gosub i2eewrite 'log time
gosub eeverif 'check if logged ok
return
many thanks for any input.










 
hi,
just realised that Hippy may have aleady answered my question in the "if/then with a gosub " topic below, but i would like to use gosubs to keep the program stepping through and able to return to where the next line of program was.
 

hippy

Ex-Staff (retired)
Within the I/O code, you aren't actually nesting GOSUB's, but entering the single routine, branching to a particular 'handler', then executing a RETURN after each handler. All will cause a continuation at the line after the initial GOSUB which started the ball rolling, and that is perfectly okay. You could have had each handler of the I/O routine finish by jumping to a single RETURN for the entire routine, but what you have is equally acceptable.
 
Hi again,
If the code above is ok, does this mean you can enter a subroutine without having to use a gosub command, (as the instruction was then delrc) and will the picaxe still treat itas a gosub and return ok (with the return instruction at the end of delrc:)
P.S. I accidentally omitted the ':' from the delrc subroutine in the code above.
regards Eric
 

hippy

Ex-Staff (retired)
No, a subroutine can only be entered by a GOSUB.

Once in a subroutine though, flow can be passed around in a sequential (GOTO) manner ...

- GOSUB MySub
:
- MySub:
- SELECT CASE b0
- CASE 1 : pin1 = 0
- CASE 2 : pin2 = 0
- END SELECT
- RETURN

could in PICAXE terms be written ...

- MySub:
- IF b0 = 1 THEN MySub_B0is1
- IF b0 = 2 THEN MySUb_B0is2
- GOTO MySub_Exit
- MySub_B0is1:
- pin1=0
- GOTO MySub_Exit
- MySub_B0is2:
- pin2=0
- GOTO MySub_Exit
- MySub_Exit:
- RETURN

That can be optimised by removing the GOTO MySub_EXit's as you effectively did in your own code to give ...

- MySub:
- IF b0 = 1 THEN MySub_B0is1
- IF b0 = 2 THEN MySUb_B0is2
- RETURN
-
- MySub_B0is1:
- pin1=0
- RETURN
-
- MySub_B0is2:
- pin2=0
- RETURN

There is still only one subroutine, called by the GOSUB, but it has multiple RETURN's rather than just a single RETURN point.
 
Hi,
many thanks Hippy I think the penny's dropped now. I'd tested the code and it seemed to work fine but I didn't Know if this was just luck and it may have caught me out at a later date. From your descriptoin of what is happening I see now why it works, I didn't know if you could have more than one return and the code still work reliably
I suppose this method may help Sollytear with
his program also.
many thanks
 
Top