VB.NET tutorial-Sub-procedure and Function

VB.NET Sub-procedure and Function

Sub-procedure and function are blocks of code to accomplish specific tasks. They are executed when they are called. A sub-procedure does not return a value while a function returns a value.


You can create a sub-procedure by placing the Sub keyword before the name of the sub-procedure.

Sub procedure_name()


End Sub



Module Module1
    Sub Main()
        printyourname() ' calling sub-procedure
    End Sub
    'defining sub-procedure
    Sub printyourname()
        Dim n As String
        Console.Write("Enter yourname:")
        n = Console.ReadLine
        Console.WriteLine("Welcome " & n & "!")
    End Sub
End Module


Note: the name of sub-procedure or function must start with a letter or underscore not a number


You can use the general form below to create a function in VB.NET. To return a value to the function, you must assign the value to the function name.

function function_name()



End Function


Module Module1
    Sub Main()
        Dim result As Integer
        result = sum2values() 'calling the function
        Console.WriteLine("Result=" & result)
    End Sub

    'defining a function
    Function sum2values()
        Dim x, y As Integer
        x = 10
        y = 20
        sum2values = x + y
    End Function End Module


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.