User-Defined Procedures and Functions

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.

Calling 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:

  • If the procedure that is called requires any arguments, you must enclose the argument list in parentheses.
  • If you omit the Call keyword, you must also omit the parentheses around the argument list.
  • If you use the Call syntax to call any built-in or user-defined function, the function's return value is discarded.
  • If JScript procedures are called from VBScript, you must use parentheses after the procedure name.
  • If a JScript procedure has no arguments, use empty parentheses.

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.



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net