ASP Hosting sale!
1,000MB space, 40GB transfer, 1 SQL Server 2000 db! - $9.95 per month*!
Limited Time Offer!

aspdev | articles | tutorials | forums

ASPdev -> ASP Tutorial -> ASP Function and Procedures

ASP Function and Procedures

Functions and procedures provide a way to create re-usable modules of programming code and avoid repeating the same block of code every time you do the same task.

The ASP pages are executed from top to bottom and if you don't have any functions/procedures in your ASP page, the ASP parsing engine simply processes your entire file from the beginning to the end.

ASP (VBScript) functions and procedures, on the other hand, are executed only when called, from within the ASP code (they might be called from another function/procedure or directly from inline code).

VBScript has 2 types of functions. The ones returning a value start with Function keyword and end with End Function. The second type of function in VBScript doesnít return a value starts with the Sub keyword and ends with End Sub, defining the function as a subroutine.

Consider the following VBScript Function example:

Function MultNum(iNum1, iNum2)
MultNum = iNum1 * iNum2
End Function

The function above gets 2 arguments and returns them multiplied.
VBScript functions can be without arguments or they can have 1 or more arguments.
To return a value from a VBScript function, just assign this value to the name of the function:

MultNum = iNum1 * iNum2

VBScript subroutines donít return value. Consider the following example:

Sub ShowNumber(iNumber)
End Sub

The subroutine above just displays the number passed as an argument in a message box.
VBScript Subroutines like VBScript functions can be with or without arguments.

You can call a VBScript Function in the following way:

iResult = MultNum(5, 10)

The execution of the MultNum function with 5 and 10 as arguments will return 50, which will be assigned to iResult variable.

To call a VBScript Subroutine, do one of the following:

Call ShowNumber(100)

Or just

ShowNumber 100

Note that if you use the Call keyword in front of the Subroutine name you need to use parentheses after the subroutine name.

Contact Us