Subroutines and functions,
You use subroutines and functions for program-code that you need more then once in 1 script. You only need to write the code once, but you can use it several times. You can also get functions from outside the script. Then you get 1 function that you use on more pages.
Subroutines and function do both calculations both only functions return a value.
Subroutines
SUB subroutinename(parameters) instructions END SUBTo call the subroutine
CALL subroutinename(parameters)Example(c11.asp):
<% @language=vbscript %> <% response.buffer=true %> <HTML> <HEAD><TITLE>Example subroutines</TITLE></HEAD> <BODY> <% if request.form("entry")="" then response.write("<form action=c11.asp method=post>") response.write("Give the side of a square <input type=text size=3 name:entry>") response.write("<input type=submit value=calculate>") response.write("</form>") else response.write("<h2>Square</h2>") response.write("The surface is: ") call surfacesquare(request.form("entry")) end if %> </BODY> </HTML> <SCRIPT language=vbscript runat=server> Sub surfacesquare(side) response.write(side*side) end sub </script>As you can see this code combines 2 pages in 1. One who ask to user to enter something and another that gives the result.
Function
FUNCTION functionname(parameters) instructions functionname=calculation END FUNCTIONThe function returns a value by giving a value to the functionname.
Example:
<% @language=vbscript %> <HTML> <HEAD><TITLE>Example function</TITLE></HEAD> <BODY> <% if request.form("entry")=" " then response.write("<form action=c11.asp method=post>") response.write("Give the side of the square <input type=text size=3 name=entry><br>") response.write("<input type=submit value=calculate>") response.write("</form>") else response.write("<H2>square</H2>") response.write("The surface is: ") response.write("surfacesquare(request.form("entry"))) end if %> </BODY> </HTML> <script language=vbscript runat=server> FUNCTION surfacesquare(side) surfacesquare=side*side END FUNCTION </script>
Local and global variables
Local variables
The variables you create in a subroutine or a function are local variables. They are only known inside the function or subroutine.
Example:
SUB subroutine1 DIM counter counter=5 END SUB SUB subroutine2 DIM counter counter=8 END SUBYou can use the same variable name in different sub-functions without conflicts.
Global variables
Variables created in the script, can be used in subroutines.
Example:
<% @language=vbscript %> <HTML> <HEAD><TITLE>Example Global variables</TITLE></HEAD> <BODY> <% DIM strglobal strglobal="The global variable" response.write(strglobal) SUB subroutine1 DIM counter counter=5 response.write(counter) response.write("<P>"& strglobal &"</P>") END SUB SUB subroutine2 DIM counter counter=8 response.write(counter) response.write("<P>"& strglobal &"</P>") END SUB %> <P>Subroutine 1 ...<%subroutine1()%> <P>Subroutine 2 ...<%subroutine2()%> <P>Subroutine 1 ...<%subroutine1()%> </BODY> </HTML>
Public and private variables
1 ASP-page can have more then 1 script.
Private variables are only known in the script they are defined. They are defined with the word
PRIVATEinstead ofDIM.
PRIVATE strtextPublic variables are also know outside the script they are defined. They are defined with the word
PUBLICinstead ofDIM.
PUBLIC strtext