Chapter 43: VBScript Functions
1. What is a “Function” in VBScript (Classic ASP)?
In VBScript (the default language inside .asp files):
- A Sub → does something but does not return a value
- A Function → does something and returns a value to the caller
Very important rule (write this 10 times):
In VBScript, the only way a procedure returns a value is by assigning something to the function’s own name inside the function body.
Syntax:
|
0 1 2 3 4 5 6 7 8 9 |
Function FunctionName(parameters) ' ... your code ... FunctionName = someValue ' ← this is the return value End Function |
If you forget to assign to FunctionName = …, the function returns Empty (like null).
2. Basic Example 1 – Simple Function that Returns a String
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <body> <h2>VBScript Function Example</h2> <% Dim fullGreeting fullGreeting = GetGreeting("Rahul", "Hyderabad") Response.Write "<p>" & Server.HTMLEncode(fullGreeting) & "</p>" %> <% Function GetGreeting(userName, cityName) Dim greeting greeting = "Namaste " & userName & "! Welcome from " & cityName & "!" GetGreeting = greeting ' ← assign to function name = return value End Function %> </body> </html> |
Output in browser:
|
0 1 2 3 4 5 6 |
Namaste Rahul! Welcome from Hyderabad! |
3. Example 2 – Function that Returns a Number (Price with Tax)
|
0 1 2 3 4 5 6 |
<% Dim basePrice, taxRate, finalPrice basePrice = 399.50 taxRate = 18 finalPrice = CalculateTax(basePrice, taxRate) Response.Write "<p>Base price: ₹" & FormatNumber(basePrice, 2) & "</p>" Response.Write "<p>With " & taxRate & "% GST: ₹" & FormatNumber(finalPrice, 2) & "</p>" %> <% Function CalculateTax(baseAmount, taxPercent) Dim taxAmount, total taxAmount = baseAmount * (taxPercent / 100) total = baseAmount + taxAmount CalculateTax = total ' ← this is what the caller gets End Function %> |
4. Example 3 – Function that Returns a Boolean (Validation)
|
0 1 2 3 4 5 6 |
<% Dim email email = "rahul@example.com" If IsValidEmail(email) Then Response.Write "<p style='color:green;'>Email looks valid!</p>" Else Response.Write "<p style='color:red;'>Invalid email format.</p>" End If %> <% Function IsValidEmail(strEmail) Dim regEx Set regEx = New RegExp regEx.Pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" regEx.IgnoreCase = True IsValidEmail = regEx.Test(strEmail) Set regEx = Nothing End Function %> |
→ Classic ASP used RegExp object for simple validation — very common pattern.
5. Example 4 – Function with Optional Parameters & Default Values
VBScript does not have true optional parameters like modern languages, but you can simulate it:
|
0 1 2 3 4 5 6 |
<% Dim msg1, msg2 msg1 = FormatMessage("Rahul") msg2 = FormatMessage("Priya", "Hyderabad") Response.Write "<p>" & msg1 & "</p>" Response.Write "<p>" & msg2 & "</p>" %> <% Function FormatMessage(userName, cityName) If IsEmpty(cityName) Or cityName = "" Then cityName = "India" End If FormatMessage = "Hello " & Server.HTMLEncode(userName) & " from " & Server.HTMLEncode(cityName) & "!" End Function %> |
→ Check IsEmpty or = “” to detect missing optional parameter.
6. Common Real-World Function Patterns in Classic ASP
- Currency formatting (very common)
|
0 1 2 3 4 5 6 |
Function FormatIndianCurrency(amount) FormatIndianCurrency = "₹ " & FormatNumber(amount, 2) End Function |
- Safe string output (every site had this)
|
0 1 2 3 4 5 6 |
Function SafeOutput(str) SafeOutput = Server.HTMLEncode(Trim(str & "")) End Function |
- SQL injection prevention (poor man’s version)
|
0 1 2 3 4 5 6 |
Function SafeSQL(str) SafeSQL = Replace(str, "'", "''") End Function |
7. Teacher Summary – VBScript Functions in Classic ASP
VBScript Functions (used inside Classic ASP) means:
- Named block that returns a value
- Syntax: Function Name(…) … Name = result … End Function
- Return by assigning to the function’s own name
- Can have parameters (all ByRef by default)
- Very common for: formatting, validation, calculations, string building
- No true optional parameters — simulate with IsEmpty / = “” checks
- Always use Server.HTMLEncode on strings going to HTML
- Often stored in .inc files and included
This is how millions of Classic ASP pages calculated prices, formatted dates, validated input, built HTML fragments, and more — and many legacy Indian systems still use exactly this function style in 2026.
Next class?
- Want a full example with functions for formatting + validation + email sending?
- Or how to pass arrays/objects to functions?
- Or compare VBScript Function vs Razor @helper?
- Or move to the next W3Schools topic (ASP Redim or ASP Date Functions)?
Just tell me — I’m here! 🚀🇮🇳 Keep learning strong, Webliance! 😊
