What is Sub Procedure in VB.net?
A Sub Procedure in VB.net 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 VB.net Sub Procedures 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.
Difference Between Sub Procedure and Function in VB.net
A sub procedure is a group of VB.net statements. It begins with a Sub keyword and ends with End Sub keywords. A sub procedure is also called a subroutine.
It is used to execute a certain block of statements that consists of the body of the procedure.
It is called explicitly by its name whenever it is required to perform a certain task.
It can be called any number of times. The sub procedure returns control to the calling code after performing a task.
A function procedure is a group of VB.net statements. It begins with a Function keyword and ends with an End Function keyword.
It is generally used to perform a task and return a value back to the calling code.
It may have multiple return points to the calling code.
A part from return stamens, End Function, or Exit function also returns control to the calling procedure.
See also the Complete Topic About the Difference Between Sub Procedure and Function in VB.net.
Defining Sub Procedures in VB.net
A sub procedure’s name, parameters, and body are declared using the Sub statement.
The syntax for the Sub Procedure in VB.net:
'syntax for sub procedure'
[Modifiers] Sub SubName [(ParameterList)]
[Statements]
End Sub
1. Modifiers
Set the procedure’s access level; options include Public, Private, Protected, Friend, and Protected Friend. Include details on overloading, overriding, sharing, and shadowing.
2. SubName
Indicates the name of the Sub.
3. ParameterList
specifies the list of the parameters.
Example Program of Sub Procedure in VB.net:
Example: Write a simple program to pass the empty, single, or double parameter of the Sub procedure in the VB.NET.
Module Sub_Program
Sub sample()
Console.WriteLine("Welcome to IT SOURCECODE")
End Sub
Sub circle(ByVal r As Integer)
Dim Area As Integer
Const PI = 3.14
Area = PI * r * r
Console.WriteLine(" Area Of circle is : {0}", Area)
End Sub
' Create the SumOfTwo() Function and pass the parameters.
Sub SumOfTwo(ByVal n1 As Integer, ByVal n2 As Integer)
' Define the local variable.
Dim sum As Integer = 0
sum = n1 + n2
Console.WriteLine(" Sum of two number is : {0}", sum)
End Sub
Sub SubtractionOfTwo(ByVal n1 As Integer, ByVal n2 As Integer)
' Define the local variable.
Dim subtract As Integer
subtract = n1 - n2
Console.WriteLine(" Subtraction of two number is : {0}", subtract)
End Sub
Sub MultiplicationOfTwo(ByVal n1 As Integer, ByVal n2 As Integer)
' Define the local variable.
Dim multiply As Integer
multiply = n1 * n2
Console.WriteLine(" Multiplication of two number is : {0}", multiply)
End Sub
Sub Main()
' Define the local variable a, b and rad.
Dim a, b, rad As Integer
sample() ' call sample() procedure
Console.WriteLine(" Please enter the First Number : ")
a = Console.ReadLine()
Console.WriteLine(" Second Number is : ")
b = Console.ReadLine()
SumOfTwo(a, b) 'call SumOfTwo() Function
SubtractionOfTwo(a, b) 'call SubtractionOfTwo() Function
MultiplicationOfTwo(a, b) 'call MultiplicationOfTwo() Function
Console.WriteLine(" Enter the radius of circle : ")
rad = Console.ReadLine()
circle(rad)
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Welcome to IT SOURCECODE
Please enter the First Number :
5
Second Number is :
5
Sum of two number is : 10
Subtraction of two number is : 0
Multiplication of two number is : 25
Enter the radius of circle :
20
Area Of circle is : 1256
Press any key to exit…
You can test the above example here! ➡ VB.net Online Compiler
In the VB.net programming language, we can pass parameters in two different ways:
- Passing parameter by Value
- Passing parameter by Reference
Passing Parameters by Value in VB.net
The default approach for sending a value in the Sub method in VB.NET is to pass a parameter by value.
The method merely creates a new storage place for each parameter when it is called, copying the actual value of each argument into the formal method of the Sub process.
Consequently, the modifications made to the actual parameter of the main function do not change the formal argument of the Sub process.
The syntax for Passing Parameters by Value in VB.net:
'syntax for passing parameters by value'
Sub Sub_method( ByVal parameter_name As datatype )
[ Statement to be executed]
End Sub
In the above syntax, the ByVal is used to declare parameters in a Sub procedure.
Example Program of Passing Parameter by Value in VB.net:
Module paramByval
Sub swap(ByVal x As Integer, ByVal y As Integer)
Dim temp As Integer
temp = x ' save the value of x
x = y ' put y into x
y = temp 'put temp into y
End Sub
Sub Main()
' local variable definition
Dim a As Integer = 100
Dim b As Integer = 200
Console.WriteLine("Before swap, value of a : {0}", a)
Console.WriteLine("Before swap, value of b : {0}", b)
' calling a function to swap the values '
swap(a, b)
Console.WriteLine("After swap, value of a : {0}", a)
Console.WriteLine("After swap, value of b : {0}", b)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 100
After swap, value of b : 200
You can test the above example here! ➡ VB.net Online Compiler
Passing Parameters by Reference in VB.net
A VB.net Reference Parameter is a memory location’s reference to a variable.
When using ByRef in the Sub procedure, the Reference Parameter in VB.net is utilized to pass a variable’s reference.
A new storage location for the formal argument of the sub-method is not created when a reference parameter is given.
Furthermore, the actual parameters supplied to the method’s reference parameters represent the same memory address.
As a result, whenever we alter the formal parameter’s value, the memory immediately updates to reflect the new value.
Example Program for Passing Parameters by Reference in VB.net:
Module paramByref
Sub swap(ByRef x As Integer, ByRef y As Integer)
Dim temp As Integer
temp = x ' save the value of x
x = y ' put y into x
y = temp 'put temp into y
End Sub
Sub Main()
' local variable definition
Dim a As Integer = 100
Dim b As Integer = 200
Console.WriteLine("Before swap, value of a : {0}", a)
Console.WriteLine("Before swap, value of b : {0}", b)
' calling a function to swap the values '
swap(a, b)
Console.WriteLine("After swap, value of a : {0}", a)
Console.WriteLine("After swap, value of b : {0}", b)
Console.ReadLine()
End Sub
End Module
When the above code is compiled and executed, it produces the following result:
Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 200
After swap, value of b : 100
You can test the above example here! ➡ VB.net Online Compiler
Summary
In this tutorial Sub Procedure in VB.net, we discuss its uses and importance in a program, this tutorial also includes an Example Program to be executed.
This tutorial is good for beginners who want to learn programming, especially VB.net Programming Language.
PREVIOUS
NEXT