ASP Sub Procedures---ASP Function Procedures

来源:百度文库 编辑:神马文学网 时间:2024/04/28 04:04:12
ASP Sub Procedures
ASP Sub Procedures are a collection of ASP statements that perform a task, and are executed by an event procedure. Event Procedures are any clickable objects, or onload event. Sub procedures do not return a value, but executes it‘s content on "call".
This is an asp sub procedure that is used to write information stored in variables:
<%
Sub GetInfo()
dim name,telephone,fee
name="Mr. John Doe"
telephone="555-5555"
fee=20
Response.write("Name: "& name &"
")
Response.write("Telephone: "& telephone &"
")
Response.write("Fee: "& fee &"
")
End Sub
GetInfo()
%> This example simply declares, populates, & writes three variables in the asp sub procedure, ‘GetInfo‘. This sub is executed right after the end sub.
Here is the execution result:
Name: Mr. John Doe
Telephone: 555-5555
Fee: 20
You can pass an argument to the asp sub procedure, and provide the value(s) when calling it.
This is an asp sub procedure that accepts an argument:
<%
Sub circle(r)
dim pi,area,diameter,circum
pi=3.14
area=pi*r^2
diameter=2*r
circum=2*pi*r
Response.write("The Circle with a radius of: "&r&" has the area of: "&area)
Response.write(", the diameter of: "&diameter&" and a circumference of: "&circum)
End Sub
%>
Here is the execution result:
The Circle with a radius of: 2 has the area of: 12.56, the diameter of: 4 and a circumference of: 12.56
ASP Function Procedures
ASP Function Procedures are a series of VBscript statements enclosed by the ‘Function‘, and ‘End Function‘ statements. Function procedures are similar to a ‘Sub procedure‘, but can also return a value. An asp function procedure can accept arguments (constants, variables, or expressions) that are passed to it by calling a procedure).
This is a none-parameterized asp function procedure:
<%
function userName()
userName="MaryLou"
end function
%>
This function procedure remembers a username. You can use this function any number of times in your program, and you can change it once to maintain it- use ‘userName()‘ to call this function. For example: response.write("The username is: "&userName())
This is a parameterized asp function procedure:
<%
Function total(price)
dim tax
tax=price*.09
total=price+tax
end Function
%>
The price value is provided when calling the function- Example: response.write("The total price is: " & total(50))
This is an asp function procedure that accepts an argument:
<%
Function profit(sellPrice, cost)
dim pr
profit=sellPrice-cost
End Function
dim currProfit currProfit=profit(1280,890) Response.write("Profit: $"&currProfit)
%>
Here is the execution result:
Profit: $390