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)

asp
Sub SayHello(name)
Response.Write "<p>Hello, " & Server.HTMLEncode(name) & "! Welcome from Hyderabad!</p>"
End Sub

Function (returns value)

asp

3. Real Example 1 – Simple Sub (Print Greeting)

File: welcome.asp

asp

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

asp

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)

asp

Then in any page:

asp

→ This is exactly how professional Classic ASP sites were built — reuse via includes.

6. Common Beginner Mistakes (and How to Avoid Them)

  1. Forgetting Option Explicit → typos create ghost variables → Always use it!
  2. Printing user input without Server.HTMLEncode → XSS vulnerability → Always encode when outputting to HTML
  3. Putting Dim inside loop → bad performance & confusion → Declare variables at the top of the block
  4. 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! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *