quick question on gosubs

i have been reading the manual on go subs, but getting erroneous results.

if i execute a gosub from another subroutine will it return to main: or will it return to where it was executed from.

i.e
Code:
main:

gosub displaytime
'stuff
goto main


displaytime:

gosub converttime
'more stuff
return

converttime
'converttime stuff
return


for example when converttime finishes will it return to displaytime?
 

hippy

Ex-Staff (retired)
A RETURN will cause execution to continue at the command after the GOSUB which caused the subroutine to be entered. In this case the 'converttime' RETURN will continue execution back in 'displaytime'.

The only issues you may have if you nest GOSUB's too deeply when it loses track of where it was and execution can end up in the wrong place.
 

boriz

Senior Member
Each GOSUB command adds a return address to the top of a pile before skipping off to the subroutine. Each RETURN command just pulls the top address off the pile and resumes program execution at that address. No other checking is undertaken. So long as you keep the GOSUBs and the RETURNS balanced, the pile will be under control and work properly. GOSUBS without RETURNS leave addresses on the pile corrupting it and causing erroneous results. RETURNS without GOSUBS will also corrupt the pile. Always follow the procedure through visually, statement by statement as if the program were running and make sure your RETURNs and GOSUBs are matched.

Your example will work just fine.
 

hippy

Ex-Staff (retired)
Another way to visualise the return stack is as a disc which has a quarter of its surface exposed.

When a GOSUB is encountered, the 'line number' where the return will be to is written to the disc in the exposed area ( any previously written is erased first ) and then the disc is rotated clockwise by a quarter turn. Execution then continues from the label of the GOSUB called.

When RETURN is encountered, the disc is rotated by a quarter turn anti-clockwise, then the 'line number' is read off the disc and execution continues from the line indicated.

Too many, too deeply nested, GOSUB's leads to overwriting earlier data on the disc, while a RETURN without a GOSUB leads to returning with no 'line number' written on the disc. Once too many nested GOSUB's have been executed and data has been over-written on the disc no matter how many RETURN are executed, no matter how many times the disc is turned, the earliest line number(s) which were there will never appear.

For X2 and PICAXE's with an 8-deep Return Stack, this disc would have an eighth exposed and can hold eight return 'line numbers' rather than just four.
 

Dippy

Moderator
Wouldn't a lovely little sketch in the Manual make it clearer?

Or else we'll soon get analogies about delegating jobs to Auntie Phoebe, who delegates it to the Butler etc. :)
 

fernando_g

Senior Member
Wouldn't a lovely little sketch in the Manual make it clearer?

Or else we'll soon get analogies about delegating jobs to Auntie Phoebe, who delegates it to the Butler etc. :)
Don't get us started on analogies, we can be quite creative! :) :) :)
 
Top