Learn to program in Visual Basic .NET, the right way

Methods - Sub Routines

What is a Sub Routine?

A Sub Routine, also known as the shortened version 'Sub', is a block of code that does not return a value.

In prior lessons, a Sub was used to print String values. The Sub we used in earlier lessons was the Console.WriteLine() Sub Routine.

Creating a Sub

A Sub is similar to a Function in that you specify the:

  • Scope (optional)
  • Name

The syntax for declaring a Sub is as described in the following code block:

[modifier] Sub [name]()

End Sub

This code example is not the actual code. Everything in the square brackets is a specific component that ultimately make up the Sub.

Modifier

Modifiers determine at what scope a Sub can be accessed at. In later lessons, modifiers will be described more in depth, but for now the modifier will be omitted in examples.

Name

The name is user defined and follows the same naming conventions that a variable and Function follows.

Example

The following demonstrates how to create a Sub Routine:

Fiddle: Live Demo