VB tutorial> VBScript Process
Process Classification
In VBScript, the process is divided into two types: sub process and function process.
Sub Process
The sub process is a set of VBScript statements between the sub and the end sub statements. The operation is executed but no return value is returned. Sub processes can use parameters (constants, variables, or expressions passed by the call process ). If the sub process has no parameters, the sub statement must contain empty parentheses ().
The following sub process uses two inherent (or built-in) VBScript functions, msgbox and inputbox, to prompt the user to enter information. The calculation result is displayed based on the information. The calculation is completed by the function process created using VBScript. This process will be demonstrated after the following discussions.
Sub converttemp ()
Temp = inputbox ("Enter the Fahrenheit temperature. ", 1)
Msgbox "temperature is" & Celsius (temp) & "Celsius. "
End sub
Function Process
The function process is a set of VBScript statements between the function and end function statements. The function process is similar to the sub process, but the function process can return values. A function process can use parameters (constants, variables, or expressions passed by the call process ). If the function process has no parameters, the function statement must contain empty parentheses (). The function process returns a value through the function name, which is assigned to the function name in the process statement. The data type returned by function is always variant.
In the following example, the Celsius function converts degrees Fahrenheit to degrees Celsius. When the sub process converttemp calls this function, variables containing parameter values are passed to the function. The conversion result is returned to the call process and displayed in the message box.
Sub converttemp ()
Temp = inputbox ("Enter the Fahrenheit temperature. ", 1)
Msgbox "temperature is" & Celsius (temp) & "Celsius. "
End sub
Function Celsius (fdegrees)
Celsius = (fdegrees-32) * 5/9
End Function
Process Data Entry and Exit
A parameter is used to transmit data to a process. The parameter is used as a placeholder for the data to be passed to the process. The parameter name can be any valid variable name. When using a sub or function statement to create a process, the process name must be followed by parentheses. All parameters are included in the brackets, which are separated by commas. For example, in the following example, fdegrees is a placeholder for the value passed to the Celsius function:
Function Celsius (fdegrees)
Celsius = (fdegrees-32) * 5/9
End Function
To obtain data from a process, you must use the function process. Remember that the function process can return values; the sub process does not return values.
InCodeUsing sub and Function
When calling a function, the function name must be used in the right side or expression of the variable assignment statement. For example:
Temp = Celsius (fdegrees)
Or
Msgbox "temperature:" & Celsius (fdegrees) & "Celsius. "
When calling the sub process, you only need to enter the process name and all parameter values. The parameter values are separated by commas. You do not need to use the call statement, but if you use this statement, you must include all parameters in brackets.
The following example shows two methods to call the myproc process. One uses the call statement, and the other does not. The two methods have the same effect.
Call myproc (firstarg, secondarg)
Myproc firstarg, secondarg
Note that parentheses are omitted when the call statement is not used.