GOSUB: Difference between revisions
Jump to navigation
Jump to search
Code by Cyperium
Code by Cyperium
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
{{PageDescription}} | {{PageDescription}} | ||
* Use [[RETURN]] in a sub procedure to return to the next line of code after the original [[GOSUB]] call. [[END]] or [[SYSTEM]] can also be used to end program. | * Use [[RETURN]] in a sub procedure to return to the next line of code after the original [[GOSUB]] call. [[END]] or [[SYSTEM]] can also be used to end program. | ||
* GOSUB and GOTO can be used '''within''' [[SUB]] or [[FUNCTION]] procedures, but cannot refer to a label located outside the procedure. | * GOSUB and GOTO can be used '''within''' [[SUB]] or [[FUNCTION]] procedures, but cannot refer to a label located outside the procedure. | ||
=== QBasic/QuickBASIC === | |||
===QBasic/QuickBASIC=== | |||
* Too many GOSUBs without a [[RETURN]] can eventually cause "Out of Stack Errors" in QBasic as each GOSUB uses memory to store the location to return to. Each [[RETURN]] frees the memory of the GOSUB it returns to. | * Too many GOSUBs without a [[RETURN]] can eventually cause "Out of Stack Errors" in QBasic as each GOSUB uses memory to store the location to return to. Each [[RETURN]] frees the memory of the GOSUB it returns to. | ||
Line 29: | Line 27: | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{ | {{Small|Code by Cyperium}} | ||
{{OutputStart}} | {{OutputStart}} | ||
1. It goes to the subroutine. | 1. It goes to the subroutine. | ||
Line 57: | Line 55: | ||
{{Cl|RETURN}} | {{Cl|RETURN}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{ | {{Small|Code by Cyperium}} | ||
{{OutputStart}} | {{OutputStart}} | ||
It went here. | It went here. | ||
Line 71: | Line 69: | ||
* [[ON...GOTO]], [[GOTO]] | * [[ON...GOTO]], [[GOTO]] | ||
* [[ON ERROR]], [[RESUME]] | * [[ON ERROR]], [[RESUME]] | ||
* [[ON TIMER (n)]] | * [[ON TIMER(n)]] | ||
* [[Line number]] | * [[Line number]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 16:55, 24 February 2023
GOSUB sends the program flow to a sub procedure identified by a line number or label.
Syntax
- GOSUB {lineNumber|label}
Description
- Use RETURN in a sub procedure to return to the next line of code after the original GOSUB call. END or SYSTEM can also be used to end program.
- GOSUB and GOTO can be used within SUB or FUNCTION procedures, but cannot refer to a label located outside the procedure.
QBasic/QuickBASIC
- Too many GOSUBs without a RETURN can eventually cause "Out of Stack Errors" in QBasic as each GOSUB uses memory to store the location to return to. Each RETURN frees the memory of the GOSUB it returns to.
Examples
Example: Simple usage of GOSUB
PRINT "1. It goes to the subroutine." GOSUB subroutine PRINT "3. And it returns." END subroutine: PRINT "2. It is at the subroutine." RETURN |
1. It goes to the subroutine. 2. It is at the subroutine. 3. And it returns. |
Example: What happens if two GOSUB executes then two RETURN's?
start: a = a + 1 IF a = 1 THEN GOSUB here: PRINT "It returned to IF a = 1": END IF a = 2 THEN GOSUB there: PRINT "It returned to IF a = 2": RETURN here: PRINT "It went here." GOTO start there: PRINT "It went there." RETURN |
It went here. It went there. It returned to IF a = 2 It returned to IF a = 1 |
Explanation: When a = 1 it uses GOSUB to go to "here:", then it uses GOTO to go back to "start:". a is increased by one so when a = 2 it uses GOSUB to go to "there:", and uses RETURN to go the last GOSUB (which is on the IF a = 2 line), it then encounters another RETURN which makes it return to the first GOSUB call we used on the IF a = 1 line.
See also