Functions and Subroutines are simple applications of ASP which are applied to make programming effortless and time saving. Both of them are re-usable codes which are easy to manage and have to be written once. The codes can be called upon when required. These two basic applications of ASP have been discussed in this article.
Functions:
They provide alternatives to developers in constructing a code that can be used again and again for all the projects.
Functions provide developers with an option to produce code that can be used time and time again within a project and beyond, yet many ASP developers tend to shy away from using them extensively.
Functions return to you a value based on the information you provide.
Function Example:
<%
Function getAnimal(strCard)
Dim Tempstr
strCard = lcase(strCard) 'This converts the value lowercase.
Select Case strCard
Case "tiger" Tempstr = "Here is your tiger card"
Case "horse" Tempstr = "Here is your horse card"
Case "dog" Tempstr = "Here is your dog card"
Case "fish" Tempstr = "Here is your fish card"
Case Else Tempstr = "Sorry, we have sold out of that Animal"
End Select
getCard = Tempstr
End Function
%>
The information passed as a Function, is known as an 'argument'. The information we get back from a Function is known as the 'return value'. A Function can have many arguments, but only one return value.
Subroutines:
Subroutines can save you having to repeat blocks of code. The use of subroutines may be very useful when there are a lot of instructions to be performed within a subroutine. This way it will allow us to simplify the structure of our script. Subroutines do not send back the information instead uses the data
Example 1
<%
TheName=request.form("name)
if TheName="John" then
response.write ("Hi, John. How are you?")
response.write ("<br>Did you know I got married last month?")
else
response.write ("Hi. How are you?")
end if
%>
Example 2
<%
TheName=request.form("name)
if TheName="John" then
ResponseToJohn()
else
ResponseToUnknown()
end if
Sub ResponseToJohn()
response.write ("Hi, John. How are you?")
response.write ("<br>Did you know I got married last month?")
End Sub
Sub ResponseToUnknown()
response.write ("Hi. How are you?")
End Sub
%>
In order to call a subroutine, we will use this kind of code:
Whatever()
Where Whatever is the name of the subroutine (it is recommended to use a very descriptive name of the task we want to perform within the subroutine to make it easier to understand the script). We may also provide information to the subroutine in order to perform the specified task. The data will be provided this way:
Whatever(data1, data2 ... dataN)
This article gives a brief description of Functions and Subroutines which will help you in writing effective codes.
This article has been compiled by the content development team at Pegasus InfoCorp which pulls subject matter specialists from different work domains. They can be contacted through the Pegasus InfoCorp website at info@pegasusinfocorp.com. Pegasus InfoCorp is an India based web design, web development and online/ offline software development company. Please visit http://www.pegasusinfocorp.com to read more articles and know more about us!
Other companies and organizations are welcome to reprint this article
on their websites provided the following conditions are met.