Subroutines and Functions
We can change the flow sequence by using Subroutines and or Functions.
We call a subroutine, which does its processing and then returns
and we continue executing the statement after the subroutine call.
The following example using two functions to calculate hotel, car
and meal costs based on number of days on the trip.
FunctionForm.asp
<htmL>
<head>
<title> Function form</title>
</head>
<body>
<h2> cost calculator for <br>
Visiting Disney World Trip</h2>
Please provide the following information so we can estimate your
costs:
<form action="FunctionAction.asp" method=post>
Please type your preference Hotel in location, either city or
suburb:
<input type=text name="location"><br>
<input type=submit>
<input type=reset>
</body>
</html>
FunctionAction.asp
<html>
<head>
<title>Function Action</title>
</head>
<body>
<%
Function CityCost(NumberDays)
varHotelTotal=NumberDays*175
varMealsTotal=NumberDays*75
varAirportTransport=85
CityCost=varHotelTotal+varMealsTotal+varAirportTransport
end function
sub Suburbcost(numberDays)
varHotelTotal=NumberDays*85
varMealsTotal=NumberDays*45
cost=varHotelTotal+varMealsTotal
response.write "The "+ numberDasy+" day trip will
cost $" & cost & "<br>"
end sub
varLocation=request.form("location")
%>
<h3> you hvave chosen the hottel in <%=varLocation%> <br>
Your estimated costs for this trip will be </h3>
<%
select case varLocation
case "city","City", "CITY"
varCost=CityCost(2)
response.write "The two day trip will cost $" & varCost &"<br>"
varCost=CityCost(4)
response.write "The four day trip will cost $" & varCost & "<br>"
varCost=CityCost(6)
response.write "The six day trip will cost $" & varCost & "<br>"
case "suburb","Suburb","SUBURB"
suburbcost(2)
suburbcost(4)
suburbcost(6)
case else
Response.write "input Error="+varLocation
end select
%>
</body>
</html>
|