Difference between function and subroutine with example

1 Answers
Answered by suresh

Difference between Function and Subroutine in VBScript with Example

Difference between Function and Subroutine in VBScript with Example

In VBScript, both Function and Subroutines are used to group a set of statements into a single unit. Below are the key differences between Function and Subroutine:

Function:

  • A Function returns a value after execution.
  • It is called using its name followed by parentheses.
  • It can be used in expressions or assignments.
  • Example:
  • Function AddNumbers(num1, num2)
        AddNumbers = num1 + num2
    End Function
    result = AddNumbers(5, 3)
                

Subroutine:

  • A Subroutine does not return any value after execution.
  • It is called using its name followed by parentheses.
  • It does not participate in expressions or assignments.
  • Example:
  • Sub ShowMessage()
        MsgBox "Hello, World!"
    End Sub
    Call ShowMessage()
                

It is important to choose between Function and Subroutine based on whether a return value is needed or not after execution.

Answer for Question: Difference between function and subroutine with example