Functions are similar to procedures but always return a value of some sort. If a procedure does not return a value then it can just be called, but with functions the value passed back must be handled some how.
A useful function to start with is Time. This role of this function is to return the current system clock time; it does not take any parameters. To try this function out create a new label control on a suitable form. Next place a Timer control on the form as well and set its Interval to 1000. Double click on the Timer control and enter the line Label1.Caption = Time. When you run the program you should see the label control showing the time updated every second.
The above Time function is simple to use because it does not require any parameters. However, most functions require parameters to supply key information to the module in order for it to work out the return value. The Time function does not require a parameter because all it needs to do is interogate the system clock. The function UCase is a simple function requiring only one parameter to be passed. To pass variables and constants as parameters they must be enclosed in brackets after the function name. Multiple parameters must be separated by commas. Create a text control on a form and then in the KeyPress event write: If KeyAscii = 13 then Text1.Text = UCase(Text1.Text). Run the program, enter some text and press RETURN. The result should be that the text in the text box turns to upper-case.
In example 2 the function returned different text depending on the string sent to it in the parameter. Other functions require additional parameters to tell it how to handle the other parameters. For example, the function Mid$ takes one parameter to pass in the text to be used, one parameter to set the starting point (measured in characters), and then a third and optional parameter to specify how many characters to return. We can use this Mid$ function to return the word 'University' out of the string 'Napier University, Edinburgh'. In the Form Activate event enter the following code: Label1.Caption = Mid$("Napier University, Edinburgh", 8, 10). When run all that will be displayed in Label1 will be the word 'University'. Try leaving out the last parameter which is optional and see what happens.
Can the returning value of one function be used as the input parameter for another? The answer is yes, the example below does just that.
Suppose we need to build an input text box on a form to enter a person's name but later in the program we do not want to use the full name just the surname. An easy (or perhaps lazy) solution would be to use two separate text box controls, one for the first name and the other for the surname. However, this uses up more screen real estate and could be slower to enter. Where possible it is better to get the computer to do the hard work instead of the user. To do this we are going to create only one text input box and use a label control to output the result of the function. In the KeyPress event of the text box place the following code:
Private Sub Text1_KeyPress (KeyAscii As Integer) Dim strFullName, strSurname As String If KeyAscii = 13 Then strFullName = Text1.Text strSurname = Mid$(strFullName, InStr(1, strFullName, " ") + 1) Label1.Caption = "Surname = " + UCase(strSurname) End If End Sub
This example uses three of Visual Basic's built in functions: Mid$, InStr, and UCase. The function Mid$ in this case is using two parameters (the option third parameter is not being used). However, the second parameter which sets the starting character has been replaced by the function InStr which is used to return the position of located characters. In this case InStr is looking for the first occurrance of the character " " (space). We will use this to determine where the first name ends and the surname begins. One is added to this returning value because we do not want to return the space, we want to start one letter after the space. In Visual Basic an functions that return values that will be used as input parameters to other functions are evaluated first. The example above uses two local varaibles to try and make the code look less complicated and make it easier to read. However, it could be rewritten so that the the result from the Mid$ function is used as the input to UCase:
Private Sub Text1_KeyPress (KeyAscii As Integer) If KeyAscii = 13 Then Label1.Caption = UCase(Mid$(Text1.Text, InStr(1, Text1.Text, " ") + 1)) End Sub
This shortened example will do exactly the same as the previous example except the label will not show 'Surname = ' before displaying the name. It should be noted that although both examples perform the same job the first is much more readable and thus less prone to bugs. When too many functions are used on a single line it is difficult matching closing brackets. If you find you are using too many functions on a single line split them up over several lines with the help of some temporary local varaibles.
If Visual Basic does not have a suitable built in function then it is possible to build your own. To create a user defined function open up a code window and move the cursor down to the bottom past the last End Sub statement. Here, enter Function function_name (). Replace the word 'function_name' with the exact name you wish to call the function. After pressing RETURN the code window should clear and display the new function on its own with the added line End Function at the end.
In this example we will write our own function to return the current time in New York. We will create a new form with a label control and a command button control on it (see below).
Using the method outlined above create a new function with the code shown below:
Function NY_time () Dim strTempTime As String Dim intNY_hour As Integer strTempTime = Time$ intNY_hour = Val(Left$(strTempTime, 2)) - 5 Rem Correct for passing back over a day If intNY_hour < 0 Then intNY_hour = intNY_hour + 24 intNY_time = Str(intNY_hour) + Right$(strTempTime, 6) End Function
Next double click on the command button and enter the following code:
Private Sub cmdGetTime_Click () lblTime.Caption = "Time in New York is " + intNY_time() End Sub
This function is quite simple it just reads the system clock and takes five hours off the hour. A check to see if the hour is less than zero is require in case the time in New York is the evening before.
Exercise: Try expanding this function to make it more generalisable. Create a parameter to pass in the time zone difference (in hours) from Greenwich.
Below are listed some of the more important functions in Visual Basic. To find out exactly how many parameters each function requires and what type of data the parameter should be use the online help within Visual Basic.