Functions and Subroutines in ASP


As developers, we should endeavour to make our lives easier wherever 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 another way of encapsulating code, but have a lot more functionality than just 'saving some code for later'.

First, let's look at Functions... Imagine a balloon salesman in the street. We've all seen them they require one piece of information when you buy a balloon, the colour.
Let say we asked for a red balloon... The balloon salesman armed with this 'information' then does a pretty basic action... he hands you the balloon. The balloon you received is a direct result of the information you gave the balloon seller.

Functions are just the same... they return to you a value based on the information you provided. Lets look at an example 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 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
%>

A Function is passed 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.

Let us look at one more example: -

<%
Function calcTax(amount, taxrate)
Dim Tempvar
Tempvar = amount * (taxrate / 100)
CalcTax = Round(Tempvar, 2) 'round the result to 2 decimal places
End Function
%>

Again, another basic example. We should notice this time that the Function 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 amount = £" & calcTax(goodsTotal)
Response.Write "Total Amount Due = £" & shoppingbill
%>

Above you see the example function in action... easy huh!

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

I will use only one example of a Sub, and in the same example 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 something in, it performs an action (in this case writing to the screen), but 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.

Well, that just about concludes this article. We should by now be writing efficient code with the use of Functions and Subs. Don?t forget that if you use your functions and subs in multiple pages then you should really store them within include files for reasons of easy maintenance and better performance.


More Resources

Unable to open RSS Feed $XMLfilename with error HTTP ERROR: 404, exiting

More Web Development Information:

Related Articles


Nonsense! Why Do I Need a Website?
"I don't sell anything online, I don't advertise online, in fact I don't really even know how to use a computer..
Website Sales: 10 Reasons Why People Dont Buy From You
You've put up a website to promote a product orservice.You've spent piles of cash to generate traffic.
Developing A Website: 10 Tips For Getting Started
1. Be clear on your purpose.
Top 7 Reasons Why Your Business Needs a Website
A website provides invaluable advantages for businesses who have one.7 reasons why your business needs a website are:1.
High Quality Website at Low Cost
Although designing a great web site is an important part of developing a successful online business, it is often becomes a problem for newcomers to the internet business.The vulgar problem is the lack of money.
Five Steps to Create Your Software Product with Outsourcing
Many executives and investors are skeptical that software products can be developed using outsourcing. Even more skepticism is expressed at the idea of outsourcing version 1.
The Problem with Paypal On Your Web Page
When I set up my website I felt paypal was a very simple and easy solution when people wished to purchase my products.For 6 months I was getting 150-200 unique visitors a day, and yet only a few sales every week.
Get to Know People through Websites
Web sites are not just places to sell things; they are a medium where people can get insight into other people's lives. Take the musician Xavier Rudd for example.
Do Authors Need a Website?
"I am a writer, not a business-person. I don't want to have anything at all to do with business," you might think.
Earning From Your Website
Before you start building your site, ask yourself "WHY?". Why did you decide to build your own web site.
Making Your Website Shine! Attract and Keep Visitors at Your Site!
When you consider the billions of individuals and companies on the net who are attempting to influence visitors with their wares, it is vitally important that your site offers something out of the ordinary. An important fact to remember is that what the site says is far more important than how it looks.
How Worldly is Your World Wide Web?
The growth in Internet usage around the world is astounding - 146% in the five years prior to March 2005. And it's not just European and North American users on the Net.
How Web Templates Are Helping Online Businesses to MultiplyTheir Income
Web templates by nature are created to aid and ease thestrenuous process of a typical web development phase. Ideacollection, analysis, research, design are just a small partof a bigger picture.
Ecommerce for Beginners
Like most average persons, I wanted some extra money to supplement my regular income. I had considered a part time second job at times, but hated that option because it would take me away from the family, make me more tired and probably just pay minimum wage.
Art, Artists and the Web: Part 4--What to Do After a Website is Designed
What to do if you are an artist after you finish your website.Unfortunately, just because you've created a website, does not mean that anyone will visit it.
Unlocking the Power of Your Website!
Why do some web sites reap huge benefits while others just get lost in the shuffle? Do some web masters have a secret that most of us have overlooked? Actually, there is a secret to unlocking the power of your web site and it has to do with words. When communicating via the web, words truly make all the difference.
Building a Great Intranet Taxonomy
While it may seem like a "quick and easy" task, defining the navigation structure and organization of your intranet site will be one of the most challenging tasks you will face in the course of your project. It is also one of the most critical success factors.
Free Webmaster Tools - 7 Things Every Webmaster Needs in Their Toolbox
Webmaster tools are vital to becoming both efficient and effective in your online efforts. The following are my picks for the top types of webmaster tools:Tool #1 Link FindersEvery webmaster needs this free tool.
I Want To Be A Freelancer
So you have decided that you want to do freelance work. You have done your homework and have developed your skills in HTML, PHP, CMS, and a slew of other applications.
4 Steps to Selling More With Your Web Site
Most web sites are more like stage sets than real buildings; they make a good impression and they look substantial at first, but when you open a door, you end up back stage in the dark. They don't generate large numbers of leads, help build relationships with prospects or generate the desired volume of sales.