Developer Documentation Library > Scripting > mrScriptBasic overview > mrScriptBasic language reference > Procedures > Sub
 
Sub
Declares the name, arguments, and code that form the body of a Sub procedure.
Syntax
Sub name([<arglist>])
[statements]
[Exit Sub]
[statements]
End Sub
<arglist> ::= varname[[]]
Argument
name
Name of the Sub. This must conform to the mrScriptBasic rules for regular identifiers described in Naming conventions.
statements
Any group of statements to be executed within the body of the Sub procedure.
varname
Name of the variable representing the argument. This must conform to the mrScriptBasic rules for regular identifiers described in Naming conventions.
Remarks
A Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. Unlike a Function procedure, a Sub procedure does not return a value and cannot be used in an expression.
Call a Sub procedure by using the procedure name, followed by the argument list, which must be enclosed in parentheses ( ). Simple arguments are passed by value, which means that updates made in the procedure are not reflected in the calling script. Array and object arguments are passed by reference, which means that any updates made to those variables in the procedure are seen in the calling script.
You can not define a Sub procedure inside any other procedure (for example, Function or Sub).
The Exit Sub statement causes an immediate exit from a Sub procedure. Program execution continues with the statement that follows the statement that called the Sub procedure. Any number of Exit Sub statements can appear anywhere in a Sub procedure.
Notes
Sub procedures can be recursive, calling themselves to perform a given task. However, recursion can lead to stack overflow if used without undue care.
Variables defined in the script block that calls the subroutine are not visible inside the subroutine unless you pass them as arguments.
Example
The following data cleaning example shows a Sub procedure and how to call it.
CheckEducation(education, school)
.
.
.
Sub CheckEducation(education, school)
If education = {No} Then
If school.AnswerCount() > 0 Then
education = {Yes}
End If
ElseIf education = {Yes} Then
If school.AnswerCount() < 1 Then
education = {No}
End If
Else
If school.AnswerCount() > 0 Then
education = {Yes}
End If
End If
End Sub
See also
Procedures