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

residential cleaning services Lincolnshire ..
In The News:

House Bill 469 would prevent AI systems from owning property, serving as executives, or gaining legal personhood in Ohio under Representative Thaddeus Claggett's proposal.
Public voter records expose retirees' personal details to election scammers who create targeted cons using names, addresses, and voting history data.
Instead of fearing what comes next with artificial intelligence, think outside the box. Here are high-earning AI jobs that don't require a computer science degree.
OpenAI CEO Sam Altman says polite words like "please" and "thank you" cost millions annually, while direct prompts may improve ChatGPT accuracy by several points.
Chattee Chat and GiMe Chat exposed intimate conversations and photos, revealing users spent up to $18,000 on AI companions before the breach.
New Instagram parental controls allow families to manage teen screen time and content limits through the Family Center with stricter safety settings.
Third-party security breach at Discord exposes sensitive user information including government IDs, highlighting cybersecurity risks from external service providers.
Survey of 1,000 students shows teens using AI for personal relationships while two-thirds of parents remain unaware of their children's AI usage.
Cybersecurity experts warn about a ShadowLeak vulnerability that weaponized ChatGPT's Deep Research agent to steal personal data from Gmail accounts through hidden commands.
Tesla's Full Self-Driving system faces federal investigation following 58 reports of crashes, with six vehicles running red lights before colliding with other cars.
The Fox News AI Newsletter brings you the latest developments on artificial intelligence, with news on OpenAI moving to soon allow erotica for adult users.
Eric Schmidt alerts that hackers can reverse-engineer AI models to bypass safety measures, citing examples like the jailbroken ChatGPT variant called DAN.
Cybercriminals exploit Microsoft Teams through impersonation, malicious links and fake profiles to gather intel and deliver ransomware to personal and work devices.
Google, Dior, Allianz and dozens of other companies lost sensitive customer data in Salesforce-related breaches affecting millions of records across multiple sectors.
Apple launches iOS 26 with new Preview app that combines document editing, PDF annotation and scanning features into one streamlined iPhone experience.
New AI road monitoring system uses sensor-embedded fabric to predict infrastructure problems, potentially reducing maintenance costs and traffic disruptions for cities.
Holiday charity scams target retirees through lookalike organization names, untraceable payment requests, and data broker information to steal donations.
The Federal Trade Commission says criminals are posing as IRS agents, law enforcement officers or other officials, often over the phone or online, to steal thousands of dollars at a time.
AI phishing scams now use voice cloning and deepfake technology to trick victims, but Kurt "CyberGuy" Knutsson reveals warning signs to watch for.
Inversion Space unveils Arc, a reusable reentry vehicle that can deliver up to 500 pounds of cargo from orbit to anywhere on Earth in under an hour.
Red flags like processing fees, urgent countdowns and requests for full Social Security numbers expose fraudulent settlement sites targeting consumers.
Comprehensive analysis of Google Maps, Waze and Apple Maps examines usability, routing accuracy, data handling and features across the top navigation platforms.
Expert analysis reveals whether wired Ethernet or wireless Wi-Fi connections are safer for home internet use, plus practical steps to secure your network from attackers.
Australian construction robot Charlotte uses sand, crushed brick and recycled glass to 3D print fireproof, floodproof homes with reduced carbon footprint.
Cybercriminals are using fake invitation emails to trick recipients into downloading malware and stealing personal information and data.

PHP & Account Activation

When a user signs up at your website, you may... Read More

Server Stats ? Analyzing Traffic To Your Site

Analyzing traffic to a site is a key factor in... Read More

Ten Ways NOT to Set Up a Website-based Service Business

1. As the first step, "Hire a web designer."First create... Read More

How Improving Your Sites Usability Can Increase Your Buyers by 40% and Boost Each Order By 10%!

With all of the factors involved in designing and building... Read More

Link Trades That Waste Your Time

Never has competition been so difficult in the Internet world.... Read More

Launch Your Own Website Today - Its Easier Than You Think!

Thousands of people are now making a living online and... Read More

1 Simple Solution to All Internet Marketers about Their Website Design Needs

It is an undeniable fact that not everybody is keen... Read More

Why Is No-one Buying From My Website?

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

Increase Hits to Help Increase Your Business

In this day and age most businesses consider having their... Read More

12 Tips To Great Websites

Why do some sites succeed while the vast majority of... Read More

Ever Wondered What Challenges Other People With Their Web Site?

Have you ever wondered what challenges are faced by other... Read More

How Worldly is Your World Wide Web?

The growth in Internet usage around the world is astounding... Read More

Creating a Sticky Web Site

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

5 Essential Traits Of A Home Business Website

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

The Importance of Website Stats to You

One of the best tools you have as a webmaster... Read More

Introductory PHP For Non-Programmers

When you started reading this sentence you assumed lo acabar?a... Read More

CSS - Maximum Benefits

What is CSS?CSS is a simple file which controls the... Read More

Flash Deadly Sins (That Can Kill Your Web Business)

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

Writing Effective ALT Text For Images

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

Maximizing ROI via Web Site Traffic Analysis

We are clearly well past the innocent "golden age" of... Read More

Selecting a Web Content Management Product

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

What Hosting Companies Dont Tell You, Could Hurt You?

Did you know that hosting companies overcrowd their servers despite... Read More

Planning A Usable Website: A Three-step Guide

A website is like an information flow, with you as... Read More

50 Ways To Use Your Website

A website is the most versatile and cost-effective marketing tool... Read More

Achieving Differentiation With Your Website

Internet Marketing is not a miracle marketing strategy ? competition... Read More

best cleaning company Highland Park ..