![]() |
URLZ.NET Web Development Information |
Back to URLZ.NET Web Directory |
|
|
Functions and Subroutines in ASP
Functions and Subroutines in ASPIf you read our Tutorial on Include Files (SSI) then you learned how to encapsulate ASP code within include files and the benefits it brings us. Function getBalloon(strColour) Dim Tempstr strColour = lcase(strColour) 'This converts the value lowercase. Select Case strColour Case "red" Tempstr = "Here is your red balloon" Case "yellow" Tempstr = "Here is your yellow balloon" Case "green" Tempstr = "Here is your green balloon" Case "blue" Tempstr = "Here is your blue balloon" Case Else Tempstr = "Sorry, we have sold out of that Colour" End Select getBalloon = Tempstr End Function %andgt; A Function is to pass some information. The information we pass a Function, is known as an 'argument'. The information we get back from a Function is known as the 'return value'. Whilst a Function can have many arguments, it can only have one return value. Function calcTax(amount, taxrate) Dim Tempvar Tempvar = amount * (taxrate / 100) CalcTax = Round(Tempvar, 2) 'round the result to 2 decimal places End Function %andgt; Again, another basic example. We should notice this time that the Function accepts two arguments. shoppingbill=goodsTotal + calcTax(goodsTotal,17.5) Response.Write "Your shopping came to ? and goodsTotal Response.Write " VAT amount = ? and calcTax(goodsTotal) Response.Write "Total Amount Due = ? and shoppingbill %andgt; Above you see the example function in action... easy huh! Sub Bday(strName, intAge) Response.Write "Happy Birthday " and Name Response.Write ", You are " and intAge and " years old today" End Sub 'now, call the sub bDay "Joe",26 %andgt; The above Sub, demonstrates my point. We put something in, it performs an action (in this case writing to the screen), however nothing is returned to us in the code. One thing that REALLY IS important when using a sub, is that we do not put brackets around the arguments... Because we do not have a return value we do not need brackets and in this case, if we try we will get an error. Rob Collyer,xperienced with 20 yearsrogramming knowledge andite administratorf www.webforumz.com? Copyright 2003-2004
MORE RESOURCES: XML error: not well-formed (invalid token) at line 20 |