Visual Basic Tutorial - Part 5, Page 3
Functions are probably the most important thing you'll learn in this entire course. They're useful, they're efficient, they're clever - but they take a fair while to get your head around.
What is a function and how is it different from the standard "sub"?
A function is really just an over-rated Sub. Remember that a Sub is just a piece of code that runs. It can also accept "arguments", such as a number or string of words that it processes in its code.
A function is just the same - except it returns a value.
For instance, I may have an application that takes two numbers from two different text boxes, adds them together and then displays the result in a message box. No problem there.
My form looks something like this -
The code behind my first command button might look something like this -
MsgBox Text1.Text & " plus " & _ Text2.Text & " equals " & Text1.Text + Text2.Text
That's great. Now my code behind the second command button would look something like this -
MsgBox Text3.Text & " plus " & _ Text4.Text & " equals " & Text3.Text + Text4.Text
Now on a form with four different command buttons, a line of code similar to the above would have to be added behind each command button.
What if my boss approached me and requested the two numbers be multiplied by each other, as opposed to simple addition? I would have to change the code behind every single command button.
That's pretty simple when you're only adding a couple of numbers together. But when you've got a complicated piece of code that changes values in databases, performs complex calculations and calls Windows API functions - you really don't want to have to change all that code behind every single command button.
Wouldn't it be great if you could create one generic piece of code that handled the core processing - plus took into account any individual needs? That's where functions come into play.
Before we look at a sample snippet, bear this in mind - a function always returns a value. All will become clear -
Public Function AddThemUp(NumberOne as Integer, NumberTwo as Integer) AddThemUp = NumberOne + NumberTwo End Function
I'll explain this example function step-by-step. The first line is pretty similar to the first line of a Sub. It simply states this is a function called AddThemUp which accepts two arguments, NumberOne and NumberTwo - both of which are integers, aka numbers.
The second line does the actual work. It says that the "result" of this function is equal to NumberOne plus NumberTwo.
The last line merely defines the end of the function.
Let's say we added this function to the code underneath our form, then added a command button with the code:
MsgBox AddThemUp(1, 2)
Upon clicking the button, we would get a message box displaying the number three.
Let's run over how this is happening. In our MsgBox code, we're telling VB to run the AddThemUp code, supplying it with the required arguments - NumberOne and NumberTwo - which are the numbers 1 and 2 respectively.
When the code finishes running, it sets the value of the function to our calculation's result. Remember I said a function returns a value? Our code simply sets that function value with the code "AddThemUp = Whatever".
Then whenever the function is referenced with a piece of code like "MsgBox AddThemUp(1, 2)" - the AddThemUp bit runs the function and returns the value.
Try building an application that uses the function we created to solve our initial problem with the numerous text boxes and four command boxes.
In the code behind the command button you can put something like -
MsgBox Text1.Text & " plus " & _ Text2.Text & " equals " & AddThemUp(Text1.Text, Text2.Text)
So if my boss now requests the two numbers be multiplied by each other - I only need to change the code behind one function as opposed to four command buttons.