Sub Procedure Vs Function in VB.NET
Last Updated :
11 May, 2022
.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. |
---|
Similar Reads
Local Function in C# The local function feature is introduced in C# 7.0. It allows you to declare a method inside the body of an already defined method. Or in other words, we can say that a local function is a private function of a function whose scope is limited to that function in which it is created. The type of loca
4 min read
Difference Between VB.NET and Visual Basic VB.NET stands for Visual Basic. Network Enabled Technologies. Microsoft released the .NET platform in 2001, that supports Visual Basic .NET which is an upgrade to the last version of VB programming language. It is a high-level programming language for the Microsoft .NET Framework. It is also possibl
2 min read
LINQ | Filtering Operator | OfType Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21. So, we filter the record, according to thei
3 min read
Difference Between ASP.NET and VB.NET ASP stands for Active Service Pages. ASP.NET is an open-source, server-side scripting web application designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic websites, applications, and servers. It works on top of the HTTP protoc
2 min read
C# | Substring() Method In C#, Substring() is a string method. It is used to retrieve a substring from the current instance of the string. This method can be overloaded by passing the different number of parameters to it as follows: String.Substring(Int32) Method String.Substring(Int32, Int32) Method String.Substring Metho
3 min read
Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
Partial Methods in C# C# contains a special method is known as a partial method, which contains declaration part in one partial class and definition part in another partial class or may contain both declaration and definition in the same partial class. Basically, partial methods exist in the partial class, or in the stru
2 min read
Difference Between VB.NET and C# Visual Basic .NET is a high-level programming language that was initially developed in 1991. It was the first programming language that directly supported programming graphical user interfaces using language-supplied objects. It supports all the concepts of an object-oriented such as object, class,
2 min read
SByte.CompareTo() Method in C# with Examples SByte.CompareTo() Method is used to compare the current instance to a specified object or SByte and returns an indication of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(SByte) Method CompareTo(Object) Method SByte.CompareTo(SByte) Method This
4 min read
Difference Between VB.NET and Java Visual Basic .NET is a high-level programming language that was initially developed in 1991 run on .NET platforms. It was the first programming language that directly supported programming graphical user interfaces using language-supplied objects. It supports all the concepts of an object-oriented s
2 min read