As you saw earlier in this chapter, a procedure is a group of script commands that performs a specific task. Specific procedures can be defined and called repeatedly in the scripts. Procedure definitions can appear within <SCRIPT> and </SCRIPT> tags and must follow the rules for the declared scripting language. You can define a procedure within the scripting delimiters <% and %> as long as the procedure is in the same scripting language as the primary script.
You can place procedure definitions in the same .asp file that calls the procedures. You can place commonly used procedures in a shared .asp file and use them with the server-side include statement <!--#INCLUDE FILE= ...--> to include the statement in other .asp files that call the procedures.
To call a procedure, include the name of the procedure in a command. For VBScript, use the Call keyword when calling a procedure, or just use the name of the procedure. The several methods that follow address conditions for calling a procedure:
You can call procedures by simply using the procedure name inside a script block. For example, to call a procedure named PrintDate, simply code the following:
PrintDate
You can use the following syntax to call the same procedure inside HTML:
<% PrintDate %>
To continue the example, the proper syntax for passing parameters when you call a procedure is
PrintFact "IBM"
Functions are a special type of procedure that return a value. You can call functions using this type of syntax:
OrderedQuantity = CalculateOrders("All")
The following example illustrates creating and calling procedures using two different scripting languages, VBScript and JScript.
<HTML> <BODY> <TABLE> <% Call Echo %> </TABLE> <% Call PrintDate %> </BODY> </HTML> <SCRIPT LANGUAGE=VBScript RUNAT=Server> Sub Echo Response.Write _ "<TR><TD>Name</TD><TD>Value</TD></TR>" Set Params = Request.QueryString For Each p in Params Response.Write "<TR><TD>" & p & "</TD><TD>" & _ Params(p) & "</TD></TR>" Next End Sub </SCRIPT> <SCRIPT LANGUAGE=JavaScript RUNAT=Server> function PrintDate() { var x x = new Date() Response.Write(x.toString()) } </SCRIPT>
To pass an entire array to a procedure in VBScript, use the array name followed by empty parentheses. In JScript, enclose the array name in square brackets.