VB.NET SubA 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 procedureFollowing is the syntax of the Sub procedure: Where,
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: In the VB.NET programming language, we can pass parameters in two different ways:
Passing parameter by ValueIn 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: Passing parameter by ReferenceA 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.vbOutput: Next TopicVB.NET Classes and Object |