Functions and Subroutines in ASP


Functions and Subroutines in ASP

If 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.

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.

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

In The News:

Full Stack Web Developer  Charlotte Agenda
What is Technical SEO?  Business 2 Community

What Works and What not Works for Your Internet Business

"Build it and they will come."--Filed of Dreams (1989) If... Read More

What Does An Ex-Basketball Coach Know About Pulling Web Metrics?

In my previous life.....Actually, my previous JOB life, I was... Read More

Why Your Brick-and-Mortar Biz Needs A Website

The internet has taken over our lives with a vengeance,... Read More

Web Content Strategy 101

Your content is what gets you in search engines, speaks... Read More

Five Question-Five Minute Web Makeover Quiz

If you haven't made the sales and built your clients... Read More

Why Local Service Companies Should Have a Website

The Internet is being used by local service business to... Read More

Linking Out is Good

Many websites I come across don't have a single link... Read More

Five Essential Questions to Answer Before Creating Your First (or second...or third) Website

Right after the thought, "I need a website," people usually... Read More

Wheres Your Focus, Money or Customer?

Is your website Really useful?I had a 'rep' in yesterday... Read More

You Need a Website!

No matter the size of your business, having a web... Read More

Freelance Programming is Easy to Manage

There are several reverse bid freelance sites out there. Beyond... Read More

How Improving Your Sites Usability Can Increase Your Buyers by 40% and Boost Each Order By 10%!

With all of the factors involved in designing and building... Read More

6 Steps To Your Own Website

A lot of people these days are interested in getting... Read More

Warning: The Truth About Having a Web Site!

If you don't have a web site yet, are thinking... Read More

From Concept to Website

You've decided to create a website to market your products... Read More

Why do I Need a Website?

Cost Effective Advertising. Online selling. Increased geopgraphic range. Wider... Read More

Web Success Secret - Keep Your Visitors Interested

Visitors to your site come looking for information or solutions... Read More

Web Site Optimization: How To Speed Up Your Web Site By Minimizing Your GIF and PNG Image Bit-Depth

Are bloated images slowing down your web site and causing... Read More

Setting Up Your First Website

Q. Hey, Cathy: I'm just setting up a website. What... Read More

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... Read More

PHP & Account Activation

When a user signs up at your website, you may... Read More

A Web Site for Your Small Business: How to Set Up a Website

Your web site will be your window to the world.... Read More

Search Engines and Customers Want Focused Web Site Content

How do you decide on the content, products and or... Read More

For Newer Sites, Web Marketing Depends On Titles & Links!

We all start out the same. Some have budgets, some... Read More

How To Get Profits From Your 404 Page Not Found File

"'Page Not Found' on this Server. Check the URL and... Read More