Introduction to Functions |
|
Introduction to Procedures |
Procedures |
Like a sub procedure, a function is used to perform an assignment. The main difference between a sub procedure and a function is that, after carrying its assignment, a function gives back a result. We also say that a function "returns a value". To distinguish both, there is a different syntax you use for a function. |
To create a function, you use the Function keyword followed by a name and parentheses. Unlike a sub procedure, because a function returns a value, you must specify the type of value the function will produce. To give this information, on the right side of the closing parenthesis, you can type the As keyword, followed by a data type. To indicate where a function stops, type End Function. Based on this, the minimum syntax used to create a function is: |
AccessModifier Function FunctionName() As DataType End Function As seen for a sub procedure, a function can have an access modifier. The Function keyword is required. The name of a function follows the same rules and suggestions we reviewed for names of sub procedures. The As keyword may be required (in the next sections, we will review the alternatives to the As DataType expression). 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. The other data types are also valid in the contexts we reviewed them in the previous lesson. Here is an example: Function GetFullName() As String End Function
As done with variables, you can also use a type character as the return type of a function and omit the As DataType expression. The type character is typed on the right side of the function name and before the opening parenthesis. An example would be GetFullName$(). As with the variables, you must use the appropriate type character for the function:
Here is an example: Function GetFullName$() End Function As mentioned for a sub procedure, 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 procedure, 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 Function
After performing an assignment in a function, to indicate the value it returns, somewhere after the assignment and before the End Function line, you can type the name of the function, followed by the = sign, followed by the value that the function returns. Here is an example in which a function returns a name: Function GetFullName$() Dim FirstName As String, LastName As String FirstName = "Patricia" LastName = "Katts" GetFullName = LastName & ", " & FirstName End Function
As done for the sub procedure, in order to use a function in your program, you must call it. Like a sub procedure, to call a function, you can simply type its name in the desired section of the program. Here is an example: Function CallMe() As String Dim Salute As String Salute = "You can call me Al" CallMe = Salute End Function Sub Exercise() CallMe End Sub When calling the function, you can optionally type the parentheses on the right side of its name. 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 variable in the section where you are calling the function. Here is an example: Function GetFullName$() Dim FirstName As String, LastName As String FirstName = "Patricia" LastName = "Katts" GetFullName = LastName & ", " & FirstName End Function Sub Exercise() Dim FullName$ FullName = GetFullName() ActiveCell.FormulaR1C1 = FullName End Sub |
By now, we have seen that the primary (if not the only) difference between a function and a sub procedure is that a function returns a value. Because a sub procedure does not return a value, it cannot be directly accessed from a spreadsheet and you cannot use it with the ActiveCell.FormulaR1C1 = Value we have been using since the previous lesson. On the other hand, since a function returns a value, you can retrieve that value and assign it to our ctiveCell.FormulaR1C1 routine. Here is an example: Function GetFullName$() Dim FirstName As String, LastName As String FirstName = "Patricia" LastName = "Katts" GetFullName = LastName & ", " & FirstName End Function Sub Exercise() Dim FullName$ FullName = GetFullName() ActiveCell.FormulaR1C1 = FullName End Sub Better yet, if/when possible, you do not have to first
declare a variable that would hold the value returned by a function. You
can directly assign the function to the Function GetFullName$() Dim FirstName As String, LastName As String FirstName = "Patricia" LastName = "Katts" GetFullName = LastName & ", " & FirstName End Function Sub Exercise() ActiveCell.FormulaR1C1 = GetFullName() End Sub In the same way, since a function returns a value, you can use it directly in your spreadsheet. To do this, click any box in the work area: After clicking the box, type =, followed by the name of the function. As you are typing the name of the function, Microsoft Excel would present a list of functions that match that name. If you see the name of the function, you can double-click it, or you can just keep typing. After typing the name of the function, type its parentheses, and press Enter or click the Enter button on the Formula Bar.
Depending on an author, in the Visual Basic language, the word "procedure" means either a sub-procedure created with the Sub keyword, or a function created with the Function keyword. In the same way, for the rest of our lessons, the word procedure will be used to represent both types. Only when we want to be precise will we use the expression "a sub-procedure" to explicitly mean the type of procedure that does not return a value. When the word "function" is used in our lessons, it explicitly refers to the type of procedure that returns a value. |
|
|||||||
|
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Next |
|