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

family-safe home cleaners Lincolnshire ..
In The News:

The Fox News AI Newsletter brings you the latest news on AI technology advancements and the challenges and opportunities AI presents now and for the future.
Texas family reunites with missing 11-year-old cat Grayson after 103 days using Petco Love Lost's AI photo matching technology and community help.
Tired of AI customer service loops? These insider tricks help you escape "frustration AI" and get real human help when you need it most for urgent issues.
Unlock richer audio from your streaming apps with simple tweaks to volume normalization, equalizer settings, and quality preferences for cleaner sound.
Scammers are sending fake Facebook settlement payout emails that mimic legitimate notices from the privacy settlement administrator to deceive users.
Holiday shopping scams surge as fake refund emails target distracted consumers during Black Friday and holiday seasons, costing Americans billions annually.
The AI-powered IRMO M1 exoskeleton features four modes, including turbo, eco, training and rest for hiking, running, cycling and sports with eight-hour battery life.
OpenAI announced upgrades for its ChatGPT Images platform on Tuesday, saying the program can now make more precise edits and produce images more quickly.
Chrome for Android now turns web articles into AI-powered podcast conversations. Get hands-free browsing with Google Gemini's natural audio summaries.
LastPass faces $1.6 million fine from U.K. regulators after 2022 data breach exposed 1.6 million users. Password manager failed proper security controls.
Petco disclosed a data breach exposing customer Social Security numbers, financial account details, and driver's license information due to a software error.
Baseball teams can now analyze complete swing mechanics in normal training environments using Theia's markerless AI system that processes standard high-speed footage.
Smart home hacking fears overblown? Expert reveals real cybersecurity risks and simple protection tips to keep your connected devices safe from hackers.
MIT develops needle-free glucose monitor using light technology. Revolutionary device could replace painful finger pricks for diabetes management.
The ClickFix campaign disguises malware as legitimate Windows updates, using steganography to hide shellcode in PNG files and bypass security detection systems.
Researchers from Osaka Metropolitan University designed a 21-foot dome that combines aquaculture and hydroponics to create a self-sustaining urban food system.
The Fox News AI Newsletter gives readers the latest AI technology advancements, covering the challenges and opportunities AI presents.
ChatGPT data breach exposes personal info of users through partner Mixpanel. OpenAI confirms names, emails compromised in security incident.
Android rolls out Emergency Live Video for 911 calls, letting dispatchers see real-time scenes during emergencies. Great for holiday travel safety.
Malicious Chrome and Edge extensions collected browsing history, keystrokes and personal data from millions of users before Google and Microsoft removed them.
Google's new Call Reason feature lets Android users mark calls as urgent before dialing, displaying an urgent label to recipients using Phone by Google app.
Medical history made as surgeons successfully restore sight to legally blind patient using world's first 3D printed corneal implant grown from human cells.
Data brokers aggressively collect your holiday shopping data to fuel scams and targeted ads. Learn how to delete your digital profile before 2025 starts.
Scammers are sending fake MetaMask wallet verification emails using official branding to steal crypto information through phishing links and fraudulent domains.
Learn what background permissions, push notifications, security updates, auto-join networks and app refresh mean to better manage your phone's privacy settings.

Tips for Building a Great Business Website

A website is lifeline of internet home business. In order... Read More

New Years Resolutions: Is Improving Your Website One of Them?

With the New Year upon us yet again, it's time... Read More

Guide to Internet Business - Website Setup

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

How Worldly is Your World Wide Web?

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

How to Handle Web Surfers Who Disabled JavaScript

There are hundreds of millions of Web surfers today. Each... Read More

Five Reasons You Have to Stop Your Web Site

That's right. Your method, behavior and strategy you are now... Read More

Creating Websites that Cater to Exam Preparations

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

Website Imperatives and Solutions

When you take a look at the most visited sites... Read More

What YOU Should Know Before Getting A Web Site!

Everyday thousands of new businesses make the leap and take... Read More

Abandonment - Why Visitors Don?t Turn Into Customers

Every good Internet business understands the value of conversions versus... Read More

PIM Team Case Study: Creating Text Effects With PHP and GD

See how you can create graphic effects on text with... Read More

Building a Web Site Requires the Right Software to Eliminate Headaches and Keep Money in Your Pocket

When most people build a web site, they think one... Read More

Do You Really Need A Website To Succeed

Rosalind wants to share her inspirational poetry with a wider... Read More

Search Engines and Customers Want Focused Web Site Content

How do you decide on the content, products and or... Read More

Entangled in the World Wide Web

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

Developing A Login System With PHP And MySQL

Most interactive websites nowadays would require a user to log... Read More

Web accessibility for screen magnifier users

The needs of screen magnifier users are overlooked when implementing... Read More

Disability Discrimination Act (DDA) & Web Accessibility

There's been widespread speculation about the new legislation being introduced... Read More

Your Future Profit Is In The Stars

You don't believe in astrology but you check your horoscope... Read More

Building a Web Store on Shoestring Budget in less than Four Hours

Starting a business online is no longer as difficult as... Read More

Guide to Internet Business - Design and Content

After deciding what kind of internet business you want to... Read More

Ever Wondered What Challenges Other People With Their Web Site?

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

Improve Conversion Rates ? Load Times

Your site is getting traffic, but conversion rates are horrible.... Read More

How Your Own Website Helps Your Small Business Grow

What do you mean, you don't have a website for... 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

maide service in Buffalo Grove ..