The difference between a Sub and a Function

February 3, 2009 by AcidRaZor · Leave a Comment
Filed under: Classic ASP, Programming 101 

During the last several years in the programming field I have seen Junior’s (even more seasoned guys) make the simple mistake of using Function’s for everything. Some even used Sub’s… I suspect it’s because they didn’t know any better.

True, nowadays with the advent of .NET and PHP languages that instill (or force for that matter) you to use the correct syntax for each moment, I feel as hobbyists who won’t necessarily go to school to learn this, might not be clued up.

Simply stated. a Function returns a value, a Sub does not.

An example of this in Classic ASP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Dim myVariable : myVariable = "AcidRaZor"
 
If checkIfTrue(myVariable) Then
Response.Write "This is indeed true"
End If
 
Function checkIfTrue(var)
Dim ret : ret = false
If var = "AcidRaZor" Then
return = true
End If
 
checkIfTrue = ret
 
End Function

An example of a Sub would be:

1
2
3
4
5
6
7
Dim myVariable : myVariable = "AcidRaZor"
 
saveThisName myVariable
 
Sub saveThisName(var)
Session("nameSaved") = var
End Sub

This spills over (and hopefully answers some of your questions) into the new versions of .NET and beyond. Returning variables from Function’s or just processing data with Sub’s. Soon the student will become the teacher :)

SEO Powered by Platinum SEO from Techblissonline