VKS-LEARNING HUB

Home » Class XII » MMWT-XII » Procedures

Procedures

There are two kinds of procedures in ASP: A sub procedure and a function. The difference lies on their behaviors but their coding (programming) depends of your goal

A procedure can be included in the body of an HTML but to separate the script behavior from the rest of the file, it is usually a good idea to include the procedures in the head section of the file.

The advantage of including a script in the head section is that it is more likely to be interpreted before the section it refers to is reached. If you have done any type of programming before, you may know that interpreters (and compilers) read a program in a top-down approach. Therefore, if the browser (actually the VBScript interpreter) finds a thing in the body section but doesn’t know what that thing is because it is in the bottom part of the body section, it may not interpret your script accurately. But if the script is in the head section, the interpreter will have “seen” it before reaching the body section

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

% >

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 %>

or

<% ShowFullName() %>

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

Arguments and Parameters

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(s))

 

End Sub

The argument must be declared as a normal variable, omitting only the Dim keyword. Procedure can takes one or more arguments,  Here is an example that creates a Sub procedure that takes a two string as argument:

<%

Sub ShowFullName(FirstName, LastName)

Dim FullName

FullName  = LastName & “, ” & FirstName

Response.Write(FullName)

End Sub

%>

Calling Procedure with Argument(s)

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” %>

Or

<% ShowFullName( “Beltrami”, “Ramirez”) %>

 

 

If the values are numbers, provide their values. If you use a mix of values, because VBScript doesn’t provide a mechanism to identify the values, it is your responsibility to make sure that each value is properly passed.

Functions

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.

Function Creation

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

%>

<% Response.Write("Person Name: ") %>
<% =GetFullName %>
 
Since the primary purpose of a function is to return a value, to better take advantage of such a value, you can assign the name of a function to a variable or to the value attribute of a web control in the section where you are calling the function.

 

Calling 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 arguments


Leave a comment