Functions
Functions are like subprograms, except they talk back -- they return a data
value to the calling statement. Functions can be passed arguments, if
needed, or they can operate without information being fed to them.
Function Formats
The general format for a function is shown below.
|--------------------------------------------------------------------------|
| |
| Function name (argument As type [, argument As type]...) [As type |
| ] ...statements |
| |
| [Return value] [Exit Function] |
| |
| End Function |
| |
|--------------------------------------------------------------------------|
A function is identified by the keyword Function at its beginning and the
keywords End Function at its end. A function name is a programmer-supplied
name that follows Visual Basic naming conventions. The name must be unique
among the collection of functions on the page. Visual Basic statements are
enclosed inside the function to carry out its processing.
There are two ways to set up a function to return a value. The function
itself can be declared As type, a value is assigned to the function name,
and an Exit Function statement returns the value to the calling statement.
This setup is illustrated in the code below which assumes no arguments
being passed to the function.
Function Get_Value() As Integer
Get_Value = 10
Exit Function
End Function
The second way to set up a function is to use a Return statement to
explicitly pass an identified value back to the calling statement.
Function Get_Value()
Dim Value As Integer
Value = 10
Return Value
End Function
Calling Functions
A function is called by a statement in the following general format.
|--------------------------------------------------------------------------|
| |
| [Call] Function name (argument [, argument]...) |
| |
|--------------------------------------------------------------------------|
The optional keyword Call is followed by a function name, followed by an
argument list, if needed, separated by commas and contained inside a set of
parentheses. If no arguments are supplied, the parentheses are not
required. As in the case for subprograms, these arguments are usually names
of variables whose values are passed to the called function.
The following example shows a function named Get_Area which calculates the
area of a rectangle when supplied with height and width dimensions by the
calling statement.
<SCRIPT runat="server">
Sub Rectangle
Dim Width As Integer = 10
Dim Height As Integer = 5
Dim Area As Integer
Area = Get_Area(Width, Height)
End Sub
Function Get_Area (Width As Integer, Height As Integer) As Integer
Get_Area = Width * Height
Exit Function
End Function
</SCRIPT>
Subprogram Rectangle sets the Width and Height of a rectangle; then it
assigns to variable Area the returned value from function Get_Area,
supplying it with the width and height values. Function Get_Area receives
the passed values through its Width and Height arguments (local to the
function and not to be confused with the like-named variables in subprogram
Rectangle). The area calculation is assigned to the function name and is
returned to the calling statement when the function exits.
As you can see, since a function returns a value, it can be treated just
like a variable. In the above case, the function is assigned to a local
variable, effectively assigning the returned value to the variable.
An alternative to the above coding is a function that explicitly returns
its computed value.
Function Get_Area (Width As Integer, Height As Integer)
Dim Area As Integer
Area = Width * Height
Return Area
End Function
You can use as many Exit Function and Return statements as needed. In more
complex functions with alternate computational paths a value may be
returned at different points in the function.
Calling Functions from ASP.NET Pages
Very often a function is called from a server control coded on the page
rather than from a subprogram. This routinely happens during data binding.
A simple example is shown below where the current date is embedded on the
page when it loads. The date is supplied by a function call.
Today's date is Monday, July 16, 2007
<SCRIPT runat="server">
Sub Page_Load
Page.DataBind()
End Sub
Function Get_Date() As String
Get_Date = FormatDateTime(DateString, DateFormat.LongDate)
End Function
</SCRIPT>
<b>Today's date is <%# Get_Date %></b>
The binding expression <%# Get_Date %> is a call to the function, whose
returned value replaces the call when Page.DataBind() is executed in the
Page_Load procedure. Similar function calls are made when binding
calculated data to <asp:Repeater>, <asp:DataGrid>, and <asp:DataList>
controls.