Procedures and Functions |
|
A sub procedure is an assignment that is carried but doesn't give back a result. To create a sub procedure, you start the section with the Sub keyword followed by a name (like everything else, a procedure must have a name). The name of a procedure is always followed by parentheses. At the end of the sub procedure, you must type End Sub. Therefore, the formula of a sub procedure is: Sub ProcedureName() End Sub The name of a procedure follows the same rules we reviewed for variables. In addition, there are some suggestions you can use when naming your procedures:
On the right side of the name of the procedure, you can add parentheses or omit them. The section between the Sub and the End Sub lines is referred to as the body of the procedure. Here is an example: <% Sub ShowFullName End Sub %> The body of the procedure is used to define what assignment and how the assignment should be carried. For example, if you need to use a variable, you can declare it and specify the kind of variable you need. There is no restriction on the type of variables that can be declared in a procedure. Here is an example in which a variable is declared in the body of a sub routine: |
|
<% Sub ShowFullName Dim FirstName End Sub %> |
In the same way, you can declare as many variables as you need inside of a procedure. The actions you perform inside of a procedure depend on what you are trying to accomplish. For example, a procedure can simply be used to create a string. The above procedure can be changed as follows: <% Sub ShowFullName Dim FirstName Dim LastName Dim FullName FirstName = "Paul" LastName = "Motto" FullName = LastName & ", " & FirstName Response.Write("Person Name: " & FullName) End Sub %> |
Practical Learning: Creating a Sub Procedure |
<%@ Language="VBScript" %> <html> <head> <title>Geometry: The Square</title> </head> <body> <h1>Geometric Figures: The Square</h1> <p>A square is a geometric figure made of 4 equal sides joined at their ends to form 4 right angles.</p> <form action="square2.asp" method="get"> <table border="0" width="316"> <tr> <td width="109">Side:</td> <td width="52"><input type="text" name="txtSide" size="14" value = <% =Request.QueryString("txtSide") %> > </td> <td width="68"><input type="submit" value="Calculate" name="btnCalculate"></td> <td width="67"><input type="reset" value="Restart" name="btnReset"></td> </tr> </table> </form> <% ' This procedure is used to calculate and display ' the perimeter of a square based on the side from the form Sub SquarePerimeter() Dim dblSide Dim dblPerimeter ' Get the measure of the side from the form dblSide = Request.QueryString("txtSide") ' Calculate the perimeter dblPerimeter = dblSide * 4 ' Display the perimeter in the browser Response.Write("Perimeter: " & dblPerimeter & "<br>") End Sub ' This procedure is used to calculate and display ' the area of a square based on the side from the form Sub SquareArea Dim dblSide Dim dblArea ' Get the measure of the side from the form dblSide = Request.QueryString("txtSide") ' Calculate the area dblArea = dblSide * dblSide ' Display the are in the browser Response.Write("Area: " & dblArea) End Sub %> </body> </html> |
Sub Procedure Call |
Once you have a procedure, whether you created it or it is part of VBScript, you can use it. Using a procedure is also referred to as calling it. Before calling a procedure, you should first locate the section of code in which you want to use it. To call a simple procedure, simply type its name in the section where you want to use. Here is an example: <% Sub ShowFullName Dim FirstName Dim LastName Dim FullName FirstName = "Paul" LastName = "Motto" FullName = LastName & ", " & FirstName Response.Write("Person Name: " & FullName) End Sub %> <% ShowFullName %> You add or omit parentheses on the right side of the name of the procedure when calling it. |
Practical Learning: Calling a Sub Procedure |
<%@ Language="VBScript" %> <html> <head> <title>Geometry: The Square</title> </head> <body> <h1>Geometric Figures: The Square</h1> <p>A square is a geometric figure made of 4 equal sides joined at their ends to form 4 right angles.</p> <form action="square2.asp" method="get"> <table border="0" width="316"> <tr> <td width="109">Side:</td> <td width="52"><input type="text" name="txtSide" size="14" value = <% =Request.QueryString("txtSide") %> > </td> <td width="68"><input type="submit" value="Calculate" name="btnCalculate"></td> <td width="67"><input type="reset" value="Restart" name="btnReset"></td> </tr> </table> </form> <% ' This procedure is used to calculate and display ' the perimeter of a square based on the side from the form Sub SquarePerimeter() Dim dblSide Dim dblPerimeter ' Get the measure of the side from the form dblSide = Request.QueryString("txtSide") ' Calculate the perimeter dblPerimeter = dblSide * 4 ' Display the perimeter in the browser Response.Write("Perimeter: " & dblPerimeter & "<br>") End Sub ' This procedure is used to calculate and display ' the area of a square based on the side from the form Sub SquareArea Dim dblSide Dim dblArea ' Get the measure of the side from the form dblSide = Request.QueryString("txtSide") ' Calculate the area dblArea = dblSide * dblSide ' Display the are in the browser Response.Write("Area: " & dblArea) End Sub %> <% SquarePerimeter %> <% SquareArea() %> </body> </html> |
Server-Side Processing |
So far, we wrote our procedures in the same file that request the values being processed. Another way you can perform this type of job, as we saw in Lesson 4, is to divide the assignment among different files. For example, you can write your code in one file that would request some values from the user. Once the user clicks the sending button, another file would receive the values and process them. This second file can then optionally display the values to the user or it can send the value to another file. Everything is done as you see fit. Perhaps planning is one of the most important aspects of implementing this type of scenarios. |
Practical Learning: Using Various Files |
<%@ Language="VBScript" %> <html> <head> <title>Geometry: The Circle</title> </head> <body> <h1>Geometric Figures: The Sphere</h1> <p>Based on the radius you provided, the results of your circle are:</p> <% Function Diameter() Dim Radius Radius = CDbl(Request.QueryString("txtRadius")) Diameter = Radius * 3.14159 End Function Function Circumference Dim Radius Radius = CDbl(Request.QueryString("txtRadius")) Circumference = Radius * 2 * 3.14159 End Function Function Area Dim Radius Radius = CDbl(Request.QueryString("txtRadius")) Area = Radius * 2 * 3.14159 End Function %> <table border="0" width="316"> <tr> <td width="109">Radius:</td> <td width="52"><input type="text" name="txtRadius" size="14" value='<%= Request.QueryString("txtRadius") %>' > </td> <td width="135"></td> </tr> <tr> <td width="109">Diameter:</td> <td width="52"><input type="text" name="txtDiameter" size="14" value='<% =Diameter() %>' > </td> <td width="135"></td> </tr> <tr> <td width="109">Circumference:</td> <td width="52"><input type="text" name="txtCircumference" size="14" value='<% =Circumference() %>' > </td> <td width="135"></td> </tr> <tr> <td width="109">Area:</td> <td width="52"><input type="text" name="txtArea" size="14" value='<% =Area() %>' > </td> <td width="135"></td> </tr> </table> <p><a href="circle2.asp">Return</a></p> </body> </html> |
<html> <head> <title>Geometry: The Circle</title> </head> <body> <h1>Geometric Figures: The Circle</h1> <p>A circle is a geometric figure made of various points with the following rules:</p> <ul> <li>There is a point of this figure called the Center</li> <li>All points that form the circle are positioned at an equal distance from the center</li> </ul> <form action="circleresults2.asp" method="get"> <table border="0" width="316"> <tr> <td width="109">Radius:</td> <td width="52"><input type="text" name="txtRadius" size="14" value="0.00"> </td> <td width="68"><input type="submit" value="Submit" name="btnSubmit"></td> <td width="67"><input type="reset" value="Reset" name="btnReset"></td> </tr> </table> </form> </body> </html> |
Arguments and Parameters |
Introduction |
So far, to use a value in a procedure, we had to declare it. In some cases, a procedure may need an external value in order to carry its assignment. A value that is supplied to a procedure is called an argument. When creating a procedure that will use an external value, declare the argument that represents that value between the parentheses of the procedure. For a sub routine, the formula you use would be: Sub ProcedureName(Argument) End Sub The argument must be declared as a normal variable, omitting only the Dim keyword. Here is an example that creates a function that takes a string as argument: <% Sub ShowFullName(FirstName) End Sub %> A certain procedure can take more than one argument. In this case, in the parentheses of the procedure, separate the arguments with a comma. Here is an example of a sub routine that takes two arguments: <% Sub ShowFullName(FirstName, LastName) Dim FullName FullName = LastName & ", " & FirstName Response.Write(FullName) End Sub %> In the body of a procedure that takes one or more arguments, use the argument(s) as you see fit as if they were locally declared variables. For example, you can involve them with values inside of the procedure. You can also exclusively use the values of the arguments to perform the assignment. |
Calling a Procedure With Argument |
If you or someone else created a procedure you want to use in your code, when calling it, make sure you provide a value for that argument. The way you call this procedure depends on some factors such as whether the procedure is a sub or a function, the number of arguments. To call a sub procedure that takes only one argument, you can type its name followed by space, and followed by the value of the argument. If you have that value, you can provide it. If the value is a number and you have the number already, you can type it after the name of the procedure. If the value is a string, make sure you provide it in double-quotes. Here is an example: <% Sub ShowCountryName(Name) Response.Write(Name) End Sub %> <% ShowCountryName "Emirats Arabes Unis" %> This would produce:
As an alternative, when calling a sub procedure you can precede it with the Call keyword. In this case, the argument(s) must be passed in parentheses. With this technique, the above procedure would be called as follows: <% Sub ShowCountryName(Name) Response.Write(Name) End Sub %> <% Call ShowCountryName("Emirats Arabes Unis") %> You can also pass the name of a variable that holds the string to be passed as argument. Here is an example: <% Sub ShowCountryName(Name) Response.Write(Name) End Sub %> <% Dim CName CName = "Mexico" ShowCountryName CName %> If you are calling a sub procedure that takes more than one argument, when passing the arguments, separate them with a comma. If you know the values to be passed as argument when you call the procedure, provide each value. Here is an example: <% Sub ShowFullName(FirstName, LastName) Dim FullName FullName = LastName & ", " & FirstName Response.Write(FullName) End Sub %> <% ShowFullName "Beltrami", "Ramirez" %> If the values are numbers, provide their values. If you use a mix of values, because VBScript doesn't provide a mechanism to identify the values, it is your responsibility to make sure that each value is properly passed. |
Functions |
Introduction |
Like a sub procedure, a function is used to perform an assignment. The main difference between a sub procedure and a function is that, after carrying its assignment, a function gives back a result. We also say that a function "returns a value". To distinguish both, there is a different syntax you use for a function. |
Practical Learning: Introducing Function |
<%@ Language="VBScript" %> <html> <head> <title>Rectangle</title> </head> <body> <h1>Geometric Figures: The Rectangle</h1> <form method="GET" action="rectangle1.asp"> <table border="0" width="276"> <tr> <td width="79">Length:</td> <td width="105"><input type="text" name="txtLength" size="12" value = <%=Request.QueryString("txtLength")%> ></td> <td width="72"></td> </tr> <tr> <td width="79">Height:</td> <td width="105"><input type="text" name="txtHeight" size="12" value = <%=Request.QueryString("txtLength")%> ></td> <td width="72"><input type="submit" value="Submit" name="B1"></td> </tr> <tr> <td width="79">Perimeter:</td> <td width="105"><input type="text" name="txtPerimeter" size="12" value = "0.00" ></td> <td width="72"></td> </tr> <tr> <td width="79">Area:</td> <td width="105"><input type="text" name="txtArea" size="12" value = "0.00" ></td> <td width="72"><input type="reset" value="Reset" name="B2"></td> </tr> </table> </form> </body> </html> |
Function Creation |
To create a function, you use the Function keyword followed by a name and parentheses. Based on this, the minimum formula used to create a function is: Function FunctionName() End Function The name of a function follows the same rules and suggestions we reviewed for sub procedures. As mentioned already, the section between the Function and the End Function lines is the body of the function. It is used to describe what the function does. As done on a sub procedure, one of the actions you can perform in a function is to declare a (local) variable and use it as you see fit. Here is an example: <% Function GetFullName Dim FirstName FirstName = "Paul" End Function %> After performing an assignment in a function, to indicate the value it returns, somewhere after the assignment and before the End Function line, type the name of the function, followed by the assignment operator "=" , followed by the value the function returns. Here is an example: <% Function GetFullName Dim FirstName Dim LastName FirstName = "Paul" LastName = "Motto" GetFullName = LastName & ", " & FirstName End Function %> You can also use some local variables in the function to perform an assignment and then assign their result to the name of the function. Here is an example: <% Function GetFullName Dim FirstName Dim LastName Dim FullName FirstName = "Paul" LastName = "Motto" FullName = LastName & ", " & FirstName GetFullName = FullName End Function %> |
Practical Learning: Creating a Function |
|
Calling a Function |
As done for the sub procedure, in order to use a function in your program, you must call it. Like a sub procedure, to call a function, you can simply type its name in the desired section of the program. Here is an example: <% Function GetFullName Dim FirstName Dim LastName Dim FullName FirstName = "Paul" LastName = "Motto" FullName = LastName & ", " & FirstName GetFullName = FullName End Function %> <% Response.Write("Person Name: ") %> <% =GetFullName %> Since the primary purpose of a function is to return a value, to better take advantage of such a value, you can assign the name of a function to a variable or to the value attribute of a web control in the section where you are calling the function. |
Practical Learning: Calling a Function |
|
A Function With Arguments |
Like a sub procedure, a function can also take one or more arguments. When creating such a function, provide the argument(s) the same way we introduced for the sub procedure. Here is an example: <% Function SquareArea(Side) SquareArea = Side * Side End Function %> To call a function that takes an argument, you must pass the argument between parentheses. Here is how the above function would be called: <% Function SquareArea(Side) SquareArea = Side * Side End Function %> <% Response.Write SquareArea(42.58) %> Just like a sub procedure, a function can take more than one argument. When calling such a function, remember to provide a value for each argument. |
|
||
Previous | Copyright © 2005 FunctionX, Inc. | |
|