Home

Introduction to Functions

 

Introduction to Procedures

 

Procedures

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.

Using a Script

If you want to create a function in the code of a web page where you plan to use it, include it in the head section of the web page. The creation of a function starts with <script> and ends with </script>:

<%@ Page Language="VB" %>

<html>
<head>

<script>

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

The <script> tag uses various attributes we will review. To start, you must specify that the script will run on the server. To do this, at the attribute runat to the script and assign the server string to it. This can be done as follows:

<%@ Page Language="VB" %>

<html>
<head>

<script runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

After doing this, you can create your function between the starting <script> and the end </script> tags.

The <script> tag is equipped with an attribute named language. To specify the language, assign VB, VBSscript, JavaScript, JScript, or ECMAScript (remember that the Visual Basic language is not case-sensitive). This can be done as follows:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

If you don't specify the language, VB is assumed. In most cases, VB and VBScript follow the same rules (but they have some differences). JavaScript and JScript don't follow the same rules. For this reason, if you plan to follow rules other than those of VB, you should specify the language.

Besides the language, you should specify how the code of your script will be formatted or considered. To support this, the <script> tag provides the type attribute. To specify it, assign:

  • text/VB to the type attribute if you had, or will, specify the language as VB
  • text/vbsscript to the type attribute if you had, or will, specify the language as VBScript
  • text/javascript to the type attribute if you had, or will, specify the language as JavaScript
  • text/jscript to the type attribute if you had, or will, specify the language as JScript
  • text/ecmascript to the type attribute if you had, or will, specify the language as ECMAScript

Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

Remember that only the runat attribute is required. The others are optional. After specifying the values of the desired attributes, you can create your procedure.

Creating a Function

To create a function, you use the Function keyword followed by a name and parentheses. Unlike a sub procedure, because a function returns a value, you must specify the type of value the function will produce. To give this information, on the right side of the closing parentheses, you can type the As keyword, followed by a data type. To indicate where a function stops, type End Function. Based on this, the minimum syntax used to create a function is:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

AccessModifier(s) Function FunctionName() As DataType
    
End Function

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

As seen for a sub procedure, a function can have an access modifier. The rules are the same as we described for a sub procedure. The Function keyword is required. The name of a function follows the same rules and suggestions we reviewed for sub procedures.

The As keyword may be required (in the next sections, we will review the alternatives to the As DataType expression).

The DataType factor indicates the type of value that the function will return. If the function will produce a word or a group of words, you can create it as String. The other data types are also valid in the contexts we reviewed them. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Function GetFullName() As String
        
End Function

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

As done with the variables, you can also use a type character as the return type of a function and omit the As DataType expression. The type character is typed on the right side of the function name and before the opening parenthesis. An example would be GetFullname$(). As with the variables, you must use the appropriate type character for the function:

Character The function must return
$ a String type
% a Byte, Short, Int16, or In32
& an Int64 or a Long
! a Single type
# a Double
@ a Long integer

Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Function GetFullName$()

End Function

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

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:

<script language="vbscript" type="text/vbsscript" runat="server">

Function GetFullName$()
    Dim Variable As String
End Function

</script>
 
 
 
 
 

Returning a Value From a Function

After performing an assignment in a function, to indicate the value it returns, somewhere after the assignment and before the End Function line, you can type the name of the function, followed by the = sign, followed by the value the function returns. Here is an example in which a function returns a name:

<script language="vbscript" type="text/vbsscript" runat="server">

Function GetFullName$()
    Dim FirstName As String, LastName As String

    FirstName = "Gertrude"
    LastName = "Monay"

    GetFullName = LastName & ", " & FirstName
End Function

</script>

Alternatively, instead of using the name of the function to indicate the value it returns, you can type Return, followed by the value or the expression that the function returns. Based on this, the above function could also be created as follows:

<script language="vbscript" type="text/vbsscript" runat="server">

Function GetFullName$()
    Dim FirstName As String, LastName As String

    FirstName = "Gertrude"
    LastName = "Monay"

    Return LastName & ", " & FirstName
End Function

</script>

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.

Calling a Function

As done for the sub procedure, in order to use a function in your program, you must call it. Like a sub procedure, to call a function, you can simply type its name in the desired section of the program. 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 in the section where you are calling the function. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Function GetFullName$()
    Dim FirstName As String, LastName As String

    FirstName = "Gertrude"
    LastName = "Monay"

    Return LastName & ", " & FirstName
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim FullName$

    FullName = GetFullName()
    Response.Write(GetFullName$)
%>
</body>
</html>

Here is an example of running this program:

Calling a Function

A Function and a Procedure

Depending on an author, in the Visual Basic language, the word "procedure" includes either a sub-procedure created with the Sub keyword, or a function created with the Function keyword. In the same way, for the rest of our lessons, the word procedure will be used to represent both types. Only when we want to be precise will we use the expression "a sub-procedure" to explicitly mean the type of procedure that does not return a value. When the word "function" is used in our lessons, it explicitly refers to the type of procedure that returns a value.

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc. Next