Procedures and Functions in VB.NET Sub
In VB.NET, procedures are code blocks that perform an action but do not return a value. Functions are code blocks that perform an action and return a value. In this page, we will discuss Sub procedures in VB.NET.
Syntax
A Sub procedure is defined using the Sub
keyword followed by the name of the procedure and the parameter list. Here is the syntax for defining a Sub procedure:
Sub procedureName(parameters)
' Code block here
End Sub
Sub procedures can be called from other parts of the program by using their name and passing in any required arguments.
Example
Here's an example of a Sub procedure that takes in two integers and writes their sum to the Console:
Sub AddNumbers(ByVal a As Integer, ByVal b As Integer)
Console.WriteLine("The sum is: " & (a + b))
End Sub
We can then call this Sub procedure with the following code:
Module MainModule
Sub Main()
AddNumbers(2, 3)
Console.ReadLine()
End Sub
End Module
Output
The output of the above code would be:
The sum is: 5
Explanation
In the above example, we define a Sub procedure named AddNumbers
that takes in two integer parameters. The Console.WriteLine
method is used to output the sum of these two parameters to the Console. The Main
method then calls the AddNumbers
procedure with the values 2 and 3.
Use
Sub procedures are used to group related code together into a reusable block. They are commonly used to implement shared functionality that can be called from multiple parts of the program.
Important Points
- Sub procedures are defined using the
Sub
keyword and do not return a value. - Sub procedures can take parameters.
- Sub procedures can be called from other parts of the program.
Summary
In this page, we discussed Sub procedures in VB.NET. We covered their syntax, example, output, explanation, use, and important points. Sub procedures are useful for grouping related code together that can be reused in multiple parts of the program.