Procedures
Calling JavaScript and VBScript Procedures in ASP
In ASP, it is possible to call a JavaScript procedure from VBScript and also call a VBScript procedure from JavaScript. This allows flexibility when working with different scripting languages in the same ASP application.
Procedures in ASP
ASP source code can include both procedures and functions.
Example (VBScript Procedure):
<% Sub vbproc(num1, num2) Response.Write(num1 * num2) End Sub %>
Result: <% Call vbproc(3,4) %>
In this example, a VBScript sub procedure multiplies two numbers and displays the result.
Using Another Scripting Language
To write procedures or functions in a different scripting language, specify the language at the top of the ASP file using:
<%@ language="javascript" %>
Example (JavaScript Procedure):
<%@ language="javascript" %>
<% function jsproc(num1, num2) { Response.Write(num1 * num2); } %>Result: <% jsproc(3,4) %>
Differences Between VBScript and JavaScript Procedure Calls
Calling from VBScript:
You can use the Call keyword followed by the procedure name.
If Call is used, parameters must be enclosed in parentheses.
If Call is not used, parameters must not be enclosed in parentheses.
If the procedure has no parameters, parentheses are optional.
Calling from JavaScript:
Parentheses must always be used, regardless of whether parameters exist.
VBScript Procedures
VBScript supports two types of procedures:
Sub Procedures
Function Procedures
VBScript Sub Procedures
A Sub procedure:
Contains a block of statements between Sub and End Sub
Performs actions but does not return a value
Can accept parameters
Syntax:
Sub mysub() statements End Sub
Or with arguments:
Sub mysub(arg1, arg2) statements End Sub
Example:
Sub mysub() Response.Write("I was written by a sub procedure") End Sub
VBScript Function Procedures
A Function procedure:
Is enclosed between Function and End Function
Can perform actions and returns a value
Can accept parameters
Must include empty parentheses if no arguments are used
Returns a value by assigning it to the function name
Syntax:
Function myfunction() statements myfunction = value End Function
Or with arguments:
Function myfunction(arg1, arg2) statements myfunction = value End Function
Example:
Function myfunction() myfunction = Date() End Function
Calling a Procedure
Below is a function that calculates the sum of two numbers:
Function myfunction(a, b) myfunction = a + b End Function Response.Write(myfunction(5, 9))
The function returns 14, which is the sum of the two arguments.
Using the Call Statement
You can call a procedure in two ways:
Using Call:
Call MyProc(argument)
Without Call:
MyProc argument
Both approaches work in VBScript.