Visual Basic Programming

Page 28

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.

Now let's code the program.

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.

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.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30