![]() | |||||||||||||||||||||
![]() |
|
|
Where's the FUNction?
Of course, one of the major strengths of any language is the ability to create functions for tasks and then call on those functions whenever necessary. In Visual Basic Script a function is declared by simply calling "Function" a name and any variable names that will get passed into the function. g=totalamount(1,5) Function TotalAmount(x,y) TotalAmount=x+y End Function In this function the values x and y are passed into the function, added and then final result is passed back to the original call. This was an example of a function that returned a value. Functions can just as easily process a few lines and then return without sending back any results.
Call WriteThis
Function WriteThis
Response.write("Hello!")
Response.write("This is a function!")
End Function
The function "WriteThis" was called using the Call statement, it processed the two response.write statements and then returned. No variable was needed since it did not need any values from outside the function. This is not SUB Par! Subs are very close to Functions with one major exception, Subs cannot return any values. This means values can be sent to them, they can process statements within them, they can call other functions or subs, but they cannot send back any results. Remember that "g=totalamount(1,5)" that called the totalamount function and made the variable "g" equal to the result. That cannot occur with Subs. An error would occur. Subs can only be called by using the Call Statement, like the second function example previously.
Call WhatColorisThis(8)
Sub WhatColorisThis(x)
Select Case x
Case 2
Response.write("Yellow")
Case 4
Response.write("Blue")
Case 6
Response.write("Red")
Case 8
Response.write("Green")
End Sub
By calling this sub the response of "Green" is outputted to the page and the rest of the processing of the Visual Basic Script occurs. I am ready to write a script! Now that we have gone over the basics we can write a full script. We will create a page and designate it default.asp. Create the file in the c:\inetpub\wwwroot\ directory. Open the file using any text editor, my editor of choice is notepad because of the fact it is installed on every Microsoft Windows machine in the world. In this file place the following code:
<%
@Language=VBScript
Option Explicit
Dim FinalResult
Dim Number1
Dim Number2
FinalResult=NumberstoMultiply(5,6)
Function NumberstoMultiply(Number1,Number2)
NumberstoMultiply=5*6
End Function
Response.write ("Your Final Result is " & FinalResult & ".")
%>
Save the file and then open a web browser on the server and go to the URL: http://localhost/default.asp, or replace localhost with the ip address or name of the server. This should process your page. With any luck the output of the page would be: Your Final Result is 30. As you can see from the previous example, the response.write method outputted a string and the variable result followed by. This is done by using the apersand (&) in between the string declaration and variable. I want more! This example is simplistic example of what we can accomplish using ASP. With our arsenal of statements and loops that we discussed we can begin to create much more complex ASP scripts that will make our web lives easier. We can pull data from a database, perform computations, even pull data from other websites. Feel free to try out each loop, it takes practice and experience to learn what loop and statement needs to be used where. ASP is designed to be easy and robust at the same time. For more information on displaying data from a database in ASP check out: An Overview in Displaying Information from a Database on the Web.
|
Section 2: Loop-the-Loop Section 3: Make a Statement Section 4: Final Results
| ||||||||||||||||||||||