.NET is a software framework that is designed and developed by Microsoft. The first version of the .Net framework was 1.0 which came in the year 2002. In other words, it is a virtual machine for compiling and executing programs written in different languages like C#, VB.Net, etc.
Sub Procedures:
A subprocedure is a group of VB.NET statements. It begins with a Sub keyword and ends with End Sub keywords. A subprocedure is also called a subroutine. It is used to execute a certain block of statements consists 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 subprocedure returns control to the calling code after performing a task.
Structure of Subprocedure:
Sub <subname> [(parameter list)]
Vb statements
End Sub
Example:
Module module1 Sub SubDivide(ByVal num1 As Integer, ByVal num2 As Integer) Dim res As Integer If (num2 <> 0) Then res = num1/num2 Console.WriteLine("Divide by Zero is possible") Else Console.WriteLine("Divide by Zero is undefined") End If End Sub Sub Main() Dim a As Integer Dim b As Integer Dim res As Integer Console.Write("Enter Number 1") a = Console.ReadLine() Console.Write("Enter Number 2") b = Console.ReadLine() SubDivide(a, b) Console.WriteLine(res) End Sub End Module
Output:
Function Procedures:
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.
Structure of Function Procedure:
Function <Functionname> [(parameter list)] As return type
Vb statements
End Function
Example:
Module module1 Function FunctionDivide(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Dim res As Integer If (num2 <> 0) Then res = num1/num2 return res Else Console.WriteLine("Divide by Zero is undefined") End If End Function Sub Main() Dim a As Integer Dim b As Integer Dim res As Integer Console.Write("Enter Number 1") a = Console.ReadLine() Console.Write("Enter Number 2") b = Console.ReadLine() res = FunctionDivide(a, b) Console.WriteLine(res) End Sub End Module
Output:
Comparison between SubProcedure and Function:
Parameters | Sub Procedures | Functions |
---|---|---|
1 | A subprocedure is not associated with an event. | A function is also not associated with an event. |
2 | A subprocedure is called, whenever it is required to perform certain tasks. It returns control to the calling code after performing a task. | A function is called, whenever a value is required to be returned to the calling code after performing a task. |
3 | A subprocedure does not return a value to the calling code. | Functions return a value to the calling code. |
4 | A sub procedure cannot be used with an expression. | Functions are used in an expression. |
5 | Subprocedure helps to make the code readable, and easy to modify and debug. | In functions, it is not easy to modify and debug the code. |
6 | Sub procedure is a generalized type of function. | A function is a specific type of procedure |
7. | A subprocedure is declared with the keyword Sub. | A function is declared with the keyword Function. |