A function is similar to a subroutine, but a function returns a result. Because they return values, functions — like variables — have types. The value you pass back to the calling program from a function is called the return value, and its type must match the type of the function. Functions accept arguments, just like subroutines. The statements that make up a function are placed in a set of Function. . .End Function statements, as shown here:
Function NextDay() As Date
Dim theNextDay As Date
theNextDay = Now.AddDays(1)
The Function keyword is followed by the function name and the As keyword that specifies its type, similar to a variable declaration. AddDays is a method of the Date type, and it adds a number of days to a Date value. The NextDay() function returns tomorrow’s date by adding one day to the current date. NextDay() is a custom function, which calls the built-in AddDays method to complete its calculations.
The result of a function is returned to the calling program with the Return statement, which is followed by the value you want to return from your function. This value, which is usually a variable, must be of the same type as the function. In our example, the Return statement happens to be the last statement in the function, but it could appear anywhere; it could even appear several times in the function’s code. The first time a Return statement is executed, the function terminates, and control is returned to the calling program.
You can also return a value to the calling routine by assigning the result to the name of the function. The following is an alternate method of coding the NextDay() function:
Function NextDay() As Date
NextDay = Now.AddDays(1)
Notice that this time I’ve assigned the result of the calculation to the function’s name directly and didn’t use a variable. This assignment, however, doesn’t terminate the function like the Return statement. It sets up the function’s return value, but the function will terminate when the End Function statement is reached, or when an Exit Function statement is encountered.
Similar to variables, a custom function has a name that must be unique in its scope (which is also true for subroutines, of course). If you declare a function in a form, the function name must be unique in the form. If you declare a function as Public or Friend, its name must be unique in the project. Functions have the same scope rules as variables and can be prefixed by many of the same keywords. In effect, you can modify the default scope of a function with the keywords Public, Private, Protected, Friend, and Protected Friend. In addition, functions have types, just like variables, and they’re declared with the As keyword.
Suppose that the function CountWords() counts the number of words, and the function CountChars() counts the number of characters in a string. The average length of a word could be calculated as follows:
Dim longString As String, avgLen As Double
longString = TextBox1.Text
avgLen = CountChars(longString) / CountWords(longString)
The first executable statement gets the text of a TextBox control and assigns it to a variable, which is then used as an argument to the two functions.When the third statement executes, Visual Basic first calls the functions CountChars() and CountWords() with the specified arguments, and then divides the results they return.
You can call functions in the same way that you call subroutines, but the result won’t be stored anywhere. For example, the function Convert() might convert the text in a text box to uppercase and return the number of characters it converts. Normally, you’d call this function as follows:
nChars = Convert()
If you don’t care about the return value — you only want to update the text on a TextBox control — you would call the Convert() function with the following statement: