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

In The News:

This RSS feed URL is deprecated, please update. New URLs can be found in the footers at https://news.google.com/news

Web Developer – Associate Director
Charlotte Agenda
At Barings, we are as invested in our associates as we are in our clients. We recognize those who work diligently for us and reward them for personal and professional integrity, communication skills, distinct competencies and expertise in specific ...


Fox5NY

Customer service, web development and other hot careers in 2018
Fox5NY
NEW YORK (FOX5NY.COM) - Some of the hottest jobs in 2017 were administrative professionals, project coordinators, and customer service representatives—and you're going to see a big demand for them in 2018, too. District president Dawn Fay of Robert ...


Technology Zimbabwe

Web Development, Potential and You (Part 1): Epiphanies, Beef And Making Money…
Technology Zimbabwe
Talking to one of my developer friends, an epiphany hits me. It wasn't really sparked by much, we were just talking about college degrees, the job market in Zimbabwe and why we need to align our resolutions so as to achieve the potential future we can ...

and more »

Forbes

12 Design Trends To Integrate Into Your Web Development And Marketing
Forbes
Forbes Agency Council PR, media strategy, creative & advertising execs share trends & tips Opinions expressed by Forbes Contributors are their own. Post written by. Forbes Agency Council. Successful PR, media strategy, creative and advertising ...

and more »

Customer Think

Contemporary Frameworks for Web App Development which will Trend in 2018
Customer Think
The trend of web app development is gearing up, and so is the demand for web app development experts and consultants; do you know why? It is because web apps are leveraging the device-independent architecture while being operational on multiple devices ...

and more »

Business 2 Community

UX and Agile Website Development: A Practical Guide
Business 2 Community
I've been reading lots of articles lately on rethinking website redesign. Many experts argue for a more agile approach with sprints based on user feedback and statistical website data. And they're not necessarily wrong. The problem is that many ...


Senior Web Developer
Technical.ly
Solid understanding of user-centered design principles and industry best practices, particularly as it applies to web-based applications. Familiar with modern UX design applications such as AxureRP, design platforms including Sketch & Adobe Creative ...


NBC 6 South Florida

Intense Computer Programming Boot Camp Trains Students
NBC 6 South Florida
Visit a class at Ironhack Miami and you'll notice there's no drill instructor screaming at students. No one is demanding that pushups be done, no one is standing at attention, but this is definitely boot camp. It's computer programming boot camp ...


Technology Zimbabwe

Web Development, Potential and You (Part 2): The Value Of Adopting The Bleeding Edge
Technology Zimbabwe
While writing the previous article about going beyond the “status quo” and actually focusing on improving the quality and worth we have as developers, my plan was to discuss a great deal on the hidden potential of the web and go quite deep on the topic ...


Freelance Sr. Web Developer
Built In Chicago
Enjoy building great things with great people? We're looking for enthusiastic individuals to help our tech team in problem solving, building, designing and architecting advanced technical solutions for our clients! One North is structured in an open ...

Google News

The 2 Most Common Web Site Mistakes

When we review the sites of potential clients, there are... Read More

Building a Great Intranet Taxonomy

While it may seem like a "quick and easy" task,... Read More

Websites: You Get What You Pay For!

So you finally decided to invest in a web presence... Read More

5 Tips To Supercharge Your Website Sales Copy

The art of turning website visitors into paying customers depends... Read More

Linking Out is Good

Many websites I come across don't have a single link... Read More

Web Site Promotion: How To Advertise Your Website For Profits

Making money with your web site is the dream and... Read More

Using Mini Websites - Powerful Way to Direct Marketing

Mini Websites are the most powerful and cost effective solution... Read More

7 Killer Ways To Increase Your Online Sales

Every online entrepreneur I talk to is asking the same... Read More

How Your Own Website Helps Your Small Business Grow

What do you mean, you don't have a website for... Read More

Is Your Website Innovative? Increase Your Business Over Night!

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

The Cost Of A Web Site

Just starting out, you can start with a simple informational... Read More

What Does An Ex-Basketball Coach Know About Pulling Web Metrics?

In my previous life.....Actually, my previous JOB life, I was... Read More

Preventing Repeat Form Submission Using PHP Sessions

We've all seen those messages on some websites warning not... Read More

From Search to Sale

The Internet car buyer is an elusive animal. Trying to... Read More

9 Keys to an Effective Website

Many small companies choose not to invest in a professionally... Read More

Website Strategy!

A website lets you put your products in front of... Read More

What and How to choose the Right Keywords for Mega Traffic

Keywords in Search Engine Optimizing and Search Engine Marketing are... Read More

12 Essential Tips to a Professional Business Website

1. Choose a big enough font size. A size 12... 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

What YOU Should Know Before Getting A Web Site!

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

Content Management

More and more businesses are recognizing the importance of content... Read More

What ROI You Should Expect From Your Web Site

Is Your Web Site an Expense or an Investment?Why don't... Read More

Be Creative Before You Purchase Or Create A Website

Before you jump into spending coutnless hours on designing and... Read More

Achieving Differentiation With Your Website

Internet Marketing is not a miracle marketing strategy ? competition... Read More

Improve Web Sales Figures Forever And Ever Amen!

"How do I improve web site sales figures?" The CEO... Read More