Table of Contents
hide
Example : A program in VB .Net to show both Subroutine and Function Procedure.
Module Module1
' Function Procedure Example
Function Display(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function
' Sub Procedure Example
Sub Output(ByVal result As Integer)
Console.WriteLine("The result is: " & result)
End Sub
Sub Main()
Dim num1 As Integer = 10
Dim num2 As Integer = 5
Dim product As Integer = Display(num1, num2)
Output(product) ' Output: The result is: 50
End Sub
End Module
Example : A program in VB .Net to show Parameterized Subroutine Procedure.
Module Module1
Sub StringMessage(ByVal name As String)
Console.WriteLine("Hello, " & name)
End Sub
Sub Main()
StringMessage("India")
End Sub
End Module
Example : A program in VB .Net to show Non-Parameterized using Subroutine Procedure.
Module Module1
Sub Message()
Console.WriteLine("Hello India")
End Sub
Sub Main()
Message()
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
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 Exit Sub Statement using 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)
End Sub
End Module
Example : A program in VB .Net to show ByVal (Call by Value) Statement using 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)
End Sub
End Module
Output:
The value after procesing is : 20
Original value before processing : 20
Example : A program in VB .Net to show ByRef (Call by Reference) Statement using 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)
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 to show Exit Function Statement using 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)
End Sub
End Module
Example : A program in VB .Net to show No Parameter and No-Return using Function Procedure.
Module Module1
Function Message()
Console.WriteLine("Hello India")
Return 1
End Function
Sub Main()
Message()
End Sub
End Module
Example : A program in VB .Net to show No Parameter and With-Return using 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())
End Sub
End Module
Example : A program in VB .Net to show With Parameter and No – Return using 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)
End Sub
End Module
Example : A program in VB .Net to show With Parameter and With – Return using Function Procedure.
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))
End Sub
End Module
Example : A program in VB .Net to show Recursive Function using 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)
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)
End Sub
End Module
0 Comments