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

Northbrook Cadillac Deville rentals .. Lockport Chicago limo O’Hare
In The News:

Kurt "CyberGuy" Knutsson explains how you can creatively repurpose your old unused Android devices, which are full of potential and hidden value.
Mech the super-humanoid robot can lift up to 132 pounds and is designed to tackle stressful and repetitive tasks that often lead to workplace injuries.
Double-clicking is something we all do, often without giving it a second thought. But it could be giving hackers permission to do something dangerous.
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 the CyberGuy offers some of his easy expert tips to free up storage space on your Android if your device is running short on storage.
You can reset your internet router remotely if you're not home when your power goes out. Kurt "CyberGuy" Knutsson explains how this can be done.
The EO Canopy is a self-sustaining, solar-powered camping platform designed to provide all the comforts of home while completely off the grid.
Experts say hackers who used to focus on Windows operating systems are increasingly targeting Apple IDs as part of a new phishing campaign.
Unitree, a Chinese robotics company that developed a backflipping robot, has now introduced a humanoid robot capable of doing a side flip.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
According to the FBI, cybercriminals are sending extortion letters, threatening to release victims' sensitive information unless a ransom is paid.
FireSat is a satellite project designed to detect and track wildfires early, aiming to detect a fire that's the size of a classroom within 20 minutes.
Tech expert Kurt “CyberGuy" Knutsson reports that researchers have uncovered a Chrome vulnerability used in a cyber espionage campaign.
Preserving voicemails securely on Android: Tech expert Kurt “CyberGuy" Knutsson reveals easy methods to keep memories alive forever.
This phishing kit bypasses 2FA via session hijacking and real-time credential theft. Kurt “CyberGuy" Knutsson offers four ways to stay safe from Astaroth phishing attacks.
Tech expert Kurt “CyberGuy" Knutsson discusses Joby Aviation and Virgin Atlantic planning to launch 200-mph U.K. air taxis linking airports and cities.
Fake Apple virus warnings, security alerts and messages are tactics used to prompt you to call a number or click on a malicious link. The CyberGuy explains how to protect your devices.
The combination of artificial intelligence and neuroscience allows a paralyzed man to manipulate a robotic arm by using his brain to imagine movements.
With the help of artificial intelligence, sophisticated fraud techniques emerging today are virtually undetectable to the untrained eye.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents
If you need to free up space on your Mac, consider deleting duplicate photos in your image library. Apple makes it easy to do, and the CyberGuy explains the process.
Microsoft is discontinuing Skype in May after 22 years of service. Kurt the CyberGuy addresses other options for internet-based phone and video service.
Tron 1, a Chinese company's two-legged robot, is versatile and can walk, roll and pivot, even on rough terrain. Tron 1 stands 33 inches tall and weighs 44 pounds.
Hackers are tricking people into installing password-stealing malware by making them press keyboard shortcuts under the pretense of proving they're not bots.
Saving the voices of loved ones can be a comforting way to keep alive memories. Kurt "the CyberGuy" Knutsson explains how to preserve voicemail messages.

Remember This When Building A New Site - Beginners Guide

I recently helped my mom to launch a website (www.mom2me.com)... Read More

Creating a Sticky Web Site

When used to describe a web site, the term "sticky"... Read More

Website Strategy!

A website lets you put your products in front of... Read More

Writing Effective ALT Text For Images

Anyone who knows anything about web accessibility knows that images... Read More

You Dont Know Which One

I still remember it very clear ... It was almost... Read More

Setting Up Your First Website

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

Profitable Websites for Exclusive Industries

In the region where my company is located, South Bend,... Read More

Business Website Building Do?s and Don?ts

First, using a lot of web graphics, flash, banners and... Read More

5 Essential Traits Of A Home Business Website

So you've decided to start an online work at home... Read More

Improve Conversion Rates ? Effective Content

Your site is fast and getting traffic, but conversion rates... Read More

Product Promoting: Getting The Response

When you publish a content site there are times when... Read More

You Need To Treat Your Website Like An Employee - Heres 5 Ways To Do It

People often come to me in a state of crisis... Read More

Earn On Offshore Web Design and Development

If you have some experience in web site design and... Read More

Flash Deadly Sins (That Can Kill Your Web Business)

Looks like every client wants a Flash site these days... Read More

Why You Really Need Your Own Web Page In-order To Make Online Business!

How many Web Sites did you stumble over telling you... Read More

Track Your Visitors, Using PHP

There are many different traffic analysis tools, ranging from simple... Read More

Getting One-way Inbound Links: the 5 Major Strategies

With search engines putting a damper on direct reciprocal links,... Read More

Building eCommerce Websites that Work - Part 2

Succeeding with an eCommerce website is a dream for many... Read More

Get to Know People through Websites

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

Ecommerce for Beginners

Like most average persons, I wanted some extra money to... Read More

Selecting a Web Content Management Product

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

Planning Your Website for Success

Most people know they need a website in order to... Read More

The Road to Better Results

A lot has changed in the way sites are optimized... Read More

10 Tips For A Successful Website

Have you ever thought the secret behind the success of... Read More

Why Is No-one Buying From My Website?

Are you, like many other website owners, frustrated at the... Read More

Beason Mercedes Benz s500 rentals ..