VBScript Procedures
VBScript can use two kinds of procedures:
- Sub procedures
- Function procedures
VBScript Sub Procedures
Sub procedure:
- is a series of statements, enclosed by Sub and End Sub statements
- can perform some actions, butdoes not return a valueValue
- can take parameters
Sub mysub()
some statements
End Sub
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
some statements
End Sub
Example (IE only)
Sub mysub()
document.write("I was written by a sub procedure")
End Sub
document.write("I was written by a sub procedure")
End Sub
Try it Yourself »
VBScript Function Procedures
Function procedure:
- is a series of statements, enclosed by Function and End Function statements
- can perform some actions, andreturns a valueValue
- can take parameters passed to it by calling the procedure.
- If there are no parameters, it must have empty parentheses ()
- By assigning a value to the function procedure name, it can return a value
Function myfunction()
some statements
myfunction=some value
End Function
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
some statements
myfunction=some value
End Function
Calling a Procedure
This simple function procedure is called to calculate the sum of two parameters:
Example (IE only)
Function myfunction(a,b)
myfunction=a+b
End Function
document.write(myfunction(5,9))
myfunction=a+b
End Function
document.write(myfunction(5,9))
Try it Yourself »
The function "myfunction" returns the sum of parameter "a" and parameter "b". Here it returns 14.
When you call a procedure, you can use the Call statement, as shown below:
Call MyProc(argument)
Or, you can omit the Call statement, as shown below:
MyProc argument
Other Extensions