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 tutorial on include files (ssi) - Copyright 2003-2004

best cleaning company Deerfield ...
In The News:

Over 5,400 legitimate websites now serve fake CAPTCHA scams that trick users into pasting malware commands into Windows Run via ClickFix attacks.
Tuya Smart's Doova AI home robot rolls to seniors who call for help and connects family via live video, but key privacy questions remain unanswered.
Jevon Martinez livestreamed himself removing 10 Flock Safety cameras in Bernalillo County, but authorities say eight belonged to neighborhood associations.
A dark web service called Nexus claimed to hold over 153 million driver's license records, and the FBI confirms it is now looking into the incident.
Asking Siri to call American Express connected an 82-year-old woman to a bogus number that nearly drained $100,000 from her Bank of America account.
The Fox News AI Newsletter covers the latest artificial intelligence technology advancements, including the challenges and opportunities AI presents now and for the future.
iPhone Duo opens to a 7.6-inch foldable display starting at $1,999, but Siri Recap's ambient listening on Apple Watch may spark privacy concerns.
Homeowners association meeting minutes and membership lists can expose names, addresses and emails that scammers use to craft convincing phishing scams.
Lima, Ohio, has installed 3,120 floating solar panels on its Twin Lakes Reservoir, projecting nearly $10 million in electricity cost savings.
Amazon's Alexa for Shopping now lets customers verify suspicious emails, texts and phone calls against the company's own communication records.
Lindsey Brooke Isaacs spent 13 days in jail after a Flock camera flagged her Dodge Durango near a fatal Florida crash that killed three people.
AI-powered smart grocery carts are linked to 32% higher spending, according to new research, as Instacart Caper Carts expand across U.S. stores.
A VPN encrypts your connection on hotel, airport and café Wi-Fi abroad, but streaming catalogs and live sports access still depend on provider rules.
Gmail's Verified Sender Program launches September 8, 2026, giving U.S. political committees a more reliable path to your personal email inbox.
Elroy Air's Chaparral can carry over 500 pounds and fly 450 miles without a runway, and the FAA just tested it under a new federal program.
Bedrock Robotics says its autonomous excavators are doing paid earthwork on active job sites in Texas and Nevada with no operator in the cab.
A dedicated banking computer isolates your financial accounts from malware and phishing, but passwords, SIM swapping and outdated software remain risks.
The OlloNi SS1 AI robot pet uses a 4K camera and facial recognition to learn your family, but its removable Red Heart memory module raises questions.
Nine identity theft warning signs range from unfamiliar credit inquiries to SIM swap attacks that can quietly drain your bank account funds.
Robot.com's R-dog uses articulated legs and wheels to climb stairs and deliver packages, while its built-in screens display paid ads along the route.
MyChart phishing emails mimic real patient portal alerts with fake lab results and malware tricks that target Windows users across the country.
The Fox News AI Newsletter covers the latest artificial intelligence technology advancements, including the challenges and opportunities AI presents now and for the future.
Google's Motion Assist places animated dots on Pixel screens to combat motion sickness in vehicles, but the Android 17 rollout remains limited.
Moving to a 55+ community can quickly expose your data to scammers and data brokers before you even unpack, putting your whole family at risk.
Amazon Prime Air drone delivery plans to reach nearly 500 U.S. cities by the end of 2026, but your yard and ZIP code may determine eligibility.

7 Ways to Gain a Professional Online Reputation

A professional online reputation is essential to the flourishing of... Read More

Build a Strong Foundation for Creating Your First Online Identity ? Notes for Beginners

If you are looking for creating your online presence you... Read More

Using Mini Websites - Powerful Way to Direct Marketing

Mini Websites are the most powerful and cost effective solution... Read More

HTML Editors 101 - Smaller Is Better

SiteSpinnerSiteSpinner is a user-friendly web site development tool that includes... Read More

Learn What 98% of Targeted Visitors Do On Your Website

Getting a Targeted Visitor to a website is a waste... Read More

How To Make Your Website More Successful? (Part I)

Building a website and getting it online is easy. Driving... Read More

Website Sales: 10 Reasons Why People Dont Buy From You

You've put up a website to promote a product or... Read More

Creating Dynamic Website Content with PHP - MySQL

Fresh website content for your visitors can be of real... Read More

Why Should Your Business Go O.N.L.I.N.E.

Optimize your time. How much time each day must you... Read More

High Quality Website at Low Cost

Although designing a great web site is an important part... Read More

Content Management ? The New Standard in Web Design

The evolution of web development parallels the model of technology... Read More

The Two Most Common Mistakes That Scare Away Web Visitors

Can you imagine the hypocrisy of spending thousands of dollars,... Read More

Top 7 Tips for Building an Antique Car Website

Like wine cars get more attractive to collectors as years... Read More

Up The Sandbox!

Go to any internet marketing forum you want these days... Read More

How to Get a Better Web Site ROI

Better Web Site ROIHow to increase your business profitability by... Read More

Establishing Your Web Presence

Part of the success of most home-based businesses is their... Read More

Ten Quick Tests To Check Your Website For Accessibility

The Disability Discrimination Act says that websites must be made... Read More

Are You Losing Sales Because of These 4 Simple Website Mistakes?

Losing website sales and traffic is sometimes simply a case... Read More

Make Your Own Website - General Website Design Tips

Wow, we`ve already taken a domain and chosen a web... Read More

Are You Being Scammed By Your Web Design Company?

This is a growing concern amongst many business owners. Does... Read More

3 Essential Tools Every Website Designer and Marketer Needs

Appealing to others is really important when you have a... Read More

Web accessibility for screen magnifier users

The needs of screen magnifier users are overlooked when implementing... Read More

Traffic for Webmasters

"If you build it, they will come"; is an age... Read More

Linking Out is Good

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

Make Your Communication Come Alive

Design and layout can make your communication come alive and... Read More

professional home cleaners Winnetka ...