Sub: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Sub ''procedurename''[(''arglist'')] <br />
Sub ''procedurename''(''arglist'') <br />
:::[''statements''] <br />
:::[''statements''] <br />
:::[Exit Sub] <br />
:::[Exit Sub] <br />
Line 6: Line 6:


== Description ==
== Description ==
BASIC only. Use function for JavaScript.


Sub declares a procedure, ''procdurename'', that executes statements, with ''arglist'' as parameters, with no return value. The required parameter, ''procedurename'', is used to call the procedure, and must follow standard variable naming conventions. The optional parameter list, ''arglist'', is a comma separated list of variables that are passed to the procedure when it is called. The optional component, ''statements'', will be executed as the body of the procedure. Any number of optional Exit Sub statements can be used to exit the procedure.
Sub declares a procedure, ''procdurename'', that executes statements, with ''arglist'' as parameters, with no return value. The required parameter, ''procedurename'', is used to call the procedure, and must follow standard variable naming conventions. The optional parameter list, ''arglist'', is a comma separated list of variables that are passed to the procedure when it is called. The optional component, ''statements'', will be executed as the body of the procedure. Any number of optional Exit Sub statements can be used to exit the procedure.
Line 13: Line 15:
A Function procedure will be called as a Sub procedure if the return value is not stored in a variable or used in an expression.
A Function procedure will be called as a Sub procedure if the return value is not stored in a variable or used in an expression.


A Sub procedure called with zero or one arguments may be called with empty parenthesis or the single argument in parenthesis. App Studio treats a single expression enclosed in parenthesis as a single expression, not a one-element argument list.
To call a Sub, use the sub's name followed with parenthesis containing the arguments. If there are no arguments, it is still recommended that parenthesis be supplied, so it is clear that a Sub is being called.
 
A Sub procedure called with zero or one arguments may be called with empty parenthesis or the single argument in parenthesis. AppStudio treats a single expression enclosed in parenthesis as a single expression, not a one-element argument list.


== Example ==
== Example ==
Line 20: Line 24:
Rem Sub Example
Rem Sub Example
'Sub declares a procedure
'Sub declares a procedure
Dim PriceA, PriceB
Dim PriceA, PriceB
PrintMenu "Wednesday"
PrintMenu "Wednesday"
PriceA = 53
PriceA = 53
PriceB = 44
PriceB = 44
Sort PriceA, PriceB
Print "Lowest Price:", mySort(PriceA, PriceB)
Print "Lowest Price:", PriceA
 
Sub PrintMenu(day)
Sub PrintMenu(Day)
   If day = "Wednesday" Then
   If Day = "Wednesday" Then
     Print "Wednesday is Salisbury Steak day"
     Print "Wednesday is Salisbury Steak day"
   End If
   End If
End Sub
End Sub
Sub Sort(BYREF x, BYREF y)
 
   Dim Temp
Function mySort(x, y)
   If x > y Then
   Print x,y
     Temp = y
   If x < y Then
     y = x
     Print "less"
    x = Temp
     mySort = x
    Exit Sub
   Else
   Else
     Temp = x
     Print "More", y
     x = y
     mySort = y
    y = Temp
   End If
   End If
End Sub
End Function
</pre>
</pre>


Line 58: Line 61:


[[Category:Language Reference]]
[[Category:Language Reference]]
[[Category:Statements - Flow of control]]

Latest revision as of 19:22, 16 April 2026

Sub procedurename(arglist)

[statements]
[Exit Sub]
[statements]

End Sub

Description

BASIC only. Use function for JavaScript.

Sub declares a procedure, procdurename, that executes statements, with arglist as parameters, with no return value. The required parameter, procedurename, is used to call the procedure, and must follow standard variable naming conventions. The optional parameter list, arglist, is a comma separated list of variables that are passed to the procedure when it is called. The optional component, statements, will be executed as the body of the procedure. Any number of optional Exit Sub statements can be used to exit the procedure.

Each member of arglist is an argument passed to the procedure. Simple variables, such as strings and numbers, are passed by value. Complex variables, such as arrays and objects, are passed by reference. Each member of arglist can be used within the procedure to reference a value passed in. It follows standard variable naming procedure.

A Function procedure will be called as a Sub procedure if the return value is not stored in a variable or used in an expression.

To call a Sub, use the sub's name followed with parenthesis containing the arguments. If there are no arguments, it is still recommended that parenthesis be supplied, so it is clear that a Sub is being called.

A Sub procedure called with zero or one arguments may be called with empty parenthesis or the single argument in parenthesis. AppStudio treats a single expression enclosed in parenthesis as a single expression, not a one-element argument list.

Example

Rem Sub Example
'Sub declares a procedure

Dim PriceA, PriceB
PrintMenu "Wednesday"
PriceA = 53
PriceB = 44
Print "Lowest Price:", mySort(PriceA, PriceB)

Sub PrintMenu(Day)
  If Day = "Wednesday" Then
    Print "Wednesday is Salisbury Steak day"
  End If
End Sub

Function mySort(x, y)
  Print x,y
  If x < y Then
    Print "less"
    mySort = x
  Else
    Print "More", y
    mySort = y
  End If
End Function

Output

Wednesday is Salisbury Steak day
Lowest Price:44

Call, Function