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: -
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"
getBalloon = Tempstr
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)
Tempvar = amount * (taxrate / 100)
CalcTax = Round(Tempvar, 2) 'round the result to 2 decimal places
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
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"
'now, call the sub
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
Top 5 Reasons to Track Your Website Traffic
#5 User Satisfaction: Learn what your visitors and site users do and do not
like about your site. Using a good reporting service allows you to monitor
your pages individually to see what pages are clicked on first, the most
popular pages, duration of any given visitor's time viewing a page (visit
duration), and what pages send your visitors off your site (exit pages).
A PHP Redirect automatically transfers a web user from one URL to another. For example, typing foo.
Improve Conversion Rates - Load Times
Your site is getting traffic, but conversion rates are horrible. Everything seems to be working, so what's wrong? Your site may simply be to slow.
Content Management - The New Standard in Web Design
The evolution of web development parallels the model of technology spending by companies today. The style and method has transformed along with the business spending trend.
Does Your Business Really Need A Website?
Website, website, website, everyone says you need one. But
do you really? It all depends.
Web Development Processes and Technical Environments
The process of Web application development is critical to the success of web-based projects. The proper processes can not be enforced unless technical environments are properly setup.
Why Should Your Business Go O.N.L.I.N.E.
Optimize your time. How much time each day must you devote to paperwork, scheduling and accounting? You strive to meet the needs of your clients, but when chained to the obligations of business management, it's just as important to make sure the doors stay open as it is to satisfy customers.
5 Essential Traits Of A Home Business Website
So you've decided to start an online work at home business and are just beginning to build your website. You probably have a million different ideas for what you want the site to look like.
Web Coach Tip: What You Should Know About DIY Web Sites
Recently, a friend asked "What's the deal with those DIY companies that advertise "How to get a website with everything your business needs for under $20 bucks a month?". "How can they do that?" she said.
You Need To Treat Your Website Like An Employee - Heres 5 Ways To Do It
People often come to me in a state of crisis and say their website isn't working. Usually they're in a cold sweat:"Mike, give me some practical things I can do to get more sales from my website!"You want practical advice? Stop treating your website like an object in your business and start treating it like an employee.
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.
Create Huge Income From Your Web Site - 10 Easy Ways
The cost of setting up a web site is dirt cheap nowadays. You can register a domain for less than $2.
7 Profit Producing Reasons Why Every Successful Service Professional Needs A Web Presence
Your business website is a critical aspect of being able to start and continue conversations with potential clients. It's the ultimate self-service window for potential and current clients who come to you interested in your services and/or products.
More and more businesses are recognizing the importance of content management when it comes to their websites. Website content is more important than ever before, and as the Internet matures and changes, it is likely be become even more important.
Lead Generation How to -- 10 Tips On Generating More Internet Sales Leads
Many millions of dollars are being wasted on promoting and sending traffic to ineffective lead generation websites. Here are my top 10 tips to make your site generate more leads for you and your company.
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.
For Newer Sites, Web Marketing Depends On Titles & Links!
We all start out the same. Some have budgets, some don't.
Website Imperatives and Solutions
When you take a look at the most visited sites on the internet, what hits you in the face? Change, growth, new content. In a sense, a search engine is the perfect web site.
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.
The Evolution Of Search
There have been many significant changes to the face of search over the last several years with engines becoming more intelligent than ever before. Today's users expect mainly fast, easy, relevant and satisfactory search results.