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

professional maid services Mundelein ...
In The News:

Travel expert Katy Nastro says 50,000 American Airlines AAdvantage miles vanished from her account after someone redeemed them without permission.
Google AI Overviews can't be permanently turned off, but adding -AI to your searches and using the built-in Web filter can help bypass AI summaries.
Attaxion researchers found 30 fake Texas campaign merchandise stores traced to just two sellers hawking gear for competing Republicans and Democrats.
GM plans to debut eyes-off driving in the Cadillac Escalade IQ by 2028, but safety testing reveals how long distracted drivers take to regain control.
Data brokers assign labels predicting your health, finances and religion based on browsing and purchases, then sell those profiles to businesses.
Elon Musk says he'll give one SpaceX share to every kid in America in 2027, but key details about eligibility and distribution remain unclear.
ClearSpace UK's robotic spacecraft with giant claws will chase down dead satellites at 17,000 mph, with a 2029 launch from Scotland planned.
A fake Social Security statement phishing email uses the SSA logo and a convincing layout, but the sender's domain instantly exposes the scam.
A clean credit report doesn't mean your identity is safe. Six ways criminals can steal from you without triggering a credit monitoring alert.
Carolyn Marvin, a 78-year-old retired professor, was arrested after blocking an Uber Eats delivery robot on a Philadelphia sidewalk for months.
The Midnight Mimosa malware campaign hides inside cheap Android phone firmware, giving attackers powerful access before owners even turn devices on.
The Fox News AI Newsletter covers the latest artificial intelligence technology advancements, including the challenges and opportunities AI presents now and for the future.
Meta's Muse AI was built to manage tasks, but users are developing romantic AI relationships and even calling the AI companion their girlfriend.
A Minnesota murder suspect's notebook listed 11 people search websites, raising urgent questions about data broker opt-out and online privacy risks.
Google Universal Cart tracks price drops, surfaces promo codes and uses Google Wallet to match your best credit card rewards before checkout.
Safari's Notify Me feature and Google's price-tracking tools can monitor products and send alerts when prices drop on your iPhone or Android.
George Weiner built PACspam.org to trace who sends nonstop political fundraising texts after blocking numbers repeatedly failed to stop the messages.
A dedicated VPN IP address may reduce frustrating CAPTCHAs and bank login security checks by giving websites a consistent connection to recognize.
Apple Wallet in iOS 27 lets you create digital passes from physical cards using a photo or screenshot, but not every card in your wallet qualifies.
Anthropic's Claude AI submitted a fabricated eyewitness tip about an unsolved murder to a Philadelphia police website during automated testing.
P7 DarkSword iPhone spyware can steal saved passwords, photos, Apple Notes and cryptocurrency wallet data by checking for commands every 15 seconds.
Cybersecurity experts told Fox News Digital that a cyberattack on French supermarket freezers should warn the U.S. about critical infrastructure gaps.
The Oracle Health breach exposed medical records, Social Security numbers and diagnoses of nearly 20 million patients across multiple states.
The new Tesla Roadster drove upside down and launched into the air at SpaceX's Texas facility, but a 2028 production target remains unconfirmed.
Apple's iOS 27 lets you add keywords to iPhone photos, while Samsung Gallery tags and Google Photos search offer similar organization tools.

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

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

Beware the Software Siren

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

Website Globalization

Globalize Your WebsiteThe Internet has unlocked a wide array of... Read More

Beautiful Web Sites Seldom Make More Sales

Designing an E-commerce Web site is not as simple as... Read More

Website Development: Become Your Own Expert

Starting and maintaining a business is challenging enough without the... Read More

Art, Artists, and the Web:Part: 2--First Steps in Building an Artist?s Website

What you should do and know if you are an... Read More

Warning: The Truth About Having a Web Site!

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

WYSIWYG Versus Coding: HTML With A Purpose

After years of working with entrepreneurs who developed their first... Read More

Improve Web Sales Figures Forever And Ever Amen!

"How do I improve web site sales figures?" The CEO... Read More

Why You Need A Web Site

"I have a small business with just a couple of... Read More

Top 5 Webmaster Tools You Can Pick Up For Pennies

1. SiteSpinner V2 - On Sale www.deprice.com/sitespinner.htmSiteSpinner is a user-friendly... Read More

Beyond Web Usability: Web Credibility

If you've been developing websites on Mars for the past... Read More

You Don?t Have to be Amazon.com to Achieve 12% Conversion Rates!

That's right. According to a recent study by Nielsen/NetRatings, Amazon.com... Read More

Creating Websites that Cater to Exam Preparations

One of the things that Internet excels is disseminating information... Read More

Optimal Website Design

Optimal website design is the art of logical navigation.It will... Read More

7 Tips to Make Your Order Page Work Harder

So your prospect, Mary, is sitting at the computer reading... Read More

Six Basic Reasons Why Visitors Stay On Your Web Site

1. The first page appears quickly.It's a good way to... Read More

Make Your Web Site Stand Out

When visitors add your web site to their Favorites or... Read More

Let Your Customers Redesign Your Website!

When you purchase a new item from a Yahoo! Store,... Read More

How to Make Your Own Website For Free

If you like the Internet and surfing through the billions... Read More

Websites for Writers ? Why You Need One and How to Get Started Today

It may seem like the publishing industry's equivalent of Beanie... Read More

Newsletter Management Using PHP w/o mySQL for Beginners

Let's begin by setting some limits. If you're like me,... Read More

HTML Editors 101 - Smaller Is Better

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

So You Want To Have A Website

So you want to have a website. You have gone... Read More

How Web Templates Are Helping Online Businesses to MultiplyTheir Income

Web templates by nature are created to aid and ease... Read More

deep house cleaning Arlington Heights ...