๐Ÿ’ป Classic ASP โ€“ Logic & Scripting
Estimated reading: 3 minutes 33 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 :

Leave a Reply

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

Share

๐Ÿงฑ Classic ASP โ€“ Procedures

Or Copy Link

CONTENTS
Scroll to Top