This page is designed for a simple exercise more than anything else. I will show you how to use functions and parameters. We have already used a few functions, such as in the previous exercise and in the circles exercise. This page will introduce function use that is a bit more practical in the programming realm A function, also sometimes called a method, is simply a set of commands that can be called in a program. They can be useful if a program uses the same set of code with different events. Instead of repeating they code frequently, the programmer can simply call the function. A function is usually defined in order to complete a specific task. A function is set up like so:
Scope Function FunctionName(arguments)
End Function
The italic words are in place of what the programmer chooses. Scope is a programming term that determines where a function, variable, object etc. can be invoked. Within Visual Basic, the scopes used are Private and Public. Private functions are declared in the form's General coding area. This makes the function usable through any object on the current form. Public functions are declared in the General Declarations section of a code module. This makes the function usable from any form in the Visual Basic project. Lets try a function.
Set up the form you see at the top of the page.
Name the form frmFunction.
Set the form's BorderStyle property to 1-Fixed Single.
Set the form's MinButton property to True.
Set the form's StartUpPosition property to 2-CenterScreen.
Name the command buttons cmdRaise and cmdExit.
Name the text box txtNumber.
Now let's code the program.
Double-click the cmdExit command button.
Press tab, then type Unload frmFunction.
Go to the form's General Declarations section and
type the following lines:
Private Function Power(intNumber As Integer, intPower As Integer)
Dim sngRaised As Single
Dim intMultiplied As Integer
sngRaised = intNumber
intMultiplied = 1
Do
sngRaised = sngRaised * intNumber
intMultiplied = intMultiplied + 1
Loop Until intMultiplied = intPower
MsgBox Str(intNumber) + " ^" + Str(intPower) + " = " +
Str(sngRaised), vbInformation, ""
End Function
Line 1: Declare function Power(intNumber as Integer, intPower as Integer).
This function is usable through this form because it is declared in the form's General Declarations section. Within the parenthesis is what we call arguments or parameters. These values act as local variables within the function. They cannot be accessed by other functions or sub-procedures, but this function will take values for them when it is called within a sub-procedure or other function. These values are used within the function.
Line 2: Declare variable sngRaised as data-type Single.
Even though we are going to produce integer values (no decimal)
because the initial value is an integer, the data-type Long only stores
up to 2,147,483,647. Data-type single is a whole lot larger
Line 3: Declare variable intMultiplied as data-type Integer.
Line 4: Set variable sngRaised to intNumber (the number
passed to this function).
Line 5: Set variable intMultiplied to 1. This variable will
track how many times the variable sngRaised is multiplied.
Line 6: Begin Do Until loop
Line 7: Multiply variable sngRaised by intNumber.
Line 8: Increment intMultiplied by 1.
Line 9: Tell the loop to keep executing until intMultiplied is
equal to intPower. For instance: 5 ^ 3 multiplies 5 by itself 3
times. The loop multiplies the variables sngRaised and intNumber
until they have been multiplied the amount of times as the value of intPower,
which is specified by the user.
Line 10: Display message box with the MsgBox function to display
results. We need to use the Str function (which treats a number value as
a string) so that the function MsgBox can display the integer variables intNumber,
intPower, and sngRaised because the MsgBox function
requires a string argument. The message is displayed as vbInformation
type message with no title.
Line 11: End the function Power. Just as sub-procedures need End
Sub at the bottom of them, so do functions need End Function.
Now we need to code the command button cmdRaise, where we will call the function that we just created.
In the code window for the command button cmdRaise, press tab, then type the following lines:
Dim intPower As Integer, intNumber As Integer
intNumber = Val(txtNumber.Text)
intPower = InputBox("Enter a number to raise " & intNumber & " to:", "")
Power intNumber, intPower
Line 1: Declare integer variables intPower and intNumber.
Line 2: Set variable intNumber to the value of the text in the
text box txtNumber.
Line 3: Prompt user to enter exponent and store the entry in variable
intPower.
Line 4: Calls function Power, setting the value of intNumber
to the function's intNumber parameter, then setting intPower to
the functions intPower parameter. Calling a function simply executes the
code written in the called function.
Everything should work dandy now. Now we may proceed to Page 29.