VB 2008 provides many functions that implement common or complicated tasks, and you can look them up in the documentation. (You’ll find them in the Visual Studio ➢ Visual Basic ➢ Reference ➢ Functions branch of the contents tree in the Visual Studio documentation.) There are functions for the common math operations, functions to perform calculations with dates (these are truly complicated operations), financial functions, and many more.When you use the built-in functions, you don’t have to know how they work internally — just how to call them and how to retrieve the return value.

The Pmt() function, for example, calculates the monthly payments on a loan. All you have to know is the arguments you must pass to the function and how to retrieve the result. The syntax of the Pmt() function is the following, where MPay is the monthly payment, Rate is the monthly interest rate, and NPer is the number of payments (the duration of the loan in months). PV is the loan’s present value (the amount you took from the bank):

`MPay = Pmt(Rate, NPer, PV, FV, Due)`

Code language: VB.NET (vbnet)

Due is an optional argument that specifies when the payments are due (the beginning or the end of the month), and FV is another optional argument that specifies the future value of an amount. This isn’t needed in the case of a loan, but it can help you calculate how much money you should deposit each month to accumulate a target amount over a given time. (The amount returned by the Pmt() function is negative because it’s a negative cash flow— it’s money you owe — so pay attention to the sign of your values.)

To calculate the monthly payment for a $20,000 loan paid off over a period of six years at a fixed interest rate of 7.25%, you call the Pmt() function, as shown in Listing 3.8.

**Listing 3.8: Using the Pmt() Built-in Function**

```
Dim mPay, totalPay As Double
Dim Duration As Integer = 6 * 12
Dim Rate As Single = (7.25 / 100) / 12
Dim Amount As Single = 20000
mPay = -Pmt(Rate, Duration, Amount)
totalPay = mPay * Duration
MsgBox("Your monthly payment will be " & mPay.ToString("C") & _
vbCrLf & "You will pay back a total of " & _
totalPay.ToString("C"))
```

Code language: VB.NET (vbnet)

Notice that the interest (7.25%) is divided by 12 because the function requires the monthly interest. The value returned by the function is the monthly payment for the loan specified with the Duration, Amount, and Rate variables. If you place the preceding lines in the Click event handler of a Button, run the project, and then click the button, the following message will appear in a message box:

```
Your monthly payment will be $343.39
You will pay back a total of $24,723.80
```

Code language: VB.NET (vbnet)

Let’s say you want to accumulate $40,000 over the next 15 years by making monthly deposits of equal amounts. To calculate the monthly deposit amount, you must call the Pmt() function, passing 0 as the present value and the target amount as the future value. Replace the statements in the button’s Click event handler with the following and run the project:

```
Dim mPay As Double
Dim Duration As Integer = 15 * 12
Dim Rate As Single = (4.0 / 100.0) / 12
Dim Amount As Single = -40000.0
mPay = Pmt(Rate, Duration, 0, Amount)
MsgBox("A monthly deposit of " & mPay.ToString("C") & vbCrLf & _
"every month will yield $40,000 in 15 years")
```

Code language: VB.NET (vbnet)

It turns out that if you want to accumulate $40,000 over the next 15 years to send your kid to college, assuming a constant interest rate of 4%, you must deposit $162.54 every month. You’ll put out almost $30,000, and the rest will be the interest you earn.

Pmt() is one of the simpler financial functions provided by the Framework, but most of us would find it really difficult to write the code for this function. Because financial calculations are quite common in business programming, many of the functions you might need already exist, and all you need to know is how to call them. If you’re developing financial applications, you should look up the financial functions in the documentation.

Let’s look at another useful built-in function, the MonthName() function, which accepts as an argument a month number and returns the name of the month. This function is not as trivial as you might think because it returns the month name or its abbreviation in the language of the current culture. The MonthName() function accepts as arguments the month number and a True/False value that determines whether it will return the abbreviation or the full name of the month. The following statements display the name of the current month (both the abbreviation and the full name). Every time you execute these statements, you will see the current month’s name in the current language:

```
Dim mName As String
mName = MonthName(Now.Month, True)
MsgBox(mName) ' prints "Jan"
mName = MonthName(Now.Month, False)
MsgBox(mName) ' prints "January"
```

Code language: VB.NET (vbnet)

A similar function, the WeekDayName() function, returns the name of the week for a specific weekday. This function accepts an additional argument that determines the first day of the week. (See the documentation for more information on the syntax of the WeekDayName() function.) The primary role of functions is to extend the functionality of the language.Many functions that perform rather common practical operations have been included in the language, but they aren’t nearly enough for the needs of all developers or all types of applications. Besides the built-in functions, you can write custom functions to simplify the development of your custom applications, as explained in the following section.