Home page
 
 Home 
 ASP 
 PHP 
 SQL 
 HTML 
 JavaScript 
 Search 
 Contact 
 
Search
or browse popular tags
ASP Tutorial
Subscription

Sign up for the free email newsletter for new tips, tutorials and more. Enter your email address below, and then click the button.

Privacy Policy

RSS Twitter

ASP: Functions and Subprocedures

Print

Functions and procedures provide a way to create re-usable modules of programming code and avoid rewriting the same block of code every time you do the particular task. If you don't have any functions/procedures in your ASP page, the ASP pages are executed from top to bottom, the ASP parsing engine simply processes your entire file from the beginning to the end. ASP/VBScript functions and procedures, however, are executed only when called, not inline with the rest of the code. A function or subprocedure can be reused as many times as required, thus saving you time and making for a less clustered looking page.

You can write functions in ASP similar to the way you write them in Visual Basic. It is good programming practice to use functions to modularize your code and to better provide reuse. To declare a subroutine (a function that doesn’t return a value, starts with the Sub keyword and ends with End Sub), you simply type:

<%@ LANGUAGE="VBSCRIPT" %>
<% 
Sub subroutineName( parameter_1, ... , parameter_n )
  statement_1
  statement_2
   ...
  statement_n

end sub
%>

A function differs from a subroutine in the fact that it returns data, start with Function keyword and end with End Function. Functions are especially good for doing calculations and returning a value. To declare a function, the syntax is similar:

<%@ LANGUAGE="VBSCRIPT" %>
<% 
Function  functionName( parameter_1, ... , parameter_n )
  statement_1
  statement_2
   ...
  statement_n
end function
%>

Have a look at the code for a procedure that is used to print out information on the page:

<%@ LANGUAGE="VBSCRIPT" %>
<%
Sub GetInfo(name, phone, fee)
  Response.write("Name: "& name &"<br>")
  Response.write("Telephone: "& telephone &"<br>")
  Response.write("Fee: "& fee &"<br>")
End Sub
%>

Now let's consider how to call the sub. There are two ways:

<%
'the first method
Call GetInfo("Mr. O'Donnel","555-5555",20)
'the second one
GetInfo "Mr. O'Donnel","555-5555",20
%>

In each example, the actual argument passed into the subprocedure is passed in the corresponding position. Note that if you use the Call statement, the arguments must be enclosed in parentheses. If you do not use call, the parentheses aren't used.

Now let's look at the code for a function that takes an integer value and returns the square of that value. Also included is code to call the function.

<%
Function Square(num)
  Square = num * num    
end function

  'Returns 25
  Response.Write(Square(5))

  'Should print "45 is less than 8^2"
  if 40 < Square(7) then
    Response.Write("45 is less than 8^2")
  else
    Response.Write("8^2 is less than 40")
  end if
%>

To return a value from a function, you need to name the output value the same as your function or you will not get a value returned.




Tags:

Add To: Add to dzone dzone | Digg this digg | Add to del.icio.us del.icio.us | Stumble it stumbleupon

  • Comments





Copyright © 2005-2014             www.WebCheatSheet.com All Rights Reserved.