Functions are basically the exact same as subroutines, except that they return a value. That means that the function itself has a type, and the function will return a value to the calling subroutine based on the code that it contains. An example of this would be a function that adds two numbers, shown below. A function is declared the exact same way as a subroutine, except using the "Function" keyword instead of "Sub". To return a value, assign a value of the proper type to the function's name, as if it were a variable. Function Creation|
To create a function, you use the Function keyword followed by a name and parentheses. Unlike a sub routine, because a function returns a value, you should/must specify the type of value the function will produce. To give this information, on the right side of the closing parentheses, type the As keyword, followed by a data type. To indicates where a function stops, type End Function. Based on this, the minimum syntax used to create a function is: Function FunctionName() As DataType End FunctionThe name of a function follows the same rules and suggestions we reviewed for sub routines. 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. If the function will check something and determine whether it produce a true or a false value, you can create it as Boolean. The other data types are also valid in the contexts we reviewed them. 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 routine, 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: Function CallMe() As String Dim Salute As String Salute = "You can call me Al"End FunctionAfter performing an assignment in a function, to indicate the value it returns, somewhere after the assignment and before the End Function line, 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: Function CallMe() As String CallMe = "You can call me Al"End FunctionYou can also use some local variables in the function to perform an assignment and then assign their result to the name of the function. Here is an example:Function CallMe() As String Dim Salute As String Salute = "You can call me Al" CallMe = SaluteEnd Function|
Practical Learning: Creating a Function|
1. Display the Routines module and delete (only) the previously implemented routines 2. To create a new function, at the end of the file, type function RectPerimeter and press Enter 3. To create another function, on the main menu, create Tools -> Add Procedure... 4. In the Name edit box, type RectArea 5. In the Type section, click Function| Calling a Function|
As done for the sub routines, in order to use a function in your program, must call it. Like a sub routine, to call a function, you can simply type its name in the desired section of the program. Here is an example: Private Sub Form_Load() CallMeEnd SubSince 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 property or a variable in the section where you are calling the function. In the following example, the return value of the CallMe function is assigned to the Caption property of the form from its own Load event: Private Sub Form_Load() Caption =...