Vous êtes sur la page 1sur 10

ASP Procedures

Procedures:

One of the techniques used for effective programming is to divide a big assignment
in (relatively small) sub-assignments. Each sub-assignment is meant to (possibly
completely) solve a particular problem so that other sub-assignments of the program
can simply request its result or refer to it when necessary. Such a sub-assignment is
called a procedure. There are two categories of procedures you will use in your
programs: those that have already been created thus made available to you, and
those you will create yourself.

Some languages like VBScript and Pascal consider that there are two types of
procedures: functions and sub routines (some other languages like C++ and C#
don't explicitly differentiate both categories).

Functions and procedures provide a way to create re-usable modules of


programming code and avoid repeating the same block of code every time you do
the same task.

Introduction to Sub-Procedures

A sub procedure is an assignment that is carried but doesn't give back a result. To
create a sub procedure, you start the section with the Sub keyword followed by a
name (like everything else, a procedure must have a name). The name of a
procedure is always followed by parentheses. At the end of the sub procedure, you
must type End Sub. Therefore, the formula of a sub procedure is:

Sub ProcedureName()

End Sub

The name of a procedure follows the same rules we reviewed for variables. In
addition, there are some suggestions you can use when naming your procedures:

• If the procedure performs an action that can be represented with a verb, you
can use that verb to name it. Here are examples: show, display
• To make the name of a procedure stand, you should start it in uppercase.
Examples are Show, Play, Dispose, Close
• You should use explicit names that identify the purpose of the procedure. If a
procedure would be used as a result of another procedure, reflect it on the
name of the sub procedure. Examples would be: afterupdate, longbefore.
• If the name of a procedure is a combination of words, you should start each
word in uppercase. Examples are: AfterUpdate, SayItLoud

On the right side of the name of the procedure, you can add parentheses or omit
them. The section between the Sub and the End Sub lines is referred to as the body
of the procedure. Here is an example:

<%
Sub ShowFullName
End Sub
%>

The body of the procedure is used to define what assignment and how the
assignment should be carried. For example, if you need to use a variable, you can
declare it and specify the kind of variable you need. There is no restriction on the
type of variables that can be declared in a procedure. Here is an example in which a
variable is declared in the body of a sub routine:

<%
Sub ShowFullName
Dim FirstName
End Sub
%>

In the same way, you can declare as many variables as you need inside of a
procedure. The actions you perform inside of a procedure depend on what you are
trying to accomplish. For example, a procedure can simply be used to create a string.
The above procedure can be changed as follows:

<%
Sub ShowFullName
Dim FirstName
Dim LastName
Dim FullName

FirstName = "Paul"
LastName = "Motto"
FullName = LastName & ", " & FirstName
Response.Write("Person Name: " & FullName)
End Sub
%>

Sub Procedure Call

Once you have a procedure, whether you created it or it is part of VBScript, you can
use it. Using a procedure is also referred to as calling it. Before calling a procedure,
you should first locate the section of code in which you want to use it. To call a
simple procedure, simply type its name in the section where you want to use. Here is
an example:

<%
Sub ShowFullName
Dim FirstName
Dim LastName
Dim FullName

FirstName = "Paul"
LastName = "Motto"
FullName = LastName & ", " & FirstName
Response.Write("Person Name: " & FullName)
End Sub
%>

<% ShowFullName %>

You add or omit parentheses on the right side of the name of the procedure when
calling it.

Arguments and Parameters

So far, to use a value in a procedure, we had to declare it. In some cases, a


procedure may need an external value in order to carry its assignment. A value that
is supplied to a procedure is called an argument.

When creating a procedure that will use an external value, declare the argument that
represents that value between the parentheses of the procedure. For a sub routine,
the formula you use would be:

Sub ProcedureName(Argument)

End Sub

The argument must be declared as a normal variable, omitting only the Dim
keyword. Here is an example that creates a function that takes a string as argument:

<%
Sub ShowFullName(FirstName)

End Sub
%>

A certain procedure can take more than one argument. In this case, in the
parentheses of the procedure, separate the arguments with a comma. Here is an
example of a sub routine that takes two arguments:

<%
Sub ShowFullName(FirstName, LastName)
Dim FullName

FullName = LastName & ", " & FirstName


Response.Write(FullName)
End Sub
%>

Calling a Procedure With Argument

To call a sub procedure that takes only one argument, you can type its name
followed by space, and followed by the value of the argument. If you have that
value, you can provide it. If the value is a number and you have the number already,
you can type it after the name of the procedure. If the value is a string, make sure
you provide it in double-quotes. Here is an example:

<%
Sub ShowCountryName(Name)
Response.Write(Name)
End Sub
%>

<% ShowCountryName "Emirats Arabes Unis" %>

As an alternative, when calling a sub procedure you can precede it with the Call
keyword. In this case, the argument(s) must be passed in parentheses. With this
technique, the above procedure would be called as follows:

<%
Sub ShowCountryName(Name)
Response.Write(Name)
End Sub
%>

<% Call ShowCountryName("Emirats Arabes Unis") %>

You can also pass the name of a variable that holds the string to be passed as
argument. Here is an example:

<%
Sub ShowCountryName(Name)
Response.Write(Name)
End Sub
%>

<%
Dim CName

CName = "Mexico"
ShowCountryName CName
%>

If you are calling a sub procedure that takes more than one argument, when passing
the arguments, separate them with a comma. If you know the values to be passed
as argument when you call the procedure, provide each value. Here is an example:

<%
Sub ShowFullName(FirstName, LastName)
Dim FullName

FullName = LastName & ", " & FirstName


Response.Write(FullName)
End Sub
%>

<% ShowFullName "Beltrami", "Ramirez" %>

**********************************************************

Functions and Subroutines exist to not only save us time, but to bring power to our
ASP.

Like a sub procedure, a function is used to perform an assignment. The main


difference between a sub procedure and a function is that, after carrying its
assignment, a function gives back a result. We also say that a function "returns a
value". To distinguish both, there is a different syntax you use for a function.

To create a function, you use the Function keyword followed by a name and
parentheses. Based on this, the minimum formula used to create a function is:

Function FunctionName()

End Function

The name of a function follows the same rules and suggestions we reviewed for sub
procedures. As mentioned already, the section between the Function and the End
Function lines is the body of the function. It is used to describe what the function
does. As done on a sub procedure, one of the actions you can perform in a function
is to declare a (local) variable and use it as you see fit. Here is an example:

<%
Function GetFullName
Dim FirstName

FirstName = "Paul"
End Function
%>

After performing an assignment in a function, to indicate the value it returns,


somewhere after the assignment and before the End Function line, type the name
of the function, followed by the assignment operator "=" , followed by the value the
function returns. Here is an example:

<%
Function GetFullName
Dim FirstName
Dim LastName

FirstName = "Paul"
LastName = "Motto"
GetFullName = LastName & ", " & FirstName
End Function
%>
You can also use some local variables in the function to perform an assignment and
then assign their result to the name of the function. Here is an example:

<%
Function GetFullName
Dim FirstName
Dim LastName
Dim FullName

FirstName = "Paul"
LastName = "Motto"
FullName = LastName & ", " & FirstName
GetFullName = FullName
End Function
%>

Calling a Function

<%
Function GetFullName
Dim FirstName
Dim LastName
Dim FullName

FirstName = "Paul"
LastName = "Motto"
FullName = LastName & ", " & FirstName
GetFullName = FullName
End Function
%>

<% Response.Write("Person Name: ") %>


<% =GetFullName %>

A Function With Arguments

Like a sub procedure, a function can also take one or more arguments. When
creating such a function, provide the argument(s) the same way we introduced for
the sub procedure. Here is an example:

<%
Function SquareArea(Side)
SquareArea = Side * Side
End Function
%>

To call a function that takes an argument, you must pass the argument between
parentheses. Here is how the above function would be called:

<%
Function SquareArea(Side)
SquareArea = Side * Side
End Function
%>

<% Response.Write SquareArea(42.58) %>

Just like a sub procedure, a function can take more than one argument. When calling
such a function, remember to provide a value for each argument.

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 " & strName

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.

Differences Between VBScript and JavaScript

When calling a VBScript or a JavaScript procedure from an ASP file written in


VBScript, you can use the "call" keyword followed by the procedure name.

If a procedure requires parameters, the parameter list must be enclosed in


parentheses when using the "call" keyword.

If you omit the "call" keyword, the parameter list must not be enclosed in
parentheses.

If the procedure has no parameters, the parentheses are optional.

When calling a JavaScript or a VBScript procedure from an ASP file written in


JavaScript, always use parentheses after the procedure name.

Call a procedure using VBScript


How to call a VBScript procedure from ASP.

<html>

<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>

<body>
<p>
You can call a procedure like this:
</p>
<p>
Result: <%call vbproc(3,4)%>
</p>
<p>
Or, like this:
</p>
<p>
Result: <%vbproc 3,4%>
</p>
</body>

</html>

Result:

You can call a procedure like this:

Result: 12

Or, like this:

Result: 12

Call a procedure using JavaScript


How to call a JavaScript procedure from ASP.

<%@ language="javascript" %>


<html>
<head>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
</head>

<body>
<p>
Result: <%jsproc(3,4)%>
</p>
</body>

</html>

Vous aimerez peut-être aussi