functions.gif (924 bytes)

Exclamtiub.gif (188 bytes) Functions are used to clear up your code, and well - make things run allot easier and more organized. Len(String) is a function. Well, lets get to it!

where.gif (1245 bytes)

1. On Visual Basics drop down menus, choose PROJECT>>ADD MODULE
2. Now in the project explorer (image) double click the bas file (module)
3. This is now where you would write your functions!

funct_example.gif (805 bytes)

Function Add2numbers(number1 as Integer, number2 as integer) as Integer
Add2numbers = number1 + number2
End Function

 

Exclamtiub.gif (188 bytes) So basically, what this does is well, we tell VB that Add2numbers, the function itself is going to be a variable, holding
number1+number2. We tell it that by adding the "as integer" command after the function declaration. (Yes, you can have
it as String, as Long, as Double, as Currency etc,)

The Breakdown!

1. We tell VB that we are making a function, and that the name of it is Add2numbers, and that it needs 2 arguments "number1" and "number2", then we tell VB that the function itself can hold an integer (as integer)
2. We set our function name "Add2numbers" to the value of number1 + number2.
3. We tell Visual Basic that the Function is over, and were ready to call it and use it in our program. Yes, its that easy.

calling_funct.gif (1846 bytes)

TheSum = Add2numbers(5,4)

Exclamtiub.gif (188 bytes) TheSum now holds the value, "9". since we made
it As Integer, we can use it like this.

Call Msgbox(Add2numbers(5,4),vbOkOnly,"Functions")

!.gif (145 bytes) Now a Message box will appear, with the value "9" in it.

You can also change the value like this:

TheSum = Add2numbers(5,4) + 10

!.gif (145 bytes) Now, TheSum takes on the value of 9, plus 10, which is "19".


boolenas.gif (1364 bytes)

Exclamtiub.gif (188 bytes) Boolean - A variable type used to hold 4 values . TRUE and FALSE. and 0 and 1

Function TheStatus(TheWord as String) as Boolean
If Len(TheWord) < 10 Then TheStatus = True
If Len(TheWord) > 10 Then TheStatus = False
End Function

This little function is going to take a  word in and:

if the word letter count is less than 10, TheWord will be True
if the word letter count is greater then 10, TheWord will be False

after.gif (1132 bytes)

If TheStatus("Nautica") = True Then Call Msgbox("Its true!!!!",vbOkOnly,"nauts-crib.com")
If TheStatus("Nautica") = False Then Call Msgbox("Its false",vbOkOnly,"nauts-crib.com")

POP QUIZ!

Objective:

!.gif (145 bytes) A Function that takes in two integer numbers, if the sum of those numbers exceeds 20 then the Function returns as TRUE.

Information:

Function Name: Exceeds
First Argument: number1
Second Argument: number2

Write the code:

arr.gif (385 bytes) Finished? Click for correct code, and compare!

 

VB_back.gif (1347 bytes)

1999-2000 © Erik Helgesen All Rights Reserved.
Only permitted Images and Material may be downloaded from this web site. 
PhotoShop, Flash, and Microsoft Visual Basic are registered trademarks!