Using Subroutines

Using Subroutines you can divide and reuse small programs of code. A Subroutine - also known as a Sub has a unique name, just like a variable but unlike a variable, a Sub doesn't hold data. When the Sub is executed, it is said to be 'called'. One Sub can "call" another Sub. Some Subs are called automatically when certain actions are performed. For example, the Form_Load Sub is automatically called when a form loads.

How to create a Subroutine

Creating and using Subroutines is very easy. To create you need to type Sub and a space, and then give a name. This 'Sub SubName' will be the start of your code and once you press enter, VB will automatically insert an End Sub statement below. As the statement suggests, the End Sub line is the end of the Sub. Here is an example.Sub DisplayMessage()
  Msgbox "This subroutine just displays a message box!"
End Sub

What are Arguments?

While using Subroutines you can pass Arguments to it. An 'Argument' in visual basic is nothing close to its actual meaning in the English language. Essentially, in visual basic, an argument is a data supplied to the Sub to fulfill parameters that need to be met. An example will give you a good clarification.

Sub GetFullName(FirstName as String, LastName as String)
  Msgbox "The full name of the person is " & FirstName & " " & "LastName"
End Sub
In the above example, you can see that the First and Last names are taken as arguments in the Sub. They are then used in the Msgbox command to display the full name of the person.

Calling one Sub from another

You can call a Sub from many other Subs as many times as required. Calling one Sub from another is also called unconditional branching. This is because you are branching from the calling Sub without any conditions being evaluated. Of course, both conditional & unconditional branching statements are used in tandem on numerous occasions to get desired outputs. Let's see an example.Sub RunAllTasks()
  OpenFile()
  ProcessData()
  SaveFile()
End Sub
As you can see in the above example, the RunAllTasks Sub is just collating all the other tasks. It first runs a Sub that opens a file. Then it runs another Sub that processes the data and finally it runs the Sub that saves the file. Breaking your code into smaller parts and making it easier to understand is always advisable. It is a sign of a good programmer!

Calling one Sub from another with arguments

We can not only call one Sub from another, but we can also pass arguments while doing so. This is really helpful to maintain data throughout all the Sub. An example is shown below:Sub DisplayName()
  GetFullName ("Pete", "Sampras")
End Sub

Sub GetFullName(FirstName as String, LastName as String)
  Msgbox "The full name of the person is " & FirstName & " " & "LastName"
End Sub
Notice here that in the DisplayName Sub, we are calling the GetFullName Sub by passing arguments to it. So, when the DisplayName sub is run, it will give the output on the screen as "Pete Sampras". Once the called Sub reaches its last line, the control is returned to the original Sub and the further statements if any are executed.

The main purpose of using subroutines is breaking important tasks into different blocks. When you break your code into smaller parts, it becomes more efficient and easy to understand for others.

Back to Visual Basic for Beginners