Sub: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(Created page with "SUB ''procedurename''[(''arglist'')] <br /> :::[''statements''] <br /> :::[EXIT SUB] <br /> :::[''statements''] <br /> END SUB '''Description''' SUB declares a procedure, ''...")
 
No edit summary
Line 1: Line 1:
SUB ''procedurename''[(''arglist'')] <br />
Sub ''procedurename''[(''arglist'')] <br />
:::[''statements''] <br />
:::[''statements''] <br />
:::[EXIT SUB] <br />
:::[Exit Sub] <br />
:::[''statements''] <br />
:::[''statements''] <br />
END SUB
End Sub


'''Description'''
== Description ==


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.


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.
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.
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. NS Basic/X treats a single expression enclosed in parenthesis as a single expression, not a one-element argument list.
A Sub procedure called with zero or one arguments may be called with empty parenthesis or the single argument in parenthesis. NS Basic/X treats a single expression enclosed in parenthesis as a single expression, not a one-element argument list.


'''Example'''
== Example ==


<pre>
<pre>
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  
Sort PriceA, PriceB  
PRINT "Lowest Price:", PriceA
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)
Sub Sort(BYREF x, BYREF y)
   DIM Temp
   Dim Temp
   IF x > y THEN
   If x > y Then
     Temp = y
     Temp = y
     y = x
     y = x
     x = Temp
     x = Temp
     EXIT SUB
     Exit Sub
   ELSE
   Else
     Temp = x
     Temp = x
     x = y
     x = y
     y = Temp
     y = Temp
   END IF
   End If
END SUB
End Sub
</pre>
</pre>


'''Output'''
== Output ==


<pre>
<pre>
Line 53: Line 53:
</pre>
</pre>


'''Related Items'''
== Related Items ==


[[call|CALL]], [[function|FUNCTION]]
[[call|CALL]], [[function|FUNCTION]]
[[Category:Language Reference]]

Revision as of 03:42, 17 August 2012

Sub procedurename[(arglist)]

[statements]
[Exit Sub]
[statements]

End Sub

Description

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.

A Sub procedure called with zero or one arguments may be called with empty parenthesis or the single argument in parenthesis. NS Basic/X 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
Sort PriceA, PriceB 
Print "Lowest Price:", PriceA
Sub PrintMenu(day)
  If day = "Wednesday" Then
    Print "Wednesday is Salisbury Steak day"
  End If
End Sub
Sub Sort(BYREF x, BYREF y)
  Dim Temp
  If x > y Then
    Temp = y
    y = x
    x = Temp
    Exit Sub
  Else
    Temp = x
    x = y
    y = Temp
  End If
End Sub

Output

Wednesday is Salisbury Steak day
Lowest Price:44

Related Items

CALL, FUNCTION