VBScript Programs
VBScript can use two kinds of procedures:
- Subroutines
- Function procedures
VBScript Subroutines
Subroutines:
- are a series of statements enclosed within Sub and End Sub statements
- can perform certain operations, butdo not returnValue
- can have parameters
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
Example (for IE only)
Sub mysub()
document.write("I was written by a sub procedure")
End Sub
Try it »
VBScript Functions
Function procedures
- are a series of statements enclosed within Function and End Function statements
- can perform certain operations, andreturnsValue
- can have parameters passed to them through procedure calls.
- If there are no parameters, empty parentheses () must be included
- A function can return a value by assigning a value to the function name
Function myfunction()
some statements
myfunction=
some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=
some value
End Function
Example (for IE only)
function myfunction()
myfunction=Date()
end function
Try it »
Calling Procedures
This simple function is called to calculate the sum of two parameters:
Example (for IE only)
Function myfunction(a,b)
myfunction=a+b
End Function
document.write(myfunction(5,9))
Try it »
The function "myfunction" returns the sum of parameter "a" and parameter "b". Here the return value is 14.
When you call a procedure, you can use the Call statement, as shown below:
Call MyProc(argument)
Alternatively, you can omit the Call statement, as shown below:
MyProc argument