Visual Basic/Procedures and Functions
Functions are named blocks program code that perform a specific task and return a result. The task can be as simple as calculating the sum of two numbers or as complex as launching a spacecraft. A procedure is simply a function that does not return a result, procedures are used for their side effects.
Example function:
Public Function Sum(ByRef a As Double, ByRef b As Double) As Double Sum = a + b Return Sum End Function
Example subroutine:
Public Sub Tell(ByVal s as string) MsgBox s End Sub
You can use these two as follows:
Dim a as Double Dim b as Double Dim c as Double a = 123.456 b = 234 c = Sum(a, b) Tell "The sum of " & a & " and " & b & " is " & c
The result will be a message box showing the text 'The sum of 123.456 and 234 is 357.456' unless your computer is set to use a comma as the decimal separator in which case it will say 'The sum of 123,456 and 234 is 357,456'.
These small fragments exhibit a number of important features:
- The arguments to the function are declared as ByRef which requires the compiler to make sure that only arguments of the specified type are used, in his case Double.
- The function returns a value by assigning it to the function name as though the function were a variable. This contrasts with the use of the keyword return in other languages such as Python.
- VB automatically converts numbers to strings when necessary
- the arguments (argument list) passed to a function must be enclosed in round brackets, a parenthesis, whereas those supplied to a subroutine need not.
Contents |
[edit] How to Write Procedures and functions
As you can see above a function can be quite simple. Still, it is a good idea to be aware of the formalities of writing them.
A procedure has these parts:
- Visibility
- Public, Friend or Private
- Procedure Type
- Sub, Function, Property Let, Property Get, Property Set
- Name
- Anything you like as long as it starts with a letter and contains only letters numbers and underscores.
- Argument List
- a list of the items of data that the procedure needs,
- Return Type
- if the type is Function or Property Get this tells the compiler what kind of thing will be returned, for instance, Double or String.
- Body
- all the statements that do the work.
Only the Name and the Procedure Type are absolutely required but a procedure without a body doesn't do anything!
[edit] Visibility
This seems like a very unimportant part of a procedure declaration to most people but in fact it is a very helpful feature. With it you can show that some procedures are just for use inside a module (Private), some only for use in this component (Friend) or available for the whole world (Public). You should mark procedures Private unless they will be called from outside the module. this will encourage you, and anyone who edits your program, to place related procedures in the same module which obviously makes maintenance easier.
Marking a procedure Private also means that you can have another procedure with exactly the same name in another module.
[edit] Procedure type
All procedures are either functions that return a result as the value of the function or subroutines that are called for their side effects.
Functions are declared like this:
Private Function fHalf(ByRef y as Double) as Double fHalf = y / 2 Return fHalf End Function
Subroutines like this:
Private Sub sHalf(ByRef y As Double, ByRef Result As Double) Result = y / 2 End Sub
The two procedures do essentially the same thing, that is, divide a number by two. The Function version does it by assigning the new value to the name of the function while the Sub version assigns it to the name of one of the arguments. This affects how you use them.
The function version can be used in an expression as follows:
Debug.Print fHalf(10)
Will print:
5
If you want to use the Sub you have to do something like this:
Dim nHalf as Double sHalf 10, nHalf Debug.Print nHalf
Generally you use a Function when the result is a single thing (number, string, object) and a Sub when you either want to return several distinct things or nothing at all.
Properties are also a form of procedure. Property Get is a function, Property Let and Property Set are subroutines. For more discussion of Properties see the Object Oriented Programming chapter.
[edit] Early Termination
Use Exit Function or Exit Sub to terminate a procedure in the middle, like this:
Sub lengthy_computation(fat, n) If n = 0 Or n = 1 Then fat = 1 ' now terminate Exit Sub End If ' now compute fat ... End Sub
| Previous: Data Types | Contents | Next: Windows_Dialogs |