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

Batchtown Chicago prom limo .. Lockport Chicago limo O’Hare
In The News:

A virtual private network can help ensure your information remains security and your privacy remains intact. Kurt the CyberGuy explains.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
Artificial intelligence-based cameras are giving air defense operators unprecedented capabilities in monitoring and protecting airspace.
Apple's iOS 18.1 Inactivity Reboot automatically reboots your iPhone if it hasn't been used or unlocked for more than three days, providing better data protection.
An inventor designed rooftop solar panels for a Tesla that draws solar energy while the car is parked, adding travel mileage without plugging in.
There are currently no laws governing what artificial intelligence can and cannot do with the information it gathers; here are 10 things to avoid telling AI chatbots to keep yourself safe.
A credit union with over 240,000 members recently revealed it was targeted by cybercriminals, resulting in a data breach that was part of a two-month attack by hackers.
Scammers have become skilled at creating convincing fake websites that can easily fool unsuspecting users. The CyberGuy offers tips to protect yourself.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
CAPTCHAs, which are used by websites to confirm whether users are people or bots, are harmless, but hackers are using them to infect PCs with malware.
Hackers recently leaked personal information of about 500,000 Americans and stole patient medical records that included lab results and insurance details.
The holiday season sees a rise in mobile shopping scams. Tech expert Kurt “CyberGuy" Knutsson helps you learn how to stay safe.
Tech expert Kurt “CyberGuy" Knutsson says a VPN enhances online banking security by encrypting data and protecting privacy.
Beware of these six sneaky holiday scams. Tech expert Kurt “CyberGuy" Knutsson gives you tips to avoid falling victim.
Tech expert Kurt “CyberGuy" Knutsson reveals how to securely back up and factory reset your Android to protect your privacy and data.
Artificial intelligence is making life easier for cybercriminals, allowing them to create elaborate scams to trick people. Kurt the Cyberguy explains how to protect yourself.
Cut through all the digital clutter and delete multiple emails from your Android simultaneously. Kurt the CyberGuy explains how it's done.
Tips to prevent your holiday decorations from being stolen
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
Kurt "CyberGuy" Knutsson lays out the immediate steps you should take if your phone has been hacked and your personal information becomes vulnerable.
Fraudsters are sending people bogus invoices through PayPal as part of a sneaky scam that is going around; here's how to protect yourself from being fooled.
A former Colgate-Palmolive employee was shocked to discover $750,000 had been drained from her 401(k) account. "CyberGuy" offers tips on how to prevent identity theft.
Electric vehicle maker Harbinger recently showed its electric delivery truck can handle icy roads with agility and stability in winter.
To make the busiest time of year more manageable, here are some tricks for tracking your packages, taking quality family photos and curating the perfect Christmas playlist.
Kurt "CyberGuy" Knutsson explains how to keep your online Amazon gift purchases a secret from loved ones or friends this holiday season.

Making Your Websites More Compelling

The Internet is a remarkable publishing medium. With just a... Read More

Developing A Website: 10 Tips For Getting Started

1. Be clear on your purpose.Building a website can be... Read More

Web Accessibility: The Basics

What is web accessibility & why is it important?Web accessibility... 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

The Problem with Automated Accessibility Testing Tools

An automated accessibility tool is a piece of software which... Read More

How to Increase Sales with Automation Improvements

How popular is online shopping? And is there room for... Read More

The Internet Road Map

1) Data CaptureOne easy way to collect e-mail addresses and... Read More

Your Website Should Be Selling

It may come as some surprise to a traditionally off-line... Read More

Trinkets, Lockets And Empty Pockets

It never ceases to amaze me how many people set-up... Read More

Web Analytics - Getting it Right

Understanding and using web analytics.In recent years, website marketers were... Read More

Topics to Add When Creating an Alternate and Modern Medicine Web Directory

Alternate medicine finds its own place in the hearts and... Read More

What Business Owners MUST Know BEFORE They Commit To Having A Web Site

Q1.Why would I need a web site? A1.- Improve my... Read More

Establishing Your Web Presence

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

Why A Simple Counter is Never Enough for Your Website

A simple website stats counter is not enough if you... 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

Lead Generation How to -- 10 Tips On Generating More Internet Sales Leads

Many millions of dollars are being wasted on promoting and... Read More

Conceptualize, Build and Publish a Web site

Conceptualize, Build and Publish a Web site - What's required... Read More

Selecting a Web Content Management Product

So you want to take a look at a real... Read More

The Secret Benefit Of Accessibility: Part 2 - A Higher Search Engine Ranking

An additional benefit of website accessibility is an improved performance... Read More

Website Content & Usability

Writing for the web is totally different to writing for... Read More

How To Keep Your Website Sticky

Developing a website with content that entices visitors to return... Read More

Beware the Software Siren

I've heard several prominent web marketers mention in their classes... Read More

Get to Know People through Websites

Web sites are not just places to sell things; they... Read More

Study Your Sites Performance Via Hit Tracking

Knowledge is power.And undoubtedly, it is the key to making... Read More

Guide to Internet Business - Website Setup

After planning the design and content of your website, it's... Read More

shuttle from Midway Munster are ..