Appendix: VBScript passed to the function. The result of the calculation is returned to the calling procedure and displayed in a message box. Sub ConvertTemp() temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function Getting Data Into and Out of Procedures Each piece of data is passed into your procedures using an argument . Arguments serve as placeholders for the data you want to pass into your procedure. You can name your arguments any valid variable name.
When you create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. Any arguments are placed inside these parentheses, separated by commas. For example, in the following example, fDegrees is a placeholder for the value being passed into the Celsius function for conversion. Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure cannot.
Using Sub and Function Procedures in Code A Function in your code must always be used on the right side of a variable assignment or in an expression. For example: Temp = Celsius(fDegrees) or MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees." To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses. The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing.
Call MyProc(firstarg, secondarg) MyProc firstarg, secondarg Notice that the parentheses are omitted in the call when the Call statement isn't used. Creating Constants A constant is a meaningful name that takes the place of a number or string and never changes. VBScript defines a number of intrinsic constants. You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values. For example: Const MyString = "This is my string." Const MyAge = 49 Note that the string literal is enclosed in quotation marks ( " " ).
Quotation marks are the most obvious way to differentiate string values from numeric values. You represent Date literals and time literals by enclosing them in number signs ( # ). For example: Const CutoffDate = #6-1-97# You may want to adopt a naming scheme to differentiate constants from variables. This will prevent you from trying to reassign constant values while your script is running. For example, you might want to use Page 666 InduSoft Web Studio