7.1.9 Sub
- Purpose
-
Declares the name and arguments, and indicates the start of the Sub procedure. It also defines a sequence of statements enclosed by the Sub statement and End Sub statement as a Sub procedure.
- Syntax
Sub Name [(ArgList)] [Statements] [Exit Sub] [Statements] End [Sub]
- Arguments
-
- Name
-
Write the name of the Sub procedure you are defining. Comply with the variable naming conventions.
- ArgList
-
Specify a list of variables representing the arguments to pass to the Sub procedure when it is called. Delimit multiple variables with commas. Enclose any arguments with parentheses.
- Statements
-
Write a group of statements to be executed within the Sub procedure.
- Description
-
Sub procedures are public and can be referenced by all other procedures in the script.
The executable code must be written entirely within the procedure. You cannot define a Sub procedure inside another Sub procedure or inside a Function procedure.
The Exit Sub statement causes the program to immediately exit from a Sub procedure. Execution continues from the statement following the statement that called the Sub procedure. You can write any number of Exit Sub statements as required anywhere in a Sub procedure.
Because the Sub procedure does not return a value, Sub procedure names cannot be used in expressions, statements, or commands.
To call a Sub procedure, write the procedure name followed by an argument list. For details on calling Sub procedures, see 7.1.10 Call.
Two types of variables can be used in Sub procedures: variables that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure, using the Dim command or equivalent, behave as local variables that are valid only within that procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure. The value of local variables in a Sub procedure are discarded when the procedure finishes execution.
A variable that is not explicitly declared in the procedure can be used in a Sub procedure, but it will be handled as a global variable if another variable or entity is defined at the script level with the same name as the undeclared variable.
- Example
' Call the Sub procedure OperateMessage ' that displays a message dialog box. Dim Task Task = "file update" Call OperateMessage("Start") : Call OperateMessage("Stop") Sub OperateMessage(status) Dim stamp stamp = Date + " " + Time MessageBox( "" & status & "" & Task & " at " & stamp) End Sub
- JP1/Script version
-
Supported from JP1/Script 01-00.