Methods are an easy way to organize your code. Methods can also be helpful because you may have a section of code that needs to be used more than once, depending on a user's action. Methods, along with functions, are essential for object-oriented programming.

In Visual Basic there are two types of methods: subroutines and functions. The difference is very easy to understand. When you call a function, it will return a value to you. However, when you call a subroutine, it will only execute code, and no value will come back.

For example, you may have two Methods. One may be called "getFullName" and one could be called "setFullName." getFullName would return to you your full name. However, setFullName would more likely allow you to set your name. Set doesn't need to return your name, only run code to set your name. However, getFullName is REQUIRED to return a value (your name). Let's look at some examples:

Private Function getFullName() As String
  Dim FullName As String = "Jacob A. Morris"
  Return FullName
End Function

If I were to call this Function from another Method, it would return my full name as the value. Notice that "Private" means you cannot access this function from another Class. The "As String" Is declaring what type of value this function will return. You use the word "Return" followed by the value (or variable) to actually return the value. Lastly, you must show that the function has ended in Visual Basic by writing "End Function." Now let's take a look at a subroutine.

Private Sub setFullName(ByVal Name As String)
  Dim FullName As String
  FullName = Name
End Sub

Notice in this example that we use the word "Sub" rather than "Function." This is to show that it is a subroutine. We also use the corresponding "End Sub." Notice that there is no Return, as well as no type to return. Inside of the parentheses is a variable that I can pass to the subroutine. These are called Parameters, and we will discuss them next.

You can call a method by writing the method's name, followed by parentheses. If the method takes a parameter, you must give the parameter, as well. For example:

setFullName("Jacob Morris")

This would set the FullName variable to "Jacob Morris" by calling the subroutine setFullName. Here is how you could call the function.

Dim strName As String
strName = getFullName()

This would set the strName variable to "Jacob A. Morris," because that is what the function would return. Notice that just because a function returns a value does not mean you must set a variable to that value. For example, I could call the function like this:

getFullName()

Although it returned my name, I don't necessarily have to set a variable to my name, or even use it in any way.

Next, we will learn about Parameters and Method Overloading.

Previous Next

Startup Discount
Small Business?
Contact us for an Enormous Discount!