Web-development informational articles

Functions and subroutines in asp - web-development


Functions and Subroutines in ASP

If you read our Tutorial on Comprise Files (SSI) then you academic how to summarize ASP code inside consist of files and the reimbursement it brings us.

As developers, we ought to endeavour to make our lives easier where possible. . . no one wants to re-invent the wheel after all.

Functions and Subroutines exist to not only save us time, but to bring power to our ASP.

They are just a new way of encapsulating code, but have a lot more functionality than just 'saving some code for later'.

First, let's look at Functions. . . Dream up a distend salesman in the street. We've all seen them they compel one piece of in a row when you buy a balloon, the colour.
Let say we asked for a red balloon. . . The blow up salesman armed with this 'information' then does a attractive basic action. . . he hands you the balloon. The expand you customary is a as the crow flies answer of the in order you gave the expand seller.

Functions are just the same. . . they come again to you a value based on the in rank you provided. Lets look at an illustration Function: -

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 fair 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

A Affair is accepted some information. The in order we pass a Function, is known as an 'argument'. The in sequence we get back from a Do is known as the 'return value'. Even as a Act can have many arguments, it can only have one come again value.

Let us look at one more example: -

Function calcTax(amount, taxrate)
Dim Tempvar
Tempvar = total * (taxrate / 100)
CalcTax = Round(Tempvar, 2) 'round the consequence to 2 decimal places
End Function

Again, a different basic example. We must announcement this time that the Do accepts two arguments.

By now, we have some idea of how to write a Function. How do we use one?
Let me show you now how we can use the calcTax example.

shoppingbill=goodsTotal + calcTax(goodsTotal,17. 5)
Response. Write "Your shopping came to £" & goodsTotal
Response. Write "
VAT total = £" & calcTax(goodsTotal)
Response. Write "Total Quantity Due = £" & shoppingbill

Above you see the case behave in action. . . easy huh!

I have tried to make appreciation Functions as easy as possible. . . Agreement a Subroutine (Sub) is now going to be easy for you. Assume a block of code that performed some directives based on in rank you gave it. . .
Sounds very much like a function, doesn?t it? Well this time, we do not get everything back. A sub does NOT pass back in order it just uses the data we give it for some purpose.

I will use only one case of a Sub, and in the same case in point make use of the sub: -

Sub Bday(strName, intAge)
Response. Write "Happy Birthday " & Name
Response. Write ", You are " & intAge & " years old today"
End Sub

'now, call the sub
bDay "Joe",26

The above Sub, demonstrates my point. We put amazing in, it performs an accomplishment (in this case inscription to the screen), but nil is returned to us in the code. One thing that Especially IS central when using a sub, is that we do not put brackets about the arguments. . . For the reason that we do not have a come again value we do not need brackets and in this case, if we try we will get an error.

Well, that just about concludes this article. We be supposed to by now be journalism cost-effective code with the use of Functions and Subs. Don?t not recall that if you use your functions and subs in numerous pages then you be supposed to actually store them surrounded by add in files for reasons of easy maintenance and beat performance.

Rob Collyer, experienced with 20 years programming acquaintance and site administrator of www. webforumz. com - Copyright 2003-2004


Web Developer - E-Commerce  Charlotte Agenda

Developed by:
home | site map
goldenarticles.net © 2022