Chapter 31: ASP Procedures
1. What is a “Procedure” in Classic ASP?
In Classic ASP (VBScript), a procedure is just a named block of reusable code — exactly like a function or subroutine in other languages.
There are two kinds:
| Kind | Keyword | Returns a value? | Typical use case |
|---|---|---|---|
| Sub | Sub … End Sub | No | Do something (print HTML, send email, log, redirect) |
| Function | Function … End Function | Yes | Calculate something and return a result (format price, check login, build string) |
Golden Rule (write this down):
- Use Sub when you just want to perform an action
- Use Function when you want to return a value to the caller
2. Basic Syntax – Sub vs Function
Sub (does not return value)
Sub SayHello(name)
Response.Write "<p>Hello, " & Server.HTMLEncode(name) & "! Welcome from Hyderabad!</p>"
End SubFunction (returns value)
|
0 1 2 3 4 5 6 |
Function FormatPrice(amount) FormatPrice = "₹" & FormatNumber(amount, 2) End Function |
3. Real Example 1 – Simple Sub (Print Greeting)
File: welcome.asp
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <body> <h1>ASP Procedures Example</h1> <% ' Call the Sub Call SayHello("Rahul") Call SayHello("Priya") Call SayHello("Webliance") %> <% ' Define the Sub Sub SayHello(userName) Dim greeting greeting = "Namaste" Response.Write "<p style='color:blue; font-size:1.2em;'>" Response.Write greeting & ", " & Server.HTMLEncode(userName) & "! 🌟" Response.Write "</p>" End Sub %> </body> </html> |
Important notes:
- You can call a Sub with Call SayHello(“Rahul”) or just SayHello “Rahul” (both work)
- Subs are often used to print repeated HTML (header, footer, menu, alert boxes)
- Always use Server.HTMLEncode when printing user-controlled data
4. Real Example 2 – Function that Returns a Value
|
0 1 2 3 4 5 6 |
<% ' Call the function and use the result Dim price, finalPrice price = 399.50 finalPrice = AddTax(price, 18) Response.Write "<p>Original price: ₹" & FormatNumber(price, 2) & "</p>" Response.Write "<p>With 18% GST: ₹" & FormatNumber(finalPrice, 2) & "</p>" %> <% Function AddTax(baseAmount, taxPercent) Dim tax, total tax = baseAmount * (taxPercent / 100) total = baseAmount + tax AddTax = total End Function %> |
Key points about Functions:
- The last line usually assigns to the function name → that becomes the return value
- You can have Exit Function to return early
- Very common for formatting (currency, dates), calculations, string building
5. Typical Real-World Classic ASP Pattern – Reusable Procedures in Included File
Most serious Classic ASP sites never put procedures directly in the main page. They put them in .inc files and include them.
File: common.inc (or functions.inc)
|
0 1 2 3 4 5 6 |
<% Sub ShowHeader(pageTitle) %> <!DOCTYPE html> <html> <head> <title><%= Server.HTMLEncode(pageTitle) %> - My Shop</title> <link href="style.css" rel="stylesheet"> </head> <body> <header> <h1>My Hyderabad Online Store</h1> <nav> <a href="default.asp">Home</a> | <a href="products.asp">Products</a> | <a href="cart.asp">Cart</a> </nav> </header> <% End Sub Function FormatIndianCurrency(amount) FormatIndianCurrency = "₹ " & FormatNumber(amount, 2) End Function %> |
Then in any page:
|
0 1 2 3 4 5 6 |
<!-- #include file="common.inc" --> <% Call ShowHeader("Products Page") Dim pPrice pPrice = 499.99 Response.Write "<p>Price: " & FormatIndianCurrency(pPrice) & "</p>" %> |
→ This is exactly how professional Classic ASP sites were built — reuse via includes.
6. Common Beginner Mistakes (and How to Avoid Them)
- Forgetting Option Explicit → typos create ghost variables → Always use it!
- Printing user input without Server.HTMLEncode → XSS vulnerability → Always encode when outputting to HTML
- Putting Dim inside loop → bad performance & confusion → Declare variables at the top of the block
- Confusing Sub vs Function → forgetting to assign return value → Remember: Function must assign to its own name
7. Teacher Summary – ASP Procedures in One Page
ASP Procedures (in Classic ASP with VBScript) means:
- Sub → named block that does something (print, redirect, log) Syntax: Sub Name(…) … End Sub
- Function → named block that returns a value Syntax: Function Name(…) … Name = result … End Function
- Can take parameters (ByRef default, ByVal optional)
- Usually stored in .inc files and included with <!– #include –>
- Called with Call SubName(args) or just SubName args
- Functions return by assigning to their own name
This was the way to organize code in Classic ASP — no classes, no modules, just Subs & Functions + includes.
Questions for next class?
- Want to see a full shopping cart example using Subs & Functions?
- Or how to pass parameters ByRef vs ByVal?
- Or compare Classic ASP procedures vs Razor @helper?
- Or move to the next W3Schools topic (ASP Forms)?
Just tell me — I’m here! 🚀🇮🇳 Keep learning strong, Webliance! 😊
