Web Development

Web Development - Articles & Information To Help You With Web Development

Functions And Subroutines In Asp


Web Development

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







Created & Maintained by Empower! CMS Web Sites

Host2Sell Web Hosting   |   Emarketing Workshops   |   Site SEO Review   |   FREE Newsletter

South Africa's Top Sites




| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |











Stop Losing Precious Web Site Traffic To The Dreaded World Wide Web Black Hole
You work hard to build traffic to your web page. If you are not doing 1 simple step you are loosing a portion of all your web site traffic to the dreaded World Wide Web Black Hole.We have all seen the dreaded 404 page not found error. This Error happens when you type a web page address into the browser window and the page does not exist on the Web Site. Most Cpanel's enabl...(related: Web Development)


Top 5 Webmaster Tools You Can Pick Up For Pennies
1. SiteSpinner V2 - On Salewww.deprice.com/sitespinner.htmSiteSpinner is a user-friendly web site development tool that includes many advanced features such as drag-and-drop positioning, built in FTP to publish directly to your web site, an image editor and graphics creation all in one.2. Codelock 2.0 - On Salewww.deprice.com/codelock.htmCodelock encrypts PHP and any HTML generated by your PHP scripts. You can password protect your scripts, create an expiry time or unlock key, lock to IP or URL and more. Codelock Offers strong protection for an affordable price. The Encryptor software works with any install of PHP4.x or better. Requires no additional software to be install...(related: Web Development)


How To Increase Sales With Automation Improvements
How popular is online shopping? And is there room for improvement? Let's check out some pros and cons of automated shopping and see.Digital carts offer convenience for people around the globe 24 hours a day. Yet an estimated 4 percent of shoppers make purchases, up from 3.5 percent in 2003, reported by DoubleClick, an online marketing company.And approximately 30 percent of shopping carts are abandoned, according to recent surveys by BizRate.com, a shopping search engine.The main reasons for shopping cart abandonment are*:
  • High shipping prices (72%)
  • Comparison shopping or browsing (61%)
  • ...(related: Web Development)

    I Did It: Six Months To A Successful Website
    For years now I've been looking to start my own business, but my highest priority has always been to spend less time working, a...(related: Web Development)


    Internet Marketing Website Promotion -the 7 Biggest Mistakes I See People Make With Websites!
    1. Many people are not getting good or complete advice. Often for example people don't understand all the concepts of Internet Marketing and having a website so they simply pay to have a website developed. Often this website may look good but...(related: Web Development)


    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.Customers can access information on all your products, no more salesperson with limited knowledge trying to explain something they no little about. No more waiting in lines for help, all people hav...(related: Web Development)


    Learn To Build A Better Online Business Website Using Keywords And Content
    Are you aware that 90% of the websites on the Internet today are a waste of time and space. You have probably been on some of the sites I am referring to. You know the ones that when you arrive it really has nothing to do with your search criteria.Why do online business newbies create these websites?How many web...(related: Web Development)


    The Internet Road Map
    1) Data CaptureOne easy way to collect e-mail addresses and permission is via a signup box on your website. Place your signup box in a visible place on your homepage, and other appropriate pages, where visitors might choose to opt-in.Don't just do what I suggested, but go one step further. What that means specifically is always giving back to your customers before you expect them to give. Give them an offer with some urgency behind it and a call to action. For example, why not offer a visitor to your website a whitepaper, a free consultation, or a short clip of audio from a recent speaking engagement or seminar. People love when you extend the olive branch. In return, you'll usua...(related: Web Development)


    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.Readers love fresh, relevant, useful content. People surf the net to find solutions to everyday problems. If you can provide this kind of information, your readers will love your site and will tell th...(related: Web Development)


    Website Conversion Rate: More Keywords Equals More Sales
    Your website conversion rate should concern you. This simply refers to how many visitors to your website it takes before you make a sale. It is called conversion because you are 'converting' a casual visitor into a buyer. Website conversion should concern you because when you can increase the rate at which your website visitors convert into buyers, you make more money without increase your costs. Let me explain.If you get one sale for every o...(related: Web Development)




    Google




    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.Many Web designers seem to focus more on appearance than functionality, and while an attractive site is helpful, visitors that become frustrated by the inability of a site to function well, will leave almost immediately, never to return!Just as important in designing an e-commerce-style Web site, is a somewhat basic knowledge of Web site design, with some simple design skills included. While a thorough knowledge of design and functionality is not important, designing an e-commerce site does have some requirements, or else you'll always find yourself g...(related: Web Development)

    Web Accessibility For Screen Magnifier Users
    The needs of screen magnifier users are overlooked when implementing web accessibility on to a website. Screen magnifiers are used by partial...(related: Web Development)

    The Problem With Automated Accessibility Testing Tools
    An automated accessibility tool is a piece of software which can test a web page, or even an entire website, for accessibility. Automated accessibility tools are useful because they can save you a huge amount of time. Don't want to check images for alt text on each and every page on your website? Run the site through an automated tester and it'll do it all for you!Automated accessibility testing tools have been around for a long time and have historically been a useful way of checking websites for accessibility. Bobby, one of the first and most well-known automated accessibility testing tools, is now almost 10 years, and although is no longer freely available, plenty of other f...(related: Web Development)

    site-map - Copyright © 2006 Empower! Web Design | All Rights Reserved. | Web Development