Table of Contents
hide
Example : A program in VB .Net 2013 to show a Subroutine/Sub Procedure.
Module Module1
Dim x, y, z As Integer
Sub Output()
x = 10
y = 20
z = x + y
'Console.WriteLine("The addition result is = " & z)
Console.WriteLine("The addition result of " & x & " and " & y & " is = " & z)
End Sub
Sub Main()
Output()
Console.ReadKey()
End Sub
End Module
Output: The addition result of 10 and 20 is = 30
NB: Subroutine(sub) procedure never return a value like a Function in VB .Net.
--------------- OR ---------------
Imports System
Module Module1
Dim x, y, z As Integer
Sub Output()
Console.Write("Enter First Value = ")
x = Console.ReadLine()
Console.Write("Enter Second Value = ")
y = Console.ReadLine()
z = x + y
Console.WriteLine("The addition result is = " & z)
End Sub
Sub Main()
Output()
Console.ReadKey()
End Sub
End Module
--------------- OR ---------------
Imports System
Module Module1
Dim x1, y1, z As Integer
Sub Output(ByVal x2 As Integer, ByVal y2 As Integer) 'Parameterized Subroutine.
x1 = x2
y1 = y2
z = x1 + y1
Console.WriteLine("The addition result is = " & z)
End Sub
Sub Main()
Output(10, 20)
Console.ReadKey()
End Sub
End Module
--------------- OR ---------------
Imports System
Module Module1
Sub Output(ByVal x2 As Integer, ByVal y2 As Integer) 'Parameterized Subroutine.
Console.WriteLine("The first value is = " & x2)
Console.WriteLine("The second value is = " & y2)
End Sub
Sub Main()
Output(10, 20)
Console.ReadKey()
End Sub
End Module
--------------- OR ---------------
Imports System
Module Module1
Dim x, y, z As Integer
Sub Output(ByVal a As Integer, ByVal b As Integer)
z = a + b
Console.WriteLine("The addition result is = " & z)
End Sub
Sub Main()
Console.Write("Enter First Value = ")
x = Console.ReadLine()
Console.Write("Enter Second Value = ")
y = Console.ReadLine()
Output(x, y)
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net to show a Parameterized Subroutine Procedure.
Module Module1
Sub StringMessage(ByVal name As String)
Console.WriteLine("Hello, " & name)
End Sub
Sub Main()
StringMessage("India")
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net 2013 to show a Non-Parameterized String value using a Subroutine Procedure.
Module Module1
Sub Message()
Console.WriteLine("Hello India")
End Sub
Sub Main()
Message()
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net to show Optional and Default Parameters using Subroutine Procedure.
Module Module1
Sub Example(Optional ByVal name As String = "India")
Console.WriteLine("Hello, " & name)
End Sub
Sub Main()
Example() ' Output: Hello, India [default parameter]
Example("World") ' Output: Hello, World
Console.ReadKey()
End Sub
End Module
NB:
Parameters in a sub procedure can be optional and can have default values.
Example : A program in VB .Net to show the Exit Sub Statement using a Sub Procedure.
Module Module1
Sub CheckAge(ByVal age As Integer)
If age < 18 Then
Console.WriteLine("Not Eligible for Vote!")
Exit Sub
End If
Console.WriteLine("Eligible for Vote")
End Sub
Sub Main()
CheckAge(50)
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net 2013 to show the ByVal (Call by Value) Statement using a Sub Procedure.
Module Module1
' Define a Sub that uses ByVal
Sub Calculation(ByVal num As Integer)
Dim x As Integer = num
Console.WriteLine("The value after procesing is : " & x)
End Sub
Sub Main()
' Declare a variable
Dim Value As Integer = 20
' Call the Sub
Calculation(Value)
' Print the original value after the Subroutine call
Console.WriteLine("Original value before processing : " & Value)
Console.ReadKey()
End Sub
End Module
Output:
The value after procesing is : 20
Original value before processing : 20
Example : A program in VB .Net to show the ByRef (Call by Reference) Statement using a Sub Procedure.
Module Module1
' Define a Sub that uses ByRef statement
Sub UpdateValue(ByRef num As Integer)
' Modify the original variable
num = num + 50
Console.WriteLine("Value inside the Sub (ByRef): " & num)
End Sub
Sub Main()
' Declare an integer variable
Dim originalValue As Integer = 50
' Print the original value before calling/using the Sub
Console.WriteLine("Original value before Sub call: " & originalValue)
' Call the Sub with ByRef
UpdateValue(originalValue)
' Print the original value after calling the Sub
Console.WriteLine("Original value after Sub call: " & originalValue)
Console.ReadKey()
End Sub
End Module
Output:
Original value before Sub call: 50
Value inside the Sub (ByRef): 100
Original value after Sub call: 100
Example : A program in VB .Net 2013 to show the Exit Function Statement using a Function Procedure.
Module Module1
Function CheckAge(ByVal age As Integer)
If age < 18 Then
Console.WriteLine("Not Eligible for Vote!")
Exit Function
End If
Console.WriteLine("Eligible for Vote")
End Function
Sub Main()
CheckAge(10)
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net to show No Parameter and No Return using a Function Procedure.
Module Module1
Dim x As Integer = 100
Function Output()
Console.WriteLine(x)
Return 0
'Return 1
'Return 10
End Function
Sub Main()
Output()
Console.ReadKey()
End Sub
End Module
Output: 100
--------------- OR ---------------
Module Module1
Function Message()
Console.WriteLine("Hello India")
Return 1
End Function
Sub Main()
Message()
Console.ReadKey()
End Sub
End Module
NB: Function must Return a value either 0,1 or numeric output or any other numeric value in VB .Net 2013. Only Return statement not allowed.
Example : A program in VB .Net to show No Parameter and With Return using a Function Procedure.
Module Module1
Function Calculation()
Dim x = 10, y = 20, z As Integer
z = x + y
Return z
End Function
Sub Main()
Console.WriteLine("The addition result is = " & Calculation())
Console.ReadKey()
End Sub
End Module
--------------- OR ---------------
Module Module1
Dim k As Integer
Function Calculation()
Dim x = 10, y = 20, z As Integer
z = x + y
Return z
End Function
Sub Main()
k = Calculation()
Console.WriteLine("The addition result is = " & k)
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net to show With Parameter and No Return using a Function Procedure.
Module Module1
Function Calculation(ByVal x As Integer, ByVal y As Integer)
Dim z As Integer
z = x + y
Console.WriteLine("The addition result is = " & z)
End Function
Sub Main()
Calculation(10, 20)
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net to show With Parameter and With Return using a Function Procedure.
Module Module1
Dim k As Integer
Function Calculation(ByVal x As Integer, ByVal y As Integer)
Dim z As Integer
z = x + y
Return z
End Function
Sub Main()
k = Calculation(10, 20)
Console.WriteLine("The addition result is = " & k)
Console.ReadKey()
End Sub
End Module
--------------- OR ---------------
Module Module1
Function Calculation(ByVal x As Integer, ByVal y As Integer)
Dim z As Integer
z = x + y
Return z
End Function
Sub Main()
Console.WriteLine("The addition result is = " & Calculation(10, 20))
Console.ReadKey()
End Sub
End Module
--------------- OR ---------------
Module Module1
Dim k, a, b As Integer
Function Calculation(ByVal x As Integer, ByVal y As Integer)
Dim z As Integer
z = x + y
Return z
End Function
Sub Main()
Console.Write("Enter First Value = ")
a = Console.ReadLine()
Console.Write("Enter Second Value = ")
b = Console.ReadLine()
k = Calculation(a, b)
Console.WriteLine("The addition result is = " & k)
Console.ReadKey()
End Sub
End Module
Example : A program in VB .Net to show a Recursive Function using a Function Procedure.
Module Module1
Function Factorial(ByVal n As Integer) As Integer
If n = 0 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
End Function
Sub Main()
Dim result As Integer = Factorial(5)
Console.WriteLine("The factorial result is: " & result)
Console.ReadKey()
End Sub
End Module
Module Module1
Class Example
Shared Function Square(ByVal num As Integer) As Integer
Return num * num
End Function
End Class
Sub Main()
Dim result As Integer = Example.Square(5)
Console.WriteLine("The square value is = " & result)
Console.ReadKey()
End Sub
End Module
![]()
0 Comments