๐Ÿ’ป Classic ASP โ€“ Logic & Scripting
Estimated reading: 3 minutes 470 views

Classic ASP โ€“ Procedures โ€“ Use Functions and Subroutines in VBScript

Introduction โ€“ What Are Procedures in Classic ASP?

In Classic ASP, procedures are reusable blocks of VBScript code used to organize logic, avoid repetition, and modularize scripts. There are two types:

  • Subroutines (Sub) โ€“ perform actions without returning a value.
  • Functions (Function) โ€“ return a value to the calling code.

In this guide, youโ€™ll learn:

  • The difference between Sub and Function
  • How to define and call procedures in .asp files
  • How to pass arguments and return results
  • Best practices for structured, reusable Classic ASP code

What Is a Subroutine (Sub)?

A Sub performs an action but does not return a value.

<%
Sub ShowGreeting(name)
    Response.Write "Hello, " & name & "!" & "<br>"
End Sub

Call ShowGreeting("Vaibhav")
%>

Output:
Hello, Vaibhav!


Example โ€“ Sub Without Call

In Classic ASP, Call is optional but required if you’re using parentheses:

ShowGreeting "Admin"  ' No parentheses = no Call
Call ShowGreeting("Admin")  ' With parentheses = use Call

What Is a Function (Function)?

A Function performs logic and returns a value using the function name.

<%
Function Add(a, b)
    Add = a + b
End Function

Dim result
result = Add(5, 7)
Response.Write "Sum: " & result
%>

Output:
Sum: 12


Procedure with Conditional Logic

<%
Function Grade(score)
    If score >= 90 Then
        Grade = "A"
    ElseIf score >= 80 Then
        Grade = "B"
    Else
        Grade = "C"
    End If
End Function

Response.Write "Grade: " & Grade(85)
%>

Output: Grade: B


Passing Parameters by Value and Reference

VBScript passes arguments by reference by default.

Example: Modify External Variable

<%
Sub DoubleIt(x)
    x = x * 2
End Sub

Dim num
num = 10
Call DoubleIt(num)
Response.Write num  ' Output: 20
%>

Use ByVal to prevent modification:

Sub ProtectIt(ByVal x)
    x = x * 2
End Sub

Function Returning String

<%
Function Greet(user)
    Greet = "Welcome, " & user & "!"
End Function

Response.Write Greet("Guest")
%>

Output: Welcome, Guest!


Nesting Procedures (Not Recommended)

VBScript does not support nested procedures. Define all Sub and Function blocks at the top or outside of conditional logic.

Invalid:

If true Then
    Sub SayHi()
        Response.Write "Hi"
    End Sub
End If

Best Practices for Classic ASP Procedures

Do:

  • Use Function for reusable logic with a return value
  • Use Sub for display/output-related tasks
  • Keep each procedure focused on a single task

Avoid:

  • Mixing display logic and calculations in one function
  • Defining procedures inside conditionals or loops
  • Using global variables when local ones suffice

Summary โ€“ Recap & Next Steps

Procedures in Classic ASP make your VBScript code modular, reusable, and readable. Use Sub for actions and Function for values. This practice improves maintainability, especially in large .asp applications.

Key Takeaways:

  • Sub does something; Function returns something
  • Use Call only when invoking with parentheses
  • Prefer modular, scoped, and named procedures

Real-world Use Cases:

  • Calculating totals, taxes, or discounts
  • Rendering greetings or formatted content
  • Processing form inputs into standardized formats

FAQs โ€“ Classic ASP Procedures


Whatโ€™s the difference between Sub and Function?
A Sub performs a task but returns nothing. A Function performs a task and returns a value.


Is Call required to run a Sub?
Only when you use parentheses around arguments.


Can I return multiple values from a Function?
Not directly. Use arrays or modify passed-by-reference arguments instead.


Share Now :
Share

๐Ÿงฑ Classic ASP โ€“ Procedures

Or Copy Link

CONTENTS
Scroll to Top