Javatpoint Logo
Javatpoint Logo

VB.NET Sub

A Sub procedure is a separate set of codes that are used in VB.NET programming to execute a specific task, and it does not return any values. The Sub procedure is enclosed by the Sub and End Sub statement. The Sub procedure is similar to the function procedure for executing a specific task except that it does not return any value, while the function procedure returns a value.

Defining the Sub procedure

Following is the syntax of the Sub procedure:

Where,

  • Access_Specifier: It defines the access level of the procedure such as public, private or friend, Protected, etc. and information about the overloading, overriding, shadowing to access the method.
  • Sub_name: The Sub_name indicates the name of the Sub that should be unique.
  • ParameterList: It defines the list of the parameters to send or retrieve data from a method.

The following are the different ways to define the types of Sub method.

Example: Write a simple program to pass the empty, a single or double parameter of Sub procedure in the VB.NET.

Sub_Program.vb

Output:

VB.NET Sub

In the VB.NET programming language, we can pass parameters in two different ways:

  • Passing parameter by Value
  • Passing parameter by Reference

Passing parameter by Value

In the VB.NET, passing parameter by value is the default mechanism to pass a value in the Sub method. When the method is called, it simply copies the actual value of an argument into the formal method of Sub procedure for creating a new storage location for each parameter. Therefore, the changes made to the main function's actual parameter that do not affect the Sub procedure's formal argument.

Syntax:

In the above syntax, the ByVal is used to declare parameters in a Sub procedure.

Let's create a program to understand the concept of passing parameter by value.

Passing_value.vb

Output:

VB.NET Sub

Passing parameter by Reference

A Reference parameter is a reference of a variable in the memory location. The reference parameter is used to pass a reference of a variable with ByRef in the Sub procedure. When we pass a reference parameter, it does not create a new storage location for the sub method's formal parameter. Furthermore, the reference parameters represent the same memory location as the actual parameters supplied to the method. So, when we changed the value of the formal parameter, the actual parameter value is automatically changed in the memory.

The syntax for the passing parameter by Reference:

In the above syntax, the ByRef keyword is used to pass the Sub procedure's reference parameters.

Let's create a program to swap the values of two variables using the ByRef keyword.

Passing_ByRef.vb

Output:

VB.NET Sub





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA