RaysWebClass.Com


Lesson 31:   Functions & Procedures


As an ASP Programmer you can write ASP Functions that work just like build-in functions. For example the function Left(Item,2) returns the first two letters from the left of Item. You can similarly write a function that will return a value like SquareRoot. The following program will ask the user to enter a Maximum Number, the Program will look through the numbers (your number down to 1) and print out all the number that have an integer square root:
<!--Square Root-->
<html>
<head>
  <title>Square Root</title>
</head>
<body>
<%
Dim MaxNumber
MaxNumber = Request.Form("MaxNumber")
If MaxNumber*1 > 0 Then
   Dim X, Y
   For X = MaxNumber*1 to 1 step -1
       Y = SquareRoot(X)
       If Y = Clng(Y) Then
           Response.Write X & " has a Square Root of " & Y & "<br>"
       End If
   Next
End If
Function SquareRoot(X)
    SquareRoot = sqr(X)
End Function
%>
<form method="post" action="SquareRoot.asp">
Enter Max Number: <input type="text" name="MaxNumber">
<input type="submit" value=" Compute ">
</form>
</body>
</html>
The Function SquareRoot is near the bottom of the ASP code:

Function SquareRoot(X)
    SquareRoot = sqr(X)
End Function


This Function is "called" from the line: Y = SquareRoot(X) and it returns the square root of the number X. So Y will be assigned the square root of X.
The main ASP routine will run when MaxNumber has a value greater then zero: If MaxNumber*1 > 0 Then (Note MaxNumber is multiplied by 1 this will insure that MaxNumber is a number; after all we are collecting it from a textbox!). The ASP routine sets up a loop that will run from MaxNumber down to 1: For X = MaxNumber*1 to 1 step -1. Each number has the square root calculated and stored in Y: Y = SquareRoot(X) and then tested to see if it is an integer: If Y = Clng(Y) Then (Clng will Convert the Square Root to an Interger) If Y is already an Integer then the "If Statement" is true. If true the program will be written along with its square root: Response.Write X & " has a Square Root of " & Y & "<br>"
An ASP Procedure is like this:
Sub Say(This)
    Response.Write(This)
End Sub

Say("Hello World")

Web Page Design: Exercise 31


Write an ASP Program that will Count and List all the integer divisors for a Number that the user enters (test to insure the number is a positive integer). Create an ASP Function (called: Divisor) that returns the users Number divided by the number you're testing.

Your input and output should look like this:

Enter an integer Number:

28 has 6 integer divisors and they are:
28
14
7
4
2
1
Save the file in the wwwroot folder as asp31.asp.