๐งฑ 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
SubandFunction - How to define and call procedures in
.aspfiles - 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
Functionfor reusable logic with a return value - Use
Subfor 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:
Subdoes something;Functionreturns something- Use
Callonly 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 :
