Sub (explanatory): Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
''' | '''SUBs''', short for subroutines, are like [[statement|statements]] but are user-defined. You define a '''SUB''' by using the [[SUB]] statement. When a subroutine is called, then it will execute the code within the [[SUB]] until it reaches [[END SUB]] or [[EXIT SUB]]. | ||
The | The [[CALL]] statement can be used to execute the code of a subroutine, but is not required. When [[CALL]] is used, then the [[argument|arguments]] must be enclosed with paranteses. | ||
'''SUBs''' do not return a value. However, altering the argument variables within the subroutine will cause the same changes to the respective variables given in the subroutine call. Variables that are not arguments of the subroutine will not be changed if used within the subroutine unless they are [[SHARED]]. | |||
The | The '''SUBs''' must be placed after all other code in a program | ||
{{PageExamples}} | {{PageExamples}} | ||
;Example 1:Demonstration of some subs. | |||
{{CodeStart}} | {{CodeStart}} | ||
printsometext | printsometext | ||
Line 30: | Line 30: | ||
;Example 2:Variables that are used as arguments changes outside the sub when they are changed within the sub. | |||
{{CodeStart}} | {{CodeStart}} | ||
demo hal, bal 'the variables hal and bal are used as arguments when calling the sub. | demo hal, bal 'the variables hal and bal are used as arguments when calling the sub. | ||
Line 50: | Line 50: | ||
{{Cl|END SUB}} | {{Cl|END SUB}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Line 58: | Line 57: | ||
* [[Function (explanatory)]], [[Statement]] | * [[Function (explanatory)]], [[Statement]] | ||
* [[Argument]] | * [[Argument]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Revision as of 21:25, 22 December 2022
SUBs, short for subroutines, are like statements but are user-defined. You define a SUB by using the SUB statement. When a subroutine is called, then it will execute the code within the SUB until it reaches END SUB or EXIT SUB.
The CALL statement can be used to execute the code of a subroutine, but is not required. When CALL is used, then the arguments must be enclosed with paranteses.
SUBs do not return a value. However, altering the argument variables within the subroutine will cause the same changes to the respective variables given in the subroutine call. Variables that are not arguments of the subroutine will not be changed if used within the subroutine unless they are SHARED.
The SUBs must be placed after all other code in a program
Examples
- Example 1
- Demonstration of some subs.
printsometext CALL printmoretext SUB printsometext PRINT "This is within the printsometext sub." EXIT SUB PRINT "This is never shown as it encounters EXIT SUB first." END SUB SUB printmoretext PRINT "This is within the printmoretext sub." END SUB |
- Example 2
- Variables that are used as arguments changes outside the sub when they are changed within the sub.
demo hal, bal 'the variables hal and bal are used as arguments when calling the sub. PRINT hal, bal 'the variables have now been changed by the sub. hal = 0 bal = 0 demo 1, 2 PRINT hal, bal 'the variables have not been changed by the sub as the variables wasn't used as arguments. SUB demo (hal, bal) hal = 3 bal = 4 END SUB |
See also