Webmaster Papers




Google
 
Web webhostingpapers.com




/pagead2.googlesyndication.com/pagead/show_ads.js">

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 www.webforumz.com�- Copyright 2003-2004

RELATED ARTICLES


Ten Quick Tests To Check Your Website For Accessibility
The Disability Discrimination Act says that websites must be made accessible to disabled people. So how can you check that your website is up to par? There are a number of basic tests you can make to address some of the main issues that provide a good start in increasing accessibility to your site visitors:
Do You And Your Website Have Credibility?
I recently received an unsolicited e-mail from some company offering me their search engine optimization services for $199. Even though I had absolutely no interest in the offer, just out of curiosity, I decided to check out this company with the various search engines.
Establishing Your Web Presence
Part of the success of most home-based businesses is their web presence. After all, you want to benefit from the global exposure, reduced marketing expenses, and low entry costs. But to receive those benefits, you must first get your web site off the ground which means you need to select an effective domain name and find reliable web hosting.
Revealing the Firefox Secret Used by Top Web Developers and Power-Browsers
Profiles are one of the best kept secrets around regarding Mozilla Firefox (my browser of choice).
The Power Is In The Pipes: How To Get Maximum Leverage From Your Website
What is the most important part of your online business?Many people would say: "my website". And that'sunderstandable ? it's the most visible part of an internetbusiness.
Benefits Of Making Your Website Accessible To Disabled Users ? Part 2: The Business Case
The Disability Discrimination Act states that service providers must not discriminate against disabled people. A website is regarded as a service and therefore comes under this law.
Beautiful Web Sites Seldom Make More Sales
Designing an E-commerce Web site is not as simple as having a "pretty" site that is a pleasure to visit. While it is important to have an attractive site, as much thought must be given to functionality as to appearance.
7 Golden Rules For Building Mini Sites
First, an eye opener. Making your fortune through affiliate programs on the internet is not easy. Some people would tell you different. It can be done, but it is not as easy as some would have you think.
Web Measurement: What You Don?t Know Would Make A Great Book
"What's in it for me?" you ask. "Why should I measure how people use my website? How does it help and what does it all mean?" The purpose of this article is to try to give you some insight into effective web measurement and to talk about the most important page of any website, the landing or home page.
Develop a Solid Website Presence
Starting an internet business can be confusing to non-veterans. The internet is the new world of business and research. Conventional business practices rely on location. If we opened a bookstore on Broadway, New York City, thousands, or millions, would pass the store daily. In contrast, if we opened a bookstore on main street, Williamstown NJ, we would have significantly less customers passing each day.
Marketing Your Business Online
As a business you can't afford to ignore the Internet Age; in 2002 there was an estimated 605 million people online around the entire globe, today this is estimated at over a billion users worldwide.
How To Keep Your Website Sticky
Developing a website with content that entices visitors to return over and over again is something that takes time and effort to put together. This checklist can give you a great start on creating a popular website that entices readers to hang around on your site.
Ten Major Tips to Develop a Multilingual Web Site to Work
If you are living in a country that its native language is something rather than English language, then you may like to develop your website to offer content in the language of your own country.
Websites for Writers ? Why You Need One and How to Get Started Today
It may seem like the publishing industry's equivalent of Beanie Baby madness- everyone is talking about writer's websites. Freelancers and authors everywhere are jumping on the bandwagon, publishing sites to promote themselves and their work. This craze leaves many writers wondering if it's just a fad or if it's really worth the expense and effort. While it may seem like an overwhelming or frivolous endeavor, there are some solid business reasons why you should consider setting up your own website.
Cost Effective and Powerful Dynamic Websites w. Fantastico Web Hosting
If you are developing your website on a host that charges less that $5 a month, make plenty of backups as you are likely to loose your website files when the webhost goes out of business without notice. Indeed, be extremely wary of any such host. Once your website is attracting getting visitors, get your website hosted with host that does not foolishly try to over sell his server resources. And, expect to pay at least $15 a month or more for the added security and reliability of such a host.
How to Make Your Own Website For Free
If you like the Internet and surfing through the billions of web pages on it then you might be thinking that you also would like to have your very own web page. Well, fortunately you can create your own web page for free and have it online so the whole world can surf the web and see what your web page is all about. If you want to learn more about building your very own website for free, simply follow the steps below and in a short period of time you will have your very own web page!
Where on Earth is Your Websitee?
You've just finished congratulating your marketing team. After six months of concentrated effort you can now actually find your own company web site within the search engines. Everyone is busy handshaking and back patting when a voice from the back of the room rises above the din. "Yeah this is great! Can't wait until we can find ourselves on wireless devices."
Building a Web Store on Shoestring Budget in less than Four Hours
Starting a business online is no longer as difficult as it used to be! Now, there are plenty of interesting business ideas that anybody can pick up from various Internet resources. Startup related cost, which used to deter many in taking action, has become negligible. Many people already started to notice this! According to a recent report GoDaddy has gained 294,368 new sites in just the first 5 months of 2005 while the top four domain registrars combined, including GoDaddy, have gained more than half a million. Mind it! This is not the quantity of domains registered, this is the quantity of websites hosting with them! This recent explosive growth means one thing. Online business is on the rise!
I Want To Be A Freelancer
So you have decided that you want to do freelance work. You have done your homework and have developed your skills in HTML, PHP, CMS, and a slew of other applications. Now you are ready to start your new career. So how do you get started? Without clients your business is non existent. So how exactly do you build your business and find clients. Below are just a few key elements to help you get started.
Having a Domain Name And Web Site Gives Your Business The Key To The Door
Just like all things in life, the ways to run a business are rapidly changing and evolving. The potential involved with having a domain name/web site is staggering to say the least. A web site gives the consumer a 'shop front' that is not only open 24 hours a day, seven days a week, but is also situated right on their desktop. Instead of being locked into only the immediate area, the whole world's marketplace becomes your sphere of interaction.