One of the most powerful features of any programming language is the ability to procedures. Rather than having all of your code in a sequential format, it is more efficient to break up your code into manageable blocks.
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.
<% Function Area(l,w) Area = l * w End Function %>
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.
<% Sub ShowDate Response.Write(“Today’s Date is: “) Response.Write(Now()) End Sub %>
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.
<% Function ProcName (var) ’ If the calling process omits “Call”, then the parameter is ’ passed ByVal. If the keyword “Call” is included, the parameter ’ is passed ByRef. End
Sub ProcName(ByRef var) ’ Calling process must specify the keyword “Call” var = new value, but also updates value of the originating parameter End Sub
Function ProcName (ByVal var) ’ Calling process may specify or omit the keword “Call” var = some new value End Function %>
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.
’ Call a function, results are assigned to variable, myArea. myArea = area(4,3)
’ Ignore the results from the funtion, or ’ call a subroutine area(4,3)
’ if passing ByRef, firstName and lastName will be ’ updated if new values are assigned in the procedure. Call fullName(firstName,lastName)