Each block of code is typically used to process a particular problem. These blocks of code are also known as Procedures. Generally, there are two types of procedures: Functions and Subroutines.

Functions

A function is a procedure that returns a value back to the caller. The “caller” is where the procedure was invoked from. Functions are created (declared) using the Function statement and terminated with an End Function statement. In the previous example, we have an example of a function that accepts two parameters (more about parameters below). This function takes both parameters and multiplies the two and returns the results to the calling process. While this function returns data, there is no requirement for this to occur. You can declare this function anywhere in your web page and it will be made available for you to call from anywhere within the page.

Subroutines

A subroutine is a procedure that does not return a value to the caller. Subroutines are declared using the Sub statement and terminated with an End Sub statement. In this example, we have a subroutine that will simply write the string and current date/time information on the screen. Unlike the function, no value is returned back to the calling process. A subroutine may or may not have parameters passed.

Parameters

A parameter is an ASP value that is passed to the procedure for the purpose of completing a task. Parameters can either be passed ByVal (by value) or ByRef (by reference). If you pass the parameter by value, any changes to the parameter within the procedure, will not be reflected in the original argument. If you pass your parameter ByRef, then any changes to the parameter update the original argument. If you want to pass your parameters ByRef, you must include the Call statement, when calling the procedure.

Calling Procedures

When invoking a function or subroutine, you need to be aware of the different calling conventions used for each. If you do not use the proper calling conventions, you may get unexpected results (ByVal vs ByRef). Generally, when you call a function or a procedure, you enclose the parameters within a set of parenthesis. If you are passing string data, enclose the string within quotes. If you are passing multiple parameters, separate the parameters using commas. Use the keyword Call, especially if you are passing parameters via ByRef.