Functions and procedures provide a way to avoid rewriting the same block of code every time you want to perform a particular task. Both VBScript and JScript allow you to call a function or procedure from any point in a script. This sample demonstrates how you can create and use these tools with ASP.
If you don't have any functions in your ASP page, the ASP engine simply processes your entire file, start to finish, each time it is requested by a client browser. Functions and procedures, however, are executed only when called, not inline with the rest of the code.
You can denote functions and procedures in VBScript or JScript by using the Function statement. In addition, VBScript makes a distinction between a function that returns a value and a function that does not; a function that returns a value is denoted with the Sub statement, indicating that it is a subroutine.
This sample defines one function, PrintOutMsg, which takes as parameters a message, and a number that specifies how many times the message is to be written to the client browser with the Response.Write method. The function, for the purposes of this sample, simply returns to the client browser the number of times the message was printed.
It is important to note the RUNAT attribute of the <SCRIPT> tag. If the RUNAT attribute is not included, ASP will assume that the script is client-side scripting, and will pass the code back to the browser for processing. ASP would then not recognize the PrintOutMsg function call, return an error, and abort.
The VBScript and JScript versions of this script are available in the IIS samples directory, at ...\asp\simple\Functions_VBScript.asp and ...\asp\simple\Functions_JScript.asp.