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

tidy up service Glenview ..
In The News:

The Fox News AI Newsletter gives readers the latest AI technology advancements, covering the challenges and opportunities AI presents.
Cybersecurity research shows weak passwords remain a major threat, with simple patterns and number sequences putting millions of accounts at risk.
New Android malware BankBot YNRK silences phones, steals banking data and drains crypto wallets automatically. Learn how this advanced threat works.
FDA approves first human trial for Paradromics' brain-computer interface that could restore speech for paralyzed patients through neural technology.
New phishing platform QRR targets Microsoft 365 users across 1,000 domains in 90 countries. Learn how to spot fake login pages and protect your accounts.
OpenTable now uses AI to track your dining habits and share insights with restaurants. Learn what data they collect and how to protect your privacy.
Google's discontinued Nest thermostats still secretly upload home data to company servers despite losing smart features, raising serious privacy concerns.
New Android malware NGate steals NFC payment codes in real-time, allowing criminals to withdraw cash from ATMs without your card. Learn protection tips.
DoorDash confirms data breach exposing customer names, emails, addresses after social engineering attack. Learn how to protect yourself from scams.
Concerned about Google's AI scanning your Gmail? Learn how to disable Gemini features that access your emails, Drive files and Chat messages for privacy.
Google warns Android users about dangerous fake VPN apps hiding malware that steals passwords, banking details and personal data from phones and tablets.
Apple's digital passport feature lets iPhone users breeze through TSA checkpoints this holiday season using Digital ID technology at 250+ airports.
A new phishing scam targets family photos with fake "Cloud Storage Full" alerts. Criminals steal credit card information through fake sites. Learn protection tips.
South Korean scientists create ultra-thin fabric muscles that turn clothes into robotic assistants, lifting 33 pounds while weighing under half an ounce.
Archer Aviation has acquired Hawthorne Airport for $126M to launch an LA air taxi network ahead of the 2028 Olympics, featuring AI-powered eVTOL operations and next-gen aviation tech.
Stay up to date on the latest AI technology advancements and learn about the challenges and opportunities AI presents now and for the future.
Fake AI apps disguised as "ChatGPT" and "DALLĀ·E" are flooding app stores with dangerous malware that steals data and monitors users without detection.
Fake buyers demand specific vehicle reports from unknown sites to steal credit card information from car sellers, but warning signs can help identify these scams before paying.
Android users can now manage apps across multiple devices more easily with Google Play Store's updated remote uninstall button in the latest update.
NASA's Perseverance rover discovers shiny metallic rock on Mars that could be a meteorite from an ancient asteroid, containing high levels of iron and nickel.
Holiday scams spike during Black Friday and Cyber Monday as criminals exploit your leaked personal data. Learn how to protect yourself from fake stores and phishing.
Commerce Department proposes TP-Link router ban over Chinese security risks. Learn how this potential prohibition could affect your home network and devices.
Hyundai AutoEver America suffered a data breach affecting 2,000 current and former employees, exposing names, Social Security numbers and driver's license information.
Washington court rules automated license plate reader images are public records, even when stored by vendors like Flock Safety, setting precedent for transparency.
Fake airline texts claiming flight cancellations are targeting travelers with convincing scams. Learn how cybercriminals steal personal data and credit card info.

Marketing Your Business Online

As a business you can't afford to ignore the Internet... Read More

35% Revenue Increase? from Your Website!

2 Golden Rules for an Engaging Website35% of visitors fail... Read More

Why Your Brick-and-Mortar Biz Needs A Website

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

Growing Your Meetings In CyberSpace

As increasing numbers of people search for information on the... Read More

Making the Most of Your Website: Start with the Basics

One of my most salient memories from when I sold... Read More

Beware the Software Siren

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

Entangled in the World Wide Web

I am just a learner who likes to delve in... Read More

Successful Web Development: 10 Key Elements

There are many elements that are key to successful web... Read More

Why Do I Need A Web Site?

Even though the Internet has been around for a long... Read More

Unlocking the Power of Your Website!

Why do some web sites reap huge benefits while others... Read More

Build It and They Will Come

Build it and they will come is not always true;... 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

Nonsense! Why Do I Need a Website?

"I don't sell anything online, I don't advertise online, in... Read More

Web Site Promotion: How To Advertise Your Website For Profits

Making money with your web site is the dream and... Read More

Maximizing ROI via Web Site Traffic Analysis

We are clearly well past the innocent "golden age" of... 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

How To Convert More Sales On Your Website

One of the biggest mistakes that most online retailers make... Read More

Stop Losing Precious Web Site Traffic to the Dreaded World Wide Web Black Hole

You work hard to build traffic to your web page.... Read More

How to Get a Knock Out Website!

The first step in setting up an online business...Starting an... Read More

Is Your Website Innovative? Increase Your Business Over Night!

While studying online for IT investment opportunities, I found that... Read More

Three Things Every Website Should Do

When I started my company in 2002, I knew I... Read More

Web Site Development Process - The Life-cycle Steps

A system development process can follow a number of standard... Read More

The Dos and Donts of Launching a Small Business Website

Launching a new small business website is often a long... Read More

The 5 Biggest Mistakes Almost All Web Designers Make

Huge Mistake #1: Creating a Website with Flash -- Did... Read More

How to Maximize the Value of Every Visitor to Your Website

While most marketers are beginning to understand the critical importance... Read More

full-service cleaning Mundelein ..