Sub (explanatory): Difference between revisions
(Created page with "A '''SUB''' procedure is a procedure within a program that can calculate and return multiple parameter values just like a full program. {{PageSyntax}} :: '''SUB Procedure_name''' [(''parameter''[, ''list''...])] :: ... :: ... 'procedure variable definitions and statements :: ... :: '''END SUB''' {{Parameters}} * Parameters passed after the procedure call must match the variable types in the SUB parameters in order. * If there are no ''parameter''s passed or they are...") |
No edit summary |
||
Line 1: | Line 1: | ||
'''Subs''', short for subroutines, are like [[statement]]s but user-defined. You define a sub by using the [[statement]] {{KW|SUB}} and when called it will execute the code within the SUB until it reaches {{KW|END SUB}} or {{KW|EXIT SUB}}. | |||
{{ | The {{KW|CALL}} statement can be used to execute the code of a sub but is not really needed, when CALL is used the [[argument]]s need to be enclosed with paranteses. | ||
Subs do not return a value, however; altering the variables that are used as arguments within the sub will cause the same changes to the variables outside the sub. This also applies to user-defined [[function (explanatory)|function]]s. Variables that are not arguments of the sub will not be changed if used within the sub unless they are {{KW|SHARED}}. | |||
The subs has to be located after all other code, this also applies to user-defined [[function (explanatory)|function]]s. | |||
''Example 1:'' | {{PageExamples}} | ||
{{CodeStart}} | ''Example 1:'' Demonstration of some subs. | ||
{{CodeStart}} | |||
printsometext | |||
{{Cl|CALL}} printmoretext | |||
{{Cl| | |||
{{Cl|SUB}} | {{Cl|SUB}} printsometext | ||
{{Cl|PRINT}} "This is within the printsometext sub." | |||
{{Cl|EXIT SUB}} | |||
{{Cl|PRINT}} "This is never shown as it encounters EXIT SUB first." | |||
{{Cl| | {{Cl|END SUB}} | ||
{{Cl|END SUB}} | |||
{{Cl|SUB}} printmoretext | |||
{{Cl|PRINT}} "This is within the printmoretext sub." | |||
{{Cl|END SUB}} | |||
{{CodeEnd}} | {{CodeEnd}} | ||
''Example 2:'' Variables that are used as arguments changes outside the sub when they are changed within the sub. | |||
{{CodeStart}} | |||
demo hal, bal 'the variables hal and bal are used as arguments when calling the sub. | |||
{{Cl|PRINT}} hal, bal 'the variables have now been changed by the sub. | |||
{{Cl|PRINT}} | |||
hal = 0 | |||
bal = 0 | |||
demo 1, 2 | |||
{{Cl|PRINT}} hal, bal 'the variables have not been changed by the sub as the variables wasn't used as arguments. | |||
{{Cl|PRINT}} | |||
{{Cl| | {{Cl|SUB}} demo (hal, bal) | ||
hal = 3 | |||
bal = 4 | |||
{{Cl|END SUB}} | {{Cl|END SUB}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{PageSeeAlso}} | |||
* [[SUB]], [[FUNCTION]] | |||
* [[Function (explanatory)]], [[Statement]] | |||
* [[Argument]] | |||
{{PageNavigation}} | {{PageNavigation}} |
Revision as of 00:55, 22 December 2022
Subs, short for subroutines, are like statements but user-defined. You define a sub by using the statement Template:KW and when called it will execute the code within the SUB until it reaches Template:KW or Template:KW.
The Template:KW statement can be used to execute the code of a sub but is not really needed, when CALL is used the arguments need to be enclosed with paranteses.
Subs do not return a value, however; altering the variables that are used as arguments within the sub will cause the same changes to the variables outside the sub. This also applies to user-defined functions. Variables that are not arguments of the sub will not be changed if used within the sub unless they are Template:KW.
The subs has to be located after all other code, this also applies to user-defined functions.
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 |