Methods Introduction

Method is a named grouping of one or more lines of code. There are two types of methods:

  • subroutines (doesn’t returns a value)
  • functions (returns a value)

Subroutines are declared with the Sub keyword, and functions are declared with the Function keyword.

Example (Subroutine)

Module Module1

    Sub Main()

        MySub() ' calling subroutine
        Console.ReadLine()
    End Sub

    Private Sub MySub()
        Console.WriteLine("Inside subroutine")
    End Sub

End Module

Output :
Inside subroutine

Example (Function)

Module Module1

    Sub Main()

        Dim returnedValue As Integer
        returnedValue = MyFunc()
        Console.WriteLine(returnedValue)

        Console.ReadLine()
    End Sub

    Private Function MyFunc() As Integer
        Console.WriteLine("I am Inside function, and also return a value")
        Return 100
    End Function

End Module

Output :
I am Inside function, and also return a value
100

Share

You may also like...