Functions

The use of functions in expressions is illustrated by our function demonstration program. The following is the listing of the program:

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Examples of numeric functions
        Label1.Text = CStr(Fix(1.9999)) 'truncate after decimal point
        Label2.Text = CStr(Fix(3.1) * 4 + Fix(5.999))
        Label3.Text = CStr(Len("VB.NET has many built-in functions")) 'length of a string
        Label4.Text = CStr(Rnd()) 'random number greater than 0 and less than 1

        'Examples of string functions
        Dim player As String = "Barry Bonds"
        Label5.Text = Mid(player, 3, 7) '7 characters, starting with the third
        Label6.Text = Mid(player, 4) '4th character through the end
        Label7.Text = Mid(player, Len(player) - 2, 3) 'rightmost 3 characters
        Label8.Text = UCase(player) 'convert to upper case
        Label9.Text = LCase(player) 'convert to lower case
        Label10.Text = Format(123.45, "currency") 'The Format function has many variations
        Label11.Text = Format(0.02, "percent")
    End Sub

Before you run the program, can you predict what each line will display? Some are fairly obvious, but others are not. For example, why does one value vary each time you click on the Evaluate and Display button while the others remain the same?

The Fix function drops the fractional portion of a number, and Len returns the length of a string expression, but what about Rnd? Rnd generates a different value every time it is called -- it returns a random number that is greater than zero and less than one.

Mid returns a portion of a string expression -- can you determine the meaning of the two numeric expressions? Ucase converts a string expression to upper case and Lcase to lower case. Format converts numeric expressions to formatted strings for display.

These are summarized in the following table:

Function Returns Syntax Data type Number of Arguments
fix the integer portion of a number fix (<numeric expression>) double 1
len the length of a string expression len (<string expression>) integer 1
rnd a random number between zero and one rnd () single 0
pmt loan payment amount pmt (<numeric expression>, <numeric expression>, <numeric expression>) double 3
mid middle of the string mid (<string expression>, <numeric expression>, <numeric expression>) string 3
ucase string in upper case letters ucase (<string expression>) string 1
lcase string in lower case letters lcase (<string expression>) string 1
format the string reformatted format (<string expression>, <format type>) string 2

Like expressions, a function returns a single value when evaluated. Functions also have data types determined by the type of the value they return.

The basic syntax of all functions is the same: the name of the function followed by parenthesized arguments. For example, the fix function takes one argument, a numeric expression, which is evaluated and the portion after the decimal point is eliminated.

If a function requires more than one argument, they are separated by commas. For example, the format function requires two arguments, a numeric expression followed by format type which can be "currency", "percent" or various other string constants.

Note that the rnd function does not require any arguments.

The pmt function is one of several financial functions, and its arguments are the interest rate of the loan, the number of payment periods it runs for and its principle.

Functions are really a way to capture a certain computation for convenient re-use throughout a program, and, programmers can extend VB by writing their own function sub-programs. We will learn to do that later.

The functions shown above are built-in or intrinsic to VB.NET. They are only examples -- VB.NET has many other intrinsic functions. For example, there are several functions for converting data types.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.