VB.Net

VB.Net Subs Tutorial

VB.Net Subs Tutorial

A Sub procedure is a reusable block of code that performs a specific task without returning a value. Instead of writing the same code multiple times, you can place it inside a Sub procedure and call it whenever needed. In this VB.NET Subs Tutorial, you'll learn how to create and call Subs, understand their syntax, and explore a simple example.

What is a Visual Basic Sub?

A Visual Basic Sub is declared using the Sub and End Sub statements. Unlike a Function, which returns a value, a Sub simply performs an action and then returns control to the calling code. Sub procedures are commonly used to display messages, process user input, or perform repeated operations.

VB.NET Sub Syntax

The basic syntax of a Sub procedure is:

Sub DisplayMessage()


    Console.WriteLine("Welcome to VB.NET")


End Sub

This example defines a Sub named DisplayMessage that prints a message to the console.

Visual Basic Sub Example

Once a Sub is created, you can call it from another procedure such as Main().

Module Program


    Sub DisplayMessage()

        Console.WriteLine("Learning VB.NET Subs")

    End Sub


    Sub Main()

        DisplayMessage()

    End Sub


End Module

In this Visual Basic Sub example, Main() calls DisplayMessage(), which executes the code inside the Sub procedure.

Sub vs Function

Although both are reusable procedures, they serve different purposes.

Sub

Function

Does not return a value

Returns a value

Performs an action

Performs a calculation or operation and returns the result

Declared using Sub

Declared using Function

Use a Sub when you only need to perform an action, and use a Function when you need to return a value.

Best Practices

  • Use meaningful names such as DisplayMessage() or PrintReport().

  • Keep each Sub focused on one specific task.

  • Reuse Subs instead of duplicating code.

  • Pass parameters when a Sub needs input instead of relying on global variables.

Learning VB.NET Subs helps you organize your programs into smaller, reusable procedures, making your code easier to read, test, and maintain. Next, continue with VB.NET Classes and Objects to learn how procedures work within object-oriented programming.


Frequently Asked Questions

What is the difference between a Sub and a Function?

A Sub (subroutine) performs an action but does not return a value, while a Function performs an action and returns a value to the caller.

How do I declare a Sub procedure in VB.NET?

Sub procedures are declared using the "Sub" and "End Sub" statements, following the format "Sub ProcedureName(parameters) ... End Sub."

How do I handle code that is too long for one line in VB.NET?

If a statement is too long to fit on a single line, you can break it up by ending the line with an underscore character ( _ ) as a line continuation symbol.

How do I call a Sub procedure in VB.NET?

You can call a Sub procedure by writing its name followed by parentheses containing any required arguments, such as calling DisplayMessage() within the Main() procedure.

Can I pass parameters to a Sub procedure in VB.NET?

Yes, you can pass parameters to a Sub procedure by declaring them inside the parentheses in the Sub statement, which allows you to pass input data instead of relying on global variables.


Top course recommendations for you

    Docker Projects
    2 hrs
    Intermediate
    5.7K+ Learners
    4.65  (144)
    Competitive Programming Course
    2 hrs
    Beginner
    11.7K+ Learners
    4.16  (91)
    Android Studio Tutorial
    1 hrs
    Beginner
    16.8K+ Learners
    4.43  (1023)
    Web Scraping with Python
    1 hrs
    Beginner
    18.6K+ Learners
    4.44  (1063)
    Visual Graphics in C
    2 hrs
    Intermediate
    18.5K+ Learners
    4.49  (856)
    Swift Tutorial
    2 hrs
    Beginner
    4.7K+ Learners
    4.49  (205)
    Systematic Inventive Thinking Innovations
    1 hrs
    Beginner
    2.8K+ Learners
    4.53  (160)
    React JS Tutorial
    2 hrs
    Beginner
    85.7K+ Learners
    4.5  (5297)
    Linux Tutorial
    2 hrs
    Beginner
    70.3K+ Learners
    4.48  (5775)