Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/a26f9f83/public_html/articles/includes/config.php on line 159
Functions and Subroutines in ASP > NetSparsh - Viral Content you Love & Share

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 - Copyright 2003-2004

In The News:

This RSS feed URL is deprecated, please update. New URLs can be found in the footers at

Research by GoodFirms Covered the list of Top Web Development Companies in USA
EIN News (press release)
WASHINGTON, WASHINGTON DC, USA, March 22, 2018 / -- As people are getting online and spending most of their waking hours on internet, all the business owners have realized the significance of having a corporate website. As there are ...

Research by GoodFirms Covered the list of Top Web Development ...
EIN News (press release)
Latest research by GoodFirms spotlights Top Web Development Companies in USA determined based on overall performance and patrons feedbacks.

and more »


Learn to Code with 21 of the Best Web Development Courses
By Seth G. Macy If you buy something through this post, IGN may get a share of the sale. For more, read our Terms of Use. Understanding the ins and outs of how the world wide web is built is an incredibly valuable skill. You can leverage your ...


Netlify wants to make it easier for web developers to use AWS Lambda event triggers
Today, the company announced another step in that vision when it introduced AWS Lambda functions on Netlify. The company aims to reduce much of the complexity associated with web development. You design your front end in HTML and JavaScript, then ...

Popular Science

Master front-end web development with 7 eBooks and 21 hours of video training
Popular Science
... every drop-down menu and login page, there's a front-end developer. This bundle helps you build this lucrative skill set, with no previous coding experience required. The lessons start with the fundamentals, showing you how to construct basic ...

Google's Dart 2 Reboot Targets Client-Side Mobile, Web Development
ADT Magazine
NET development, for example -- to programming code for app logic and so on. These changes and more are especially suited for client-side development of mobile iOS and Web apps wherein much of the codebase -- gauged at 50 percent to 70 percent -- can ...

and more »


Get a complete web development education for less than $5 per e-book
Who would you rather hire: a guy to build your new website and another guy to build the hardware infrastructure to run your new site? Or one guy who can do both jobs? Hence…the explosion of the full stack developer. With the knowledge to handle both ...

Business Wire (press release)

GoodFirms Research Reveals Top Web Development Companies in USA
Business Wire (press release)
WASHINGTON--(BUSINESS WIRE)--In a business environment today, every large and small company is looking for their web presence to enhance and beat the competition. Websites have become the key for the entrepreneurs to get profit through online customers ...

and more »

Wicked Local

Nashoba Tech receives programming, web development grant
Wicked Local
Nashoba Tech's mission is to provide the highest-quality career and technical education to prepare students for the world of college and career, and Director of Curriculum Gabriella White said the $20,000 grant will further that mission for students in ...

One of central Indiana's oldest web developers acquired
Indianapolis Business Journal
Brumbarger kept Bitwise alive in the early years of web commerce by networking tirelessly, staying focused on web development and running his company with a policy of refusing to take on new investors and debt. “The bottom line is, we've survived the ...

and more »
Google News

What Works and What not Works for Your Internet Business

"Build it and they will come."--Filed of Dreams (1989) If... Read More

Whats Your Business Online Attitude?

This article was prompted by some customer support work I... Read More

Is Your Website Innovative? Increase Your Business Over Night!

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

What Your Customers Say About YOUR Site! Part 1

The following is a list of patterns that many visitors... 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

Profitable Websites for Exclusive Industries

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

Developing A Website: 10 Tips For Getting Started

1. Be clear on your purpose.Building a website can be... Read More

How To Get Profits From Your 404 Page Not Found File

"'Page Not Found' on this Server. Check the URL and... Read More

Using Psychology Easily on Your Website to Make More Sales!

Psychology is a long word which everyone thinks, "no, that's... Read More

Why Should Your Business Go O.N.L.I.N.E.

Optimize your time. How much time each day must you... Read More

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

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

How to Make Your Own Website For Free

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

Web accessibility for screen magnifier users

The needs of screen magnifier users are overlooked when implementing... 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

How To Keep Your Website Sticky

Developing a website with content that entices visitors to return... Read More

Building eCommerce Websites that Work - Part 2

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

Building eCommerce Websites that Work - Part 3

An interesting eCommerce success factor that isn't precisely overlooked, but... Read More

Bring Your Visitors Back Clamoring for More! Maintain and Improve Your Web Site Weekly - Part 1

80% of your Web site is Maintenance!Once your Web site... Read More

The Power Of CPanel

cPanel is a control panel for your website and allows... Read More

How Web Templates Are Helping Online Businesses to MultiplyTheir Income

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

Improve Conversion Rates ? Effective Content

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

Website Sales: 10 Reasons Why People Dont Buy From You

You've put up a website to promote a product or... Read More

But My Business Doesnt Need A Website!

Revenues are often lost because many business owners don't see... Read More

Eight Deadly Web Site Mistakes and How to Avoid Them

Creating and maintaining an effective presence on the Web has... Read More

Your Website Reflects Your Business

Some left shoes are in isle 5, while the right... Read More