Saturday, December 26, 2009

Defining function

Making a Simple Function


A function is a procedure that executes lines of code and returns a value. The syntax for declaring a simple function is as follows:

[Private|Public] Function FunctionName() As DataType
...lines of code
FunctionName = ReturnValue
End Function

In this syntax

Private|Public are the optional Visual Basic keywords that define the scope of the function.

Function is the Visual Basic keyword that denotes the procedure is a function.

FunctionName is the name that you assign to your function.

As is the Visual Basic keyword that denotes a data type assignment.

DataType is the data type of the value that the function will return.

ReturnValue is the value that you pass back from the function by assigning it to the function's name. (This is very important!)

End Function are the Visual Basic keywords that denote the end of a code block.

The code snippet in Listing 3.2 shows a function, GetNumber(), the purpose of which is to return a number defined within the function itself.


LISTING 3.2 18LIST02.TXT--A Simple Function

01 Public Function GetNumber() As Integer
02 Dim a%
03 Dim b%
04 Dim c%
05 `Assign values to some variables
06 a% = 7
07 b% = 12
08
09 `Add them together
10 c% = a% + b%
11
12 `Pass the result out of the function by assigning
13 `it to the function name.
14 GetNumber = c%
15 End Function

You add a function to your project by using the same two methods that you use to add a sub--by putting it directly into the General Declarations section of the form or module or by using the Add
Procedure dialog. However, be advised that you have to manually add a little code when you add a function to your code by using the Add Procedure dialog

Passing Arguments into Subs and Functions


You can enhance the power and versatility of subs and functions by using arguments. An argument, also referred to as a parameter, is a variable that acts as a placeholder for a value that you'll pass into
the sub or function. You create arguments by placing them within the parentheses of the declaration statement of the sub or function. The following snippet of code shows the declaration for the function
EndDay(), which takes two arguments: one of type Integer and one of type String.

EndDay(NumOne As Integer, strName As String) As Integer

Using arguments greatly increases the reusability of your code. For example, imagine that in many places of your code you need to figure out the greater of two numbers. Every time you need to do this
calculation, you could write out the code, line for line, or you could write out a function that does this for you and then call the function when you need to do the calculation. The advantage of the latter
method is twofold:

One call satisfies many needs throughout your code.

If you need to enhance this functionality, you don't have to go through your code and make enhancements line by line; you simply go back to the function and make the changes within the function's code block.

Listing 3.3 shows the user-defined function GetGreaterNum(), which returns the greater of two numbers passed to it.


LISTING 3.3 18LIST03.TXT--A Simple Function That Takes Arguments

01 Public Function GetGreaterNum(NumOne As Integer, _
NumTwo As Integer) As Integer
02 `If the first number is greater than the second
03 If NumOne > NumTwo Then
04 `return the first number
05 GetGreaterNum = NumOne
06 Else
07 `if not, return the second number
08 GetGreaterNum = NumTwo
09 End If
10 End Function

Listing 3.4 shows the GetGreaterNum() function called from within a Click() event procedure.





LISTING 3.4 18LIST04.TXT--Using a Function Within an Event Procedure

0 1 Private Sub cmdGreaterNum_Click()
02 Dim i%
03 Dim j%
04 Dim RetVal%
05
06 `Get the input in txtNumOne and convert it to an integer
07 i% = CInt(txtNumOne.Text)
08
09 `Get the input in txtNumTwo and convert it to an integer
10 j% = CInt(txtNumTwo.Text)
11
12 RetVal% = GetGreaterNum(i%, j%)
13
14 `Take the result from the function, convert it to a
15 `string and assign it to the caption of the button.
16 cmdGreaterNum.Caption = CStr(RetVal%)
17 End Sub

It's very important when you use subs or functions that the argument's type and order match up. If you have a procedure that has three arguments of type Integer, you must pass in three integers; if you pass
in two Integers and a String, the compiler will throw an error. For example, if you have a function EndDay() declared as follows,

Public Function EndDay(iNum As Integer, dAccount _
As Double) As Double

and call the function by using the following line of code,

dMyResult = EndDay(6, "D56R")

this call generates an error. "D56R" is of type String, but the function is expecting the second argument to be of type Double. For this reason, a variable type declaration error appears.

Also, the argument count must match up. For example, you have a function declared as follows:

Public Function Bar(iNum as Integer, dNum as Double, _
strName as String) as Integer

and you call the function by using the following line of code:

iMyResult = Bar(6, 7)

This call also causes an error. The function expects three arguments, but you've passed in only two. Again, an error occurs.

It's possible to make an argument optional by using the Optional keyword before an argument when you declare the function. If there's an upper limit on the number of arguments that you're going to pass,
you should use the Optional keyword. These arguments must be declared as Variant.

No comments:

Post a Comment